yamlconfig

package module
v0.0.0-...-7940e33 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2018 License: MIT Imports: 2 Imported by: 0

README

go-yamlconfig

This is a small library for parsing yaml config files. The main advantage of this over plain yaml parsing is this library falls back on defaults block when getting key value, ie the way Rails defines its yaml config files.

Usage

Initialize client:

var exampleConfig = `
defaults: &defaults
 env: development

development:
 <<: *defaults // inherits from defaults

production:
 <<: *defaults   // inherits from defaults
 env: production // overrides defaults for this specific key
`

config, err := New([]byte(exampleConfig))
if err != nil {
  return err
}

Get default environment (development) config:

val, err := config.GetString("database")
if err != nil {
  return err
}

fmt.Println(val) // development

Get config from another environment:

// override environment
config.SetEnv(Production)
val, err = config.GetString("database")
if err != nil {
  return err
}

fmt.Println(val) // production

If config is not string, Get can be used to return an interface{} value:

val1, err := config.Get("database")
if err != nil {
  return err
}

fmt.Println(val) // production

Install

go get github.com/sent-hil/go-yamlconfig

Test

go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	YamlStruct map[string]interface{}
	// contains filtered or unexported fields
}

func New

func New(data []byte) (*Client, error)

New initializes Client after unmarshalling given data arg into yaml.

func (*Client) Get

func (r *Client) Get(key string) (interface{}, error)

Get gets given key from set env block. If key is not found in env, it looks in `defaults` blocks. If key doesn't exist there either, it returns ErrKeyNotFound.

func (*Client) GetEnv

func (r *Client) GetEnv() Env

GetEnv returns current env. If not set, it returns Development.

func (*Client) GetString

func (r *Client) GetString(key string) (string, error)

GetString gets string value from given key from set env block. Use `Get` for interface return value.

func (*Client) MustGet

func (r *Client) MustGet(key string) interface{}

func (*Client) MustGetString

func (r *Client) MustGetString(key string) string

func (*Client) SetEnv

func (r *Client) SetEnv(env Env)

SetEnv sets given env. This should be used to set env based on env vars or user specified env.

type Env

type Env string
var (
	// preset environments
	Development Env = "development"
	Test        Env = "test"
	Staging     Env = "staging"
	Production  Env = "production"

	// name of block that'll be used to look for key, if key is not in env.
	Default = "defaults"

	ErrKeyNotFound = errors.New("Key not defined for env.")
)

Jump to

Keyboard shortcuts

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