scfg

package module
v0.0.0-...-2ae16e7 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 10 Imported by: 33

README

go-scfg

godocs.io

Go library for scfg.

Contributing

Send patches on the mailing list.

License

MIT

Documentation

Overview

Package scfg parses and formats configuration files.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(w io.Writer, blk Block) error

Write writes a parsed configuration to the provided io.Writer.

Types

type Block

type Block []*Directive

Block is a list of directives.

func Load

func Load(path string) (Block, error)

Load loads a configuration file.

func Read

func Read(r io.Reader) (Block, error)

Read parses a configuration file from an io.Reader.

func (Block) Get

func (blk Block) Get(name string) *Directive

Get returns the first directive with the provided name.

func (Block) GetAll

func (blk Block) GetAll(name string) []*Directive

GetAll returns a list of directives with the provided name.

type Decoder

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

Decoder reads and decodes an scfg document from an input stream.

Example
var data struct {
	Foo int `scfg:"foo"`
	Bar struct {
		Param string `scfg:",param"`
		Baz   string `scfg:"baz"`
	} `scfg:"bar"`
}

raw := `foo 42
bar asdf {
	baz hello
}
`

r := strings.NewReader(raw)
if err := scfg.NewDecoder(r).Decode(&data); err != nil {
	log.Fatal(err)
}

fmt.Printf("Foo = %v\n", data.Foo)
fmt.Printf("Bar.Param = %v\n", data.Bar.Param)
fmt.Printf("Bar.Baz = %v\n", data.Bar.Baz)
Output:

Foo = 42
Bar.Param = asdf
Bar.Baz = hello

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder which reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode reads scfg document from the input and stores it in the value pointed to by v.

If v is nil or not a pointer, Decode returns an error.

Blocks can be unmarshaled to:

  • Maps. Each directive is unmarshaled into a map entry. The map key must be a string.
  • Structs. Each directive is unmarshaled into a struct field.

Duplicate directives are not allowed, unless the struct field or map value is a slice of values representing a directive: structs or maps.

Directives can be unmarshaled to:

  • Maps. The children block is unmarshaled into the map. Parameters are not allowed.
  • Structs. The children block is unmarshaled into the struct. Parameters are allowed if one of the struct fields contains the "param" option in its tag.
  • Slices. Parameters are unmarshaled into the slice. Children blocks are not allowed.
  • Arrays. Parameters are unmarshaled into the array. The number of parameters must match exactly the length of the array. Children blocks are not allowed.
  • Strings, booleans, integers, floating-point values, values implementing encoding.TextUnmarshaler. Only a single parameter is allowed and is unmarshaled into the value. Children blocks are not allowed.

The decoding of each struct field can be customized by the format string stored under the "scfg" key in the struct field's tag. The tag contains the name of the field possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name. As a special case, if the field name is "-", the field is ignored. The "param" option specifies that directive parameters are stored in this field (the name must be empty).

type Directive

type Directive struct {
	Name   string
	Params []string

	Children Block
	// contains filtered or unexported fields
}

Directive is a configuration directive.

func (*Directive) ParseParams

func (d *Directive) ParseParams(params ...*string) error

ParseParams extracts parameters from the directive. It errors out if the user hasn't provided enough parameters.

Jump to

Keyboard shortcuts

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