config

package module
v0.0.0-...-4d3ae11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2018 License: MIT Imports: 12 Imported by: 0

README

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


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

Examples

Constants

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

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

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

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

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

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

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

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

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.

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

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL