confloader

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 10 Imported by: 1

README

Configuration Loader

Build Status Coverage Status Software License

Presentation

Confloader is a minimal configuration file loader without any fancy features. It accepts JSON and YAML formats.

How to use it

The following example uses all of the methods available in confloader:

Here are the configuration file that will be used for our example. You can write it in YAML or JSON, whichever you prefer:

{
    "paramString": "foo",
    "paramInt": 42,
    "paramFloat": 42.1,
    "paramBool": true,
    "paramDuration": "20s",
    "paramObj": {
        "paramStringArray": ["foo", "bar", "baz"],
        "paramIntArray": [42, 43, 44],
        "paramFloatArray": [42.2, 43.4, 44.6],
        "paramBoolArray": [true, false, true],
        "paramDurationArray": ["42ns", "5m", "10h10m"]
    },
    "paramEnv": "${ENV_FOO}"
}
paramString: foo
paramInt: 42
paramFloat: 42.1
paramBool: true
paramDuration: 20s
paramObj:
  paramStringArray: ["foo", "bar", "baz"]
  paramIntArray: [42, 43, 44]
  paramFloatArray: [42.2, 43.4, 44.6]
  paramBoolArray: # you can also write it this way
   - true
   - false
   - true
  paramDurationArray: [42ns, 5m, 10h10m]
paramEnv: ${ENV_FOO}

Then, you can use them in your code like so:

package main

import (
    "fmt"
    cl "github.com/blakelead/confloader"
)

func main() {
    config, err := cl.Load("conf.json") // or cl.Load("conf.yml")
    if err != nil {
        panic(err)
    }

    // Examples for all methods provided by the library

    ps := config.GetString("paramString")
    fmt.Println(ps) // foo

    pi := config.GetInt("paramInt")
    fmt.Println(pi) // 42

    pd := config.GetDuration("paramDuration")
    fmt.Println(pd) // 20s

    pf := config.GetFloat("paramFloat")
    fmt.Println(pf) // 42.1

    pb := config.GetBool("paramBool")
    fmt.Println(pb) // true

    psa := config.GetStringArray("paramObj.paramStringArray")
    fmt.Println(psa) // [foo bar baz]

    pia := config.GetIntArray("paramObj.paramIntArray")
    fmt.Println(pia) // [42 43 44]

    pda := config.GetDurationArray("paramObj.paramDurationArray")
    fmt.Println(pda) // [42ns 5m 10h10m0s]

    pfa := config.GetFloatArray("paramObj.paramFloatArray")
    fmt.Println(pfa) // [42.2 43.4 44.6]

    pba := config.GetBoolArray("paramObj.paramBoolArray")
    fmt.Println(pba) // [true false true]

    // It is also possible to access elements in an array with the following syntax

    pa1 := config.GetInt("paramObj.paramIntArray.1")
    fmt.Println(pa1) // 43

    // Some basic conversions are done automatically

    // bool -> int: true=1 false=0
    pbi := config.GetInt("paramObj.paramBoolArray.1")
    fmt.Println(pbi) // 0

    // int -> bool 0=false else=true
    pib := config.GetBool("paramObj.paramIntArray.1")
    fmt.Println(pib) // true

    // float64 -> string
    pfs := config.GetString("paramObj.paramFloatArray.1")
    fmt.Println(pfs) // 43.4

    // []string -> string
    pss := config.GetString("paramObj.paramStringArray")
    fmt.Println(pss) // foo,bar,baz

    // Finally, environment variables can be used inside configuration file,
    // but only for string values

    // export ENV_FOO=fooz
    pe := config.GetString("paramEnv")
    fmt.Println(pe) // fooz
}

Author Information

Adel Abdelhak

Documentation

Overview

Package confloader is a simple configuration file loader that accepts both JSON and YAML file formats.

Configuration file (JSON):

{
  "paramString": "foo"
}

Code:

package main
import (
    "fmt"
    cl "github.com/blakelead/confloader"
)
func main() {
    cnf, err := confloader.Load("conf.json")
    if err != nil {
        panic(err)
    }
    ps := cnf.GetString("paramString")
    fmt.Println(ps) // foo
}

Parameters can be of type string, int, float, bool, duration and array. See the Github Readme for a more thorough example.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config map[string]interface{}

Config is a map of parameters. Each key corresponds to the absolute path of the parameter in the configuration file. Values are the raw value of the parameter.

func Load

func Load(filename string) (Config, error)

Load loads a configuration file and returns a Config object, or an error if file could not be read or unmarshalled, or if the file doesn't exist.

func (*Config) Get

func (c *Config) Get(p string) interface{}

Get gets value of parameter p. p should be the absolute path to the parameter. Example: { "param1": { "param2": 3.14 } }; to access param2, p should be "param1.param2".

func (*Config) GetBool

func (c *Config) GetBool(p string) (b bool)

GetBool gets number value of parameter p. If parameter is a number, the boolean will be true if parameter is not 0, false otherwise.

func (*Config) GetBoolArray

func (c *Config) GetBoolArray(p string) (a []bool)

GetBoolArray gets a bool slice from parameter p.

func (*Config) GetDuration

func (c *Config) GetDuration(p string) (d time.Duration)

GetDuration gets duration value of parameter p. p can have suffixes like s, ms, h, etc. In fact the same as standard time.ParseDuration().

func (*Config) GetDurationArray

func (c *Config) GetDurationArray(p string) []time.Duration

GetDurationArray gets a duration slice from parameter p.

func (*Config) GetFloat

func (c *Config) GetFloat(p string) (f float64)

GetFloat gets float value of parameter p. If parameter is a boolean, the number will be 1.0 if true, 0.0 if false.

func (*Config) GetFloatArray

func (c *Config) GetFloatArray(p string) (a []float64)

GetFloatArray gets a float64 slice from parameter p.

func (*Config) GetInt

func (c *Config) GetInt(p string) int

GetInt gets int value of parameter p.

func (*Config) GetIntArray

func (c *Config) GetIntArray(p string) []int

GetIntArray gets a int slice from parameter p.

func (*Config) GetString

func (c *Config) GetString(p string) (s string)

GetString gets string value of parameter p. If parameter is a number, the number is converted to a string. If parameter is a boolean, the string will be "true" or "false".

func (*Config) GetStringArray

func (c *Config) GetStringArray(p string) (a []string)

GetStringArray gets a string slice from parameter p.

Jump to

Keyboard shortcuts

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