Documentation ¶
Overview ¶
Package scfg parses and formats configuration files.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Block ¶
type Block []*Directive
Block is a list of directives.
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 ¶
NewDecoder returns a new decoder which reads from r.
func (*Decoder) Decode ¶
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 ¶
ParseParams extracts parameters from the directive. It errors out if the user hasn't provided enough parameters.