yaml

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2018 License: MIT Imports: 2 Imported by: 10

Documentation

Overview

Here use YAML format content as config source

Usage please see example:

Example
// add yaml decoder
// only add decoder
// config.SetDecoder(config.Yaml, Decoder)
// Or
config.AddDriver(config.Yaml, Driver)
// Or
// config.DecoderEncoder(config.Yaml, Decoder, Encoder)

err := config.LoadFiles("testdata/yml_other.yml")
if err != nil {
	panic(err)
}

// load from string
config.LoadSources(config.Yaml, []byte(yamlStr))

fmt.Print("get config example:\n")

name, ok := config.GetString("name")
fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name)

arr1, ok := config.GetStringArr("arr1")
fmt.Printf("- get array\n ok: %v, val: %#v\n", ok, arr1)

val0, ok := config.GetString("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n ok: %v, val: %#v\n", ok, val0)

map1, ok := config.GetStringMap("map1")
fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1)

val0, ok = config.GetString("map1.key")
fmt.Printf("- get sub-value by path 'map.key'\n ok: %v, val: %#v\n", ok, val0)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.DefString("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.DefString("envKey1", ""))
Output:

get config example:
- get string
ok: true, val: app2
- get array
ok: true, val: []string{"val1", "val21"}
- get sub-value by path 'arr.index'
ok: true, val: "val1"
- get map
ok: true, val: map[string]string{"key":"val2", "key2":"val20"}
- get sub-value by path 'map.key'
ok: true, val: "val2"
get env 'envKey' val: /bin/zsh
get env 'envKey1' val: defValue
Example (ExportConfig)
// Notice: before dump please set driver encoder
config.SetEncoder(config.Yaml, Encoder)

buf := new(bytes.Buffer)
_, err := config.DumpTo(buf, config.Yaml)
if err != nil {
	panic(err)
}

fmt.Printf("export config:\n%s", buf.String())
Output:

arr1:
	- val1
	- val21
baseKey: value2
debug: false
... ...

Index

Examples

Constants

This section is empty.

Variables

View Source
var Decoder config.Decoder = func(blob []byte, ptr interface{}) (err error) {
	return yaml.Unmarshal(blob, ptr)
}

Decoder the yaml content decoder

View Source
var Driver = &yamlDriver{config.Yaml}

Driver

View Source
var Encoder config.Encoder = func(ptr interface{}) (out []byte, err error) {
	return yaml.Marshal(ptr)
}

Encoder the yaml content encoder

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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