conf_loader

package module
v0.0.0-...-8ceb174 Latest Latest
Warning

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

Go to latest
Published: May 14, 2020 License: MIT Imports: 5 Imported by: 0

README

conf-loader

Unmarshal values from env, json and yaml.

Usage example

ENV variables:

MYAPP_INT_VAL=1
MYAPP_STRING_ENV=test1
MYAPP_OPTIONS_FIRST=1
MYAPP_OPTIONS_SECOND=test1

JSON file overwrites two values:

{
  "int_val": 2,
  "options": {
    "second": "test2"
  }
}

Go app:

package main
import (
	"fmt"
	"github.com/golanghelper/conf-loader"
	"log"
	"os"
)
// prepare test structures
type testOptionsConfig struct {
	First  string
	Second string
}
type testConfig struct {
	IntVal    int                `json:"int_val" envconfig:"int_val" yaml:"int_val"`
	StringVal string             `json:"string_json" envconfig:"string_env" yaml:"string_yaml"`
	Options   *testOptionsConfig `json:"options" envconfig:"options" yaml:"options"`
}
func main() {
	f, _ := os.Open("data.example.json")
	var v testConfig
	e := conf_loader.Unmarshal("myapp", f, &v)
	if e != nil {
		log.Fatalf("Load config error: %s", e.Error())
	}
	fmt.Print(v)
}

Output:

{2 test1 0xc042002620}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(envPrefix string, f *os.File, v interface{}) (e error)

Unmarshal parses data from environment variables and a file (if defined) and stores the result in the value pointed to by v

Types

This section is empty.

Jump to

Keyboard shortcuts

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