import "github.com/mkmueller/config"
Config provides encoding and decoding routines for configuration files. This package supports most of the built-in datatypes, including string, int8-64, uint8-64, float32-64, time.Time, struct, and string-keyed maps. Deeply nested structs are supported as well as maps of structs. The data types not supported are complex64/128 and slices.
This package also provides a Parse function which will allow any configuration data to be parsed directly into a string map.
At this writing, struct tags are not supported. However, optional flags provide a means to convert all fields to lower case or snake_case for encoding and decoding.
- Constants
- func Decode(x interface{}, src interface{}, options ...int) error
- func DecodeFile(filename string, x interface{}, options ...int) error
- func Encode(x interface{}, options ...int) ([]byte, error)
- func EncodeToFile(x interface{}, filename string, options ...int) error
- type Decoder
- type Encoder
- type Parser
- type StringMap
const (
// ALLOW_SNAKE_CASE will cause the decoder to interpret snake case fields in
// the configuration file if the supplied struct is using Pascal case, eg.
// crew_members == CrewMembers. Decode will attempt to find the actual struct
// field before trying snake case.
ALLOW_SNAKE_CASE = 1 << iota
// IGNORE_CASE will cause the decoder to interpret lower case fields in
// the configuration file, eg. crewmembers == CrewMembers. Decode will first
// attempt to find the actual struct field before trying lower case.
IGNORE_CASE
// PARSE_LOWER_CASE will cause the parser to convert all keys to lower case.
PARSE_LOWER_CASE
// ENCODE_SNAKE_CASE will cause the encoder to convert all fields into
// snake case, eg., DarkMatter == dark_matter.
ENCODE_SNAKE_CASE
// ENCODE_SNAKE_CASE will cause the encoder to convert all fields into
// snake case, eg., DarkMatter == darkmatter.
ENCODE_LOWER_CASE
// ENCODE_ZERO_VALUES will cause zero values in the supplied struct to be encoded.
ENCODE_ZERO_VALUES
// OVERWRITE_FILE will cause the function EncodeToFile() to overwrite the
// supplied filename if it already exists.
OVERWRITE_FILE
)func Decode(x interface{}, src interface{}, options ...int) errorDecode will accept a string, byte slice, or anything that implements an io.Reader
func DecodeFile(filename string, x interface{}, options ...int) errorDecodeFile will decode the supplied file into the supplied struct. Decoder options are optional.
func Encode(x interface{}, options ...int) ([]byte, error)func EncodeToFile(x interface{}, filename string, options ...int) errortype Decoder struct {
// contains filtered or unexported fields
}The Decoder converts the parsed data to the expected data type and assignes it to a struct.
func NewDecoder(x interface{}, options ...int) *DecoderNewDecoder accepts a pointer to a struct or a map and returns a new Decoder.
func (*Decoder) DecodeBytes
func (o *Decoder) DecodeBytes(bs []byte) errorDecodeBytes will accept a byteslice
func (*Decoder) DecodeFile
func (o *Decoder) DecodeFile(filename string) errorDecodeFile will decode the supplied filename
func (*Decoder) DecodeStream
func (o *Decoder) DecodeStream(r io.Reader) errorDecodeStream will accept an io.Reader
func (*Decoder) DecodeString
func (o *Decoder) DecodeString(s string) errorDecodeString will accept a string
type Encoder struct {
// contains filtered or unexported fields
}The Encoder handles encoding a struct to an io.Writer.
func NewEncoder(x interface{}, options ...int) *EncoderNewEncoder accepts a struct or map and returns a new Encoder.
func (o *Encoder) ToBytes(bs *[]byte) errorToBytes
func (o *Encoder) ToFile(filename string) errorToFile will encode a struct to the supplied filename. If the file exists, it will not be overwritten unless the overwrite options is used.
func (o *Encoder) ToStream(w io.Writer) errorToStream
type Parser struct {
// contains filtered or unexported fields
}The Parser handles parsing input data from a reader.
func NewParser(options ...int) *ParserNewParser returns a new Parser.
func (o *Parser) Includes() []stringIncludes will return a list of file names that have been included in the source configuration file.
func (o *Parser) Parse(bs []byte) (StringMap, error)Parse a byte slice to a string map.
func (*Parser) ParseStream
func (o *Parser) ParseStream(r io.Reader) (StringMap, error)Parse a stream to a string map.
type StringMap map[string]stringType StringMap is the data type output by the Parse function.
func Parse(src interface{}, options ...int) (StringMap, error)Parse a string, a byte slice or an io.Reader to a string map.
func ParseFile(filename string, options ...int) (StringMap, error)Parse a file