config: github.com/mkmueller/config Index | Examples | Files

package config

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, byte arrays, 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.

Index

Examples

Package Files

decode.go encode.go parser.go

Constants

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 Uses

func Decode(x interface{}, src interface{}, options ...int) error

Decode will accept a string, byte slice, or anything that implements an io.Reader

Decode a string

Code:

var x struct {
    Pi               float32
    Question, Answer string
}
cfg := `
    pi = 3.1415927
    question = What is my purpose?
    answer = You pass butter.
    `
if err := config.Decode(&x, cfg, config.IGNORE_CASE); err != nil {
    log.Println(err)
}
fmt.Println(x.Pi)
fmt.Println(x.Question)
fmt.Println(x.Answer)

Output:

3.1415927
What is my purpose?
You pass butter.

Decode float64 values to a map.

Code:

m := make(map[string]float64)
cfg := `
	    e = 2.718281828459
	    pi = 3.14159265359
	`
err := config.Decode(m, cfg)
if err != nil {
    log.Fatal(err)
}
fmt.Println(m["e"])
fmt.Println(m["pi"])

Output:

2.718281828459
3.14159265359

func DecodeFile Uses

func DecodeFile(filename string, x interface{}, options ...int) error

DecodeFile will decode the supplied file into the supplied struct. Decoder options are optional.

func Encode Uses

func Encode(x interface{}, options ...int) ([]byte, error)

Encode a struct using lower case option.

Code:

x := struct {
    E   float64
    Pi  float64
}{2.718281828459, 3.14159265359}
ba, err := config.Encode(x, config.ENCODE_LOWER_CASE)
if err != nil {
    log.Print(err)
    return
}
fmt.Printf("%s", ba)

Output:

e = 2.718281828459
pi = 3.14159265359

Encode a map of float64 values.

Code:

x := make(map[string]float64)
x["e"] = 2.718281828459
x["pi"] = 3.14159265359
ba, err := config.Encode(x)
if err != nil {
    log.Print(err)
    return
}
fmt.Printf("%s", ba)

Output:

e = 2.718281828459
pi = 3.14159265359

func EncodeToFile Uses

func EncodeToFile(x interface{}, filename string, options ...int) error

type Decoder Uses

type 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 Uses

func NewDecoder(x interface{}, options ...int) *Decoder

NewDecoder accepts a pointer to a struct or a map and returns a new Decoder.

Code:

var x struct{ Pi float32 }
cfg := "pi = 3.1415927"
o := config.NewDecoder(&x, config.IGNORE_CASE)
err := o.DecodeString(cfg)
if logError(err) {
    return
}
fmt.Println(x.Pi)

Output:

3.1415927

func (*Decoder) DecodeBytes Uses

func (o *Decoder) DecodeBytes(bs []byte) error

DecodeBytes will accept a byteslice

func (*Decoder) DecodeFile Uses

func (o *Decoder) DecodeFile(filename string) error

DecodeFile will decode the supplied filename

func (*Decoder) DecodeStream Uses

func (o *Decoder) DecodeStream(r io.Reader) error

DecodeStream will accept an io.Reader

func (*Decoder) DecodeString Uses

func (o *Decoder) DecodeString(s string) error

DecodeString will accept a string

type Encoder Uses

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

The Encoder handles encoding a struct to an io.Writer.

func NewEncoder Uses

func NewEncoder(x interface{}, options ...int) *Encoder

NewEncoder accepts a struct or map and returns a new Encoder.

Code:

x := struct{ Pi float64 }{3.14159265359}
var ba []byte
o := config.NewEncoder(x)
o.ToBytes(&ba)
fmt.Printf("%s", ba)

Output:

Pi = 3.14159265359

func (*Encoder) ToBytes Uses

func (o *Encoder) ToBytes(bs *[]byte) error

ToBytes

func (*Encoder) ToFile Uses

func (o *Encoder) ToFile(filename string) error

ToFile will encode a struct to the supplied filename. If the file exists, it will not be overwritten unless the overwrite options is used.

func (*Encoder) ToStream Uses

func (o *Encoder) ToStream(w io.Writer) error

ToStream

type Parser Uses

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

The Parser handles parsing input data from a reader.

func NewParser Uses

func NewParser(options ...int) *Parser

NewParser returns a new Parser.

func (*Parser) Includes Uses

func (o *Parser) Includes() []string

Includes will return a list of file names that have been included in the source configuration file.

func (*Parser) Parse Uses

func (o *Parser) Parse(bs []byte) (StringMap, error)

Parse a byte slice to a string map.

func (*Parser) ParseStream Uses

func (o *Parser) ParseStream(r io.Reader) (StringMap, error)

Parse a stream to a string map.

type StringMap Uses

type StringMap map[string]string

Type StringMap is the data type output by the Parse function.

func Parse Uses

func Parse(src interface{}, options ...int) (StringMap, error)

Parse a string, a byte slice or an io.Reader to a string map.

func ParseFile Uses

func ParseFile(filename string, options ...int) (StringMap, error)

Parse a file

Package config imports 12 packages (graph). Updated 2018-05-02. Refresh now. Tools for package owners.