Documentation ¶
Overview ¶
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 ¶
- 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
Examples ¶
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 )
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode will accept a string, byte slice, or anything that implements an io.Reader
Example ¶
Decode a string
package main import ( "fmt" "github.com/mkmueller/config" "log" ) func main() { 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.
Example (Map) ¶
Decode float64 values to a map.
package main import ( "fmt" "github.com/mkmueller/config" "log" ) func main() { 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 ¶
DecodeFile will decode the supplied file into the supplied struct. Decoder options are optional.
func Encode ¶
Example ¶
Encode a struct using lower case option.
package main import ( "fmt" "github.com/mkmueller/config" "log" ) func main() { 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
Example (Map) ¶
Encode a map of float64 values.
package main import ( "fmt" "github.com/mkmueller/config" "log" ) func main() { 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 ¶
Types ¶
type Decoder ¶
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 ¶
NewDecoder accepts a pointer to a struct or a map and returns a new Decoder.
Example ¶
package main import ( "fmt" "github.com/mkmueller/config" "log" ) func main() { 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) } func logError(err error) bool { if err != nil { log.Print(err) return true } return false }
Output: 3.1415927
func (*Decoder) DecodeBytes ¶
DecodeBytes will accept a byteslice
func (*Decoder) DecodeFile ¶
DecodeFile will decode the supplied filename
func (*Decoder) DecodeStream ¶
DecodeStream will accept an io.Reader
func (*Decoder) DecodeString ¶
DecodeString will accept a string
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
The Encoder handles encoding a struct to an io.Writer.
func NewEncoder ¶
NewEncoder accepts a struct or map and returns a new Encoder.
Example ¶
package main import ( "fmt" "github.com/mkmueller/config" ) func main() { x := struct{ Pi float64 }{3.14159265359} var ba []byte o := config.NewEncoder(x) o.ToBytes(&ba) fmt.Printf("%s", ba) }
Output: Pi = 3.14159265359
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
The Parser handles parsing input data from a reader.
func (*Parser) Includes ¶
Includes will return a list of file names that have been included in the source configuration file.