config

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

Config

The objective of the config package is to allow a client to tag a struct with (optional)default values and environment variables and then the config package will use viper to populate a struct.

Tag descriptions

Tag Mandatory Description
env Y The environment variable the field value will be retrieved from if the environment variable is present. N.B. It takes priority over any other tag.
file N The path to the file to take the default value from. This can be useful for things like passwords where they can be taken from docker / k8s secrets. N.B. If this tag is present and the file can be successfully processed then the default is redundant - if the value can not be read from the file and the default is present then the default is used.
default N The default value which will be used if no environment variable is present. N.B. If this is not populated then the default for the type will be used i.e. 0 for int, "" for string, false for bool etc etc.

N.B. A nested struct does not need tags associated with it. Tags are only required for non struct entries.
N.N.B. Nested structs will use the "squash" property by default. That means no mapstructure tag is required.

Struct API

LoadViperConfig(cfg interface{}) error

Parameters
Name Input/Output Type Description
cfg input/output interface{} A pointer to the config structure which will be populated on return.
- output error. This will hold an error if there are any issues with the tagging or viper was unable to unmarshal

File API

LoadViperConfigFromFile(filename string, cfg interface{}) error

Parameters
Name Input/Output Type Description
filename input string The filename of the config file - this could be yaml, json, xml, env or ini. (i.e. anything which Viper supports)
cfg input/output interface{} A pointer to the config structure which will be populated on return.

Reader API

LoadViperConfigFromReader(reader io.Reader, cfg interface{}, cfgType string) error

Parameters
Name Input/Output Type Description
reader input io.Reader An io.reader which can be attached to a file, a string, a byte buffer etc etc
cfg input/output interface{} A pointer to the config structure which will be populated on return.
cfgType input string The type of config contained within the reader - N.B. This is the range of supported viper types and can be json, yaml/yml, hcl, props, env, toml, ini. See viper docs for the full set.
- output error. This will hold an error if there are any issues with the tagging or viper was unable to unmarshal
Examples
Simple Struct
type TestStruct struct {       
   TestVal string `env:"TEST_VAL" default:"abc"`   
   TestFileVal string `env:"TEST_FILE_VAL" file:"/tmp/test_file_val"`  
} 
Nested Structs
//InnerStruct is the nested struct 
type InnerStruct struct {    
TestNestInner string `env:"TEST_NEST_INNER" default:"inner"`  
}
   
//OuterStruct is the struct containing the nested struct 
type OuterStruct struct {    
TestNestOuter string `env:"TEST_NEST_OUTER" default:"outer"` InnerStruct  
}

N.B. This library only supports anonymous nested structs.

Handling the response

The struct passed in will be populated upon return if no error.

var testStruct TestStruct
err := config.LoadViperConfig(&testStruct) 
if err != nil {      
	//Error handling code 
} 

Documentation

Overview

Package config provides facilities for application config.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadViperConfig

func LoadViperConfig(cfg interface{}) error

LoadViperConfig populates the cfg structure passed in (it must be the address passed in).

func LoadViperConfigFromFile

func LoadViperConfigFromFile(filename string, cfg interface{}) error

LoadViperConfigFromFile populates the cfg structure passed in (it must be the address passed in). The config will be read from a file but environment variables override and default values can still be set. The file can be anything that Viper supports. N.B. This only supports anonymous nested structs.

func LoadViperConfigFromReader

func LoadViperConfigFromReader(in io.Reader, cfg interface{}, cfgType string) error

LoadViperConfigFromReader will populate the cfg structure passed in (it must be the address passed in). The config will be read from a reader which must be setup prior to the call. N.B. The cfgType can be anything supported by viper i.e. yaml, json, env, ini, toml.

Types

This section is empty.

Jump to

Keyboard shortcuts

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