Provides encoding and decoding routines for configuration files
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE.txt
README.md
decode.go
decode_test.go
encode.go
encode_test.go
example-conf.html
example.conf
examples_test.go
parser.go
parser_test.go
setup_test.go

README.md

MarkMueller GoDoc GoDoc

config

import "github.com/mkmueller/config"

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

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

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

func DecodeFile

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

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

func EncodeToFile

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

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

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

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

func (*Decoder) DecodeBytes

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

DecodeBytes will accept a byteslice

func (*Decoder) DecodeFile

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

DecodeFile will decode the supplied filename

func (*Decoder) DecodeStream

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

DecodeStream will accept an io.Reader

func (*Decoder) DecodeString

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

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

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

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

func (*Encoder) ToBytes

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

ToBytes

func (*Encoder) ToFile

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

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

ToStream

type Parser

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

The Parser handles parsing input data from a reader.

func NewParser

func NewParser(options ...int) *Parser

NewParser returns a new Parser.

func (*Parser) Includes

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

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

type StringMap map[string]string

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

func Parse

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

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

func ParseFile

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

Parse a file