keyfile

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 12 Imported by: 0

README

Keyfile parser for Go

Keyfile is simular to INI file, but it is much flexible. It supports maps and lists as built-in types.

You can find more information about Keyfile format in https://docs.gtk.org/glib/struct.KeyFile.html

Installation

go get -u github.com/ksckaan1/keyfile@latest

Usage

Example keyfile data:
[profile]
name=John Doe
age=42
is_working=true
hobbies=swimming;reading
job=Software Developer
job[de]=Softwareentwickler
job[sp]=Desarrollador de software
job[tr]=Yazılım Geliştirici
Example Model
type Config struct{
  Profile struct{
    Name      string            `keyfile:"name"`
    Age       int               `keyfile:"age"`
    IsWorking bool              `keyfile:"is_working"`
    Hobbies   []string          `keyfile:"hobbies"`
    Job       map[string]string `keyfile:"job"
  } `keyfile:"example"`
}
Unmarshal
err := keyfile.Unmarshal([]byte(data), &config)
if err != nil {
 // handle error
}
Marshal
data, err := keyfile.Marshal(config)
if err != nil {
 // handle error
}

Supported Types

  • string
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • complex64, complex128
  • bool
  • slice of supported types
  • map of supported types

Working with Custom Types

Custom types can be used with keyfile parser. If custom type is underlying supported type, no need to do anything. If it is not, must be implement Unmarshaler or Marshaler interface like in std json package.

Example:

type CustomType struct {
  value string
}

func (c *CustomType) UnmarshalJSON(b []byte) error {
  c.value = string(b)
  return nil
}

func (c *CustomType) MarshalJSON() ([]byte, error) {
  return []byte(c.value), nil
}

Struct Tags

The keyfile tag is used for the struct tag. It is similar to the JSON struct tag.

type Config struct{
  Example struct{
    // Key1 is ignored
    Key1       string   `keyfile:"-"`
    // ignore Key2 if it's zero value
    Key2       string   `keyfile:"key2,omitempty"`
    // split Key3 with comma (default is semicolon)
    Key3       []string `keyfile:"key3;sep:,"`
    // its unexported, so it will not be included in the keyfile
    unexported string
  } `keyfile:"example"`
}

Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Parameter
	ErrParameterMustBePointer = errors.New("keyfile: parameter must be a pointer")
	ErrParameterMustNotBeNil  = errors.New("keyfile: parameter must not be nil")
	ErrInvalidParameterType   = errors.New("keyfile: invalid parameter type")

	// Key-Value Pair
	ErrInvalidMapKeyType = errors.New("invalid map key type, must be a string")
)

Functions

func Marshal

func Marshal(v any) ([]byte, error)

func Unmarshal

func Unmarshal(data []byte, v any) error

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (dec *Decoder) Decode(v any) error

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (enc *Encoder) Encode(v any) error

type ErrCanNotParsed

type ErrCanNotParsed struct {
	Err        error
	SourceKey  string
	TargetName string
	TargetType string
}

func (ErrCanNotParsed) Error

func (e ErrCanNotParsed) Error() string

type ErrInvalidEntry

type ErrInvalidEntry struct {
	Line       string
	LineNumber int
}

func (ErrInvalidEntry) Error

func (e ErrInvalidEntry) Error() string

type ErrInvalidGroupName

type ErrInvalidGroupName struct {
	Line       string
	LineNumber int
}

func (ErrInvalidGroupName) Error

func (e ErrInvalidGroupName) Error() string

type ErrInvalidGroupType

type ErrInvalidGroupType struct {
	GroupName string
	GroupType string
}

func (ErrInvalidGroupType) Error

func (e ErrInvalidGroupType) Error() string

type ErrInvalidKey

type ErrInvalidKey struct {
	Line       string
	LineNumber int
}

func (ErrInvalidKey) Error

func (e ErrInvalidKey) Error() string

type ErrKeyValuePairMustBeContainedInAGroup

type ErrKeyValuePairMustBeContainedInAGroup struct {
	Line       string
	LineNumber int
}

func (ErrKeyValuePairMustBeContainedInAGroup) Error

type ErrUnsupportedValueType

type ErrUnsupportedValueType struct {
	FieldName string
	FieldType string
}

func (ErrUnsupportedValueType) Error

func (e ErrUnsupportedValueType) Error() string

type Marshaler

type Marshaler interface {
	MarshalKeyFile() ([]byte, error)
}

type Unmarshaler

type Unmarshaler interface {
	UnmarshalKeyFile(data []byte) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL