configparser

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 6 Imported by: 4

README

Go Report Card GoDoc

configparser

Parse a json or yaml file and unmarshal the content to a struct type

To set the values of the struct members from environment variables, you can define an "env" struct tag which defines the name of the envvar to set the value from and call configparser.SetValuesFromEnvironmentTag on the already parsed struct or use configparser.SetValuesFromEnvironment to lookup the envvar by the name of the fields.

Example

This is the config yaml

name: sebidude
age: 99
species: unknown
street: see address
address:
  street: homestreet
  city: donk city
limit: 10

Define a stuct type

type Address struct {
	Street string `json:"street" env:"USER_ADDRESS_STREET"`
	City   string `json:"city"`
}

type User struct {
	Name    string  `json:"name" env:"USER_NAME"`
	Age     int     `json:"age" env:"USER_AGE"`
	Species string  `json:"species" env:"USER_SPECIES"`
	Street  string  `json:"street" env:"USER_STREET"`
	NoEnv   string  `json:"noenv"`
	Address Address `json:"address"`
	Limit   int     `json:"limit"`
}
Parse the configfile
...
var user User
configparser.ParseYaml("config.yaml",&user)
...
Use environment variables

Set values from env var using struct tags

configparser.SetValuesFromEnvironmentTag(&user)

The env vars stated in the tags will be looked up and set if not empty

To set the values from env vars without defining a struct tag, use a matching name for the variable and set the values with configparser.SetValuesFromEnvironment("CONFIG",&user)

Example

export CONFIG_ADDRESS_CITY="Londinium"
configparser.SetValuesFromEnvironment("CONFIG",&user)
fmt.Println(user.Address.City)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseJSON

func ParseJSON(filename string, config interface{}) error

ParseJSON reads the file at filename and unmarshals the content to config

func ParseYaml

func ParseYaml(filename string, config interface{}) error

ParseYaml reads the file at filename and unmarshals the content to config

func SetValuesFromEnvironment

func SetValuesFromEnvironment(prefix string, config interface{})

SetValuesFromEnvironment iterates over the struct and checks if environment variables were set. It will transform the name of the field to upper and join the the prefix with an _. The resulting string will be looked up with os.Getenv if found it will set the value of the struct member to the value from the env var.

func SetValuesFromEnvironmentTag

func SetValuesFromEnvironmentTag(config interface{})

SetValuesFromEnvironmentTag iterates over the struct and checks for "env" tags. If an "env" tag was found, it will set the value of the struct member to the value from the specified env var.

Types

This section is empty.

Jump to

Keyboard shortcuts

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