config

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

README

                                            ____________         
    _______ ______       ______________________  __/__(_)______ _
    __  __ `/  __ \_______  ___/  __ \_  __ \_  /_ __  /__  __ `/
    _  /_/ // /_/ //_____/ /__ / /_/ /  / / /  __/ _  / _  /_/ / 
    _\__, / \____/       \___/ \____//_/ /_//_/    /_/  _\__, /  
    /____/                                              /____/

CI Coverage Status Go Report Card PkgGoDev

Simple and idiomatic parsing of configuration files and environment variables.

Installation

$ go get github.com/hapci/go-config

Getting started

Unmarshal files

example.yml

string: "text"
map:
  key1:
    array: [ "item10", "item20", "item30" ]
    int: 40
    float: 50.6
    bool: true

code:

package main

import (
	"fmt"

	"github.com/hapci/go-config"
)

func main() {
	cfg := struct {
		String string `mapstructure:"string"`
		Map    map[string]struct {
			Array []string `mapstructure:"array"`
			Int   int      `mapstructure:"int"`
			Float float64  `mapstructure:"float"`
			Bool  bool     `mapstructure:"bool"`
		} `mapstructure:"map"`
	}{}

	err := config.UnmarshalFromFile("example.yml", &cfg)
	if err != nil {
		fmt.Println(err)
	}
}

Supported formats: JSON, TOML, YAML, HCL, INI, envfile and Java properties.

Unmarshal environment variables

package main

import (
	"fmt"
	"os"

	"github.com/hapci/go-config"
)

func main() {
	err := os.Setenv("int", "1")
	if err != nil {
		fmt.Println(err)
	}

	err = os.Setenv("string", "text")
	if err != nil {
		fmt.Println(err)
	}

	err = os.Setenv("BOOL", "true")
	if err != nil {
		fmt.Println(err)
	}

	cfg := struct {
		Int    int     `env:"int"`
		String string  `env:"string,required=true"`
		Float  float64 `env:"float,default=3.14"`
		Bool   bool    `env:"bool,BOOL"`
	}{}

	err = config.UnmarshalFromEnv(&cfg)
	if err != nil {
		fmt.Println(err)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnmarshalFromEnv

func UnmarshalFromEnv(v interface{}) error

UnmarshalFromEnv parses the environment variables and stores the results in the value pointed to by v. If v is nil or not a pointer to a struct, UnmarshalFromEnv returns an env.ErrInvalidValue.

Fields tagged with "env" will have the unmarshalled env value of the matching key. If the tagged field is not exported, UnmarshalFromEnv returns env.ErrUnexportedField.

If the field has a type that is unsupported, UnmarshalFromEnv returns env.ErrUnsupportedType.

Example
err := os.Setenv("int", "1")
if err != nil {
	fmt.Println(err)
}

err = os.Setenv("string", "text")
if err != nil {
	fmt.Println(err)
}

err = os.Setenv("BOOL", "true")
if err != nil {
	fmt.Println(err)
}

cfg := struct {
	Int    int     `env:"int"`
	String string  `env:"string,required=true"`
	Float  float64 `env:"float,default=3.14"`
	Bool   bool    `env:"bool,BOOL"`
}{}

err = config.UnmarshalFromEnv(&cfg)
if err != nil {
	fmt.Println(err)
}

func UnmarshalFromFile

func UnmarshalFromFile(filename string, v interface{}) error

UnmarshalFromFile decodes the file and assigns the decoded values into the out value.

Fields tagged with "mapstructure" will have the unmarshalled value of the matching key.

Example
cfg := struct {
	String string `mapstructure:"string"`
	Map    map[string]struct {
		Array []string `mapstructure:"array"`
		Int   int      `mapstructure:"int"`
		Float float64  `mapstructure:"float"`
		Bool  bool     `mapstructure:"bool"`
	} `mapstructure:"map"`
}{}

err := config.UnmarshalFromFile("filename.yml", &cfg)
if err != nil {
	fmt.Println(err)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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