config

package module
v0.0.0-...-657c9d2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2016 License: MIT Imports: 8 Imported by: 0

README

Config wercker status GoDoc

Package config provides convenient access methods to configuration stored as JSON or YAML.

This is a fork of the original version. This version extends the functionality of the original without losing compatibility. Major features added:

Example and more information you can find here.

Documentation

Overview

Package config provides convenient access methods to configuration stored as JSON or YAML.

Let's start with a simple YAML example:

development:
  database:
    host: localhost
  users:
    - name: calvin
      password: yukon
    - name: hobbes
      password: tuna
production:
  database:
    host: 192.168.1.1

We can parse it using ParseYaml(), which will return a *Config instance on success:

cfg, err := config.ParseYaml(yamlString)

An equivalent JSON configuration could be built using ParseJson():

cfg, err := config.ParseJson(jsonString)

From now, we can retrieve configuration values using a path in dotted notation:

// "localhost"
host, err := cfg.String("development.database.host")

// or...

// "192.168.1.1"
host, err := cfg.String("production.database.host")

Besides String(), other types can be fetched directly: Bool(), Float64(), Int(), Map() and List(). All these methods will return an error if the path doesn't exist, or the value doesn't match or can't be converted to the requested type.

A nested configuration can be fetched using Get(). Here we get a new *Config instance with a subset of the configuration:

cfg, err := cfg.Get("development")

Then the inner values are fetched relatively to the subset:

// "localhost"
host, err := cfg.String("database.host")

For lists, the dotted path must use an index to refer to a specific value. To retrieve the information from a user stored in the configuration above:

// map[string]interface{}{ ... }
user1, err := cfg.Map("development.users.0")
// map[string]interface{}{ ... }
user2, err := cfg.Map("development.users.1")

// or...

// "calvin"
name1, err := cfg.String("development.users.0.name")
// "hobbes"
name2, err := cfg.String("development.users.1.name")

JSON or YAML strings can be created calling the appropriate Render*() functions. Here's how we render a configuration like the one used in these examples:

cfg := map[string]interface{}{
    "development": map[string]interface{}{
        "database": map[string]interface{}{
            "host": "localhost",
        },
        "users": []interface{}{
            map[string]interface{}{
                "name":     "calvin",
                "password": "yukon",
            },
            map[string]interface{}{
                "name":     "hobbes",
                "password": "tuna",
            },
        },
    },
    "production": map[string]interface{}{
        "database": map[string]interface{}{
            "host": "192.168.1.1",
        },
    },
}

json, err := config.RenderJson(cfg)

// or...

yaml, err := config.RenderYaml(cfg)

This results in a configuration string to be stored in a file or database.

For more more convenience it can parse OS environment variables and command line arguments.

cfg, err := config.ParseYaml(yamlString)
cfg.Env()

// or

cfg.Flag()

We can also specify the order of parsing:

cfg.Env().Flag()

// or

cfg.Flag().Env()

In case of OS environment all existing at the moment of parsing keys will be scanned in OS environment, but in uppercase and the separator will be `_` instead of a `.`. In case of flags separator will be `-`. In case of command line arguments possible to use regular dot notation syntax for all keys. For see existing keys we can run application with `-h`.

We can use unsafe method to get value:

// ""
cfg.UString("undefined.key")

// or with default value
unsafeValue := cfg.UString("undefined.key", "default value")

There is unsafe methods, like regular, but wuth prefix `U`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(cfg interface{}, path string) (interface{}, error)

Get returns a child of the given value according to a dotted path.

func RenderJSON

func RenderJSON(cfg interface{}) (string, error)

RenderJSON renders a JSON configuration.

func RenderYAML

func RenderYAML(cfg interface{}) (string, error)

RenderYAML renders a YAML configuration.

func Set

func Set(cfg interface{}, path string, value interface{}) error

Set returns an error, in case when it is not possible to establish the value obtained in accordance with given dotted path.

Types

type Config

type Config struct {
	Root interface{}
}

Config represents a configuration with convenient access methods.

func Must

func Must(cfg *Config, err error) *Config

Must is a wrapper for parsing functions to be used during initialization. It panics on failure.

func ParseJSON

func ParseJSON(cfg string) (*Config, error)

ParseJSON reads a JSON configuration from the given string.

func ParseJSONFile

func ParseJSONFile(filename string) (*Config, error)

ParseJSONFile reads a JSON configuration from the given filename.

func ParseYAML

func ParseYAML(cfg string) (*Config, error)

ParseYAML reads a YAML configuration from the given string.

func ParseYAMLFile

func ParseYAMLFile(filename string) (*Config, error)

ParseYAMLFile reads a YAML configuration from the given filename.

func (*Config) Bool

func (cfg *Config) Bool(path string) (bool, error)

Bool returns a bool according to a dotted path.

func (*Config) Copy

func (cfg *Config) Copy(dottedPath ...string) (*Config, error)

Copy returns a deep copy with given path or without.

func (*Config) Env

func (cfg *Config) Env() *Config

Env data from system env, based on existing config keys.

func (*Config) Extend

func (cfg *Config) Extend(from *Config) (*Config, error)

Extend returns extended copy of current config with applied values from the given config instance. Note that if you extend with different structure you will get an error. See: `.Set()` method for details.

func (*Config) Flag

func (cfg *Config) Flag() *Config

Flag command line arguments, based on existing config keys.

func (*Config) Float64

func (cfg *Config) Float64(path string) (float64, error)

Float64 returns a float64 according to a dotted path.

func (*Config) Get

func (cfg *Config) Get(path string) (*Config, error)

Get returns a nested config according to a dotted path.

func (*Config) Int

func (cfg *Config) Int(path string) (int, error)

Int returns an int according to a dotted path.

func (*Config) List

func (cfg *Config) List(path string) ([]interface{}, error)

List returns a []interface{} according to a dotted path.

func (*Config) Map

func (cfg *Config) Map(path string) (map[string]interface{}, error)

Map returns a map[string]interface{} according to a dotted path.

func (*Config) MustBool

func (cfg *Config) MustBool(path string, defaults ...bool) bool

MustBool retirns a bool according to a dotted path or default value or false.

func (*Config) MustFloat64

func (cfg *Config) MustFloat64(path string, defaults ...float64) float64

MustFloat64 returns a float64 according to a dotted path or default value or 0.

func (*Config) MustInt

func (cfg *Config) MustInt(path string, defaults ...int) int

MustInt returns an int according to a dotted path or default value or 0.

func (*Config) MustList

func (cfg *Config) MustList(path string, defaults ...[]interface{}) []interface{}

MustList returns a []interface{} according to a dotted path or defaults or []interface{}.

func (*Config) MustMap

func (cfg *Config) MustMap(path string, defaults ...map[string]interface{}) map[string]interface{}

MustMap returns a map[string]interface{} according to a dotted path or default or map[string]interface{}.

func (*Config) MustString

func (cfg *Config) MustString(path string, defaults ...string) string

MustString returns a string according to a dotted path or default or "".

func (*Config) Set

func (cfg *Config) Set(path string, val interface{}) error

Set a nested config according to a dotted path.

func (*Config) String

func (cfg *Config) String(path string) (string, error)

String returns a string according to a dotted path.

Jump to

Keyboard shortcuts

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