configparser

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 5 Imported by: 0

README

go-config-parser

codecov goreportcard pipeline

This is a Go wrapper package for reading and unmarshalling data from configuration files based on Viper.

Installation

To install this package, use go get:

go get github.com/psyb0t/go-config-parser

Import the package into your Go code:

import "github.com/psyb0t/go-config-parser"

Usage

go-config-parser provides the Parse function for reading in and unmarshalling data from a configuration file into a target value. The function takes four arguments:

  • configFileType: a constant representing the type of the configuration file (either ConfigFileTypeJSON or ConfigFileTypeYAML).
  • file: the file path of the configuration file.
  • target: a pointer to the value that the configuration data will be unmarshaled into.
  • defaults: a map of default values that will be set in the configuration if they are not present in the configuration file.
  • envPrefix: a string specifying the environment variables prefix to use when reading env vars (eg. "myPrefix" results in "MYPREFIX_")

The function uses the viper package to read in the configuration file, set the config file type, set default values, and unmarshal the data into the target value. It returns an error if there is a problem reading the configuration file or unmarshalling the data.

The function also handles setting environment variables as configuration sources using viper.AutomaticEnv to allow the configuration data to be overridden by environment variables. Using viper.SetEnvKeyReplacer it replaces periods in the environment variable keys with underscores to match the keys in the configuration file.

The order of importance for the used values are as such: environment variables > config file values > default values.

If an empty file name is provided it will try to populate the target structure via environment variables.

Important

Because of the way the underlying viper package works, environment variables only get recognised if there is either a value for it in a config file or if it can be found in the default values. In order for environment values to properly work with your desired structure make sure to pass in a complete map with defaults even with zero values for all.

Example

Here is an example of how to use the Parse function to read in a JSON configuration file and unmarshal the data into a struct:

type Foo

type Config struct {
	IntValue    int    `json:"intValue"`
	StringValue string `json:"stringValue"`
	BoolValue   bool   `json:"boolValue"`
	SliceValue  []int  `json:"sliceValue"`
	Nested struct {
		Bar string `json:"bar"`
	} `json:"nested"`
}

var config Config

defaults := map[string]interface{}{
	"sliceValue": []int{1, 2, 3},
}

os.Setenv("NESTED_BAR", "test")

err := configparser.Parse(configparser.ConfigFileTypeJSON, "config.json", &config, defaults, "")
if err != nil {
	// handle error
}

// use config values
fmt.Println(config.IntValue)
fmt.Println(config.StringValue)
fmt.Println(config.BoolValue)
fmt.Println(config.SliceValue)
fmt.Println(config.Nested.Bar)

Errors

The package defines several error constants that may be returned by the Parse function:

  • ErrTargetNotPointer: returned if the provided target value is not a pointer.
  • ErrFileDoesNotExist: returned if the provided file path does not exist.
  • ErrInvalidConfigFileType: returned if the provided configFileType constant is not a valid type.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTargetNotPointer is returned if the provided target value is not a pointer.
	ErrTargetNotPointer = errors.New("target is not pointer")
	// ErrFileDoesNotExist is returned if the provided file path does not exist.
	ErrFileDoesNotExist = errors.New("file does not exist")
	// ErrInvalidConfigFileType is returned if the provided configFileType constant is not a valid type.
	ErrInvalidConfigFileType = errors.New("invalid config file type")
)

Functions

func Parse

func Parse(configFileType ConfigFileType, file string, target interface{},
	defaults map[string]interface{}, envPrefix string) error

Parse reads in a configuration file and unmarshals the data into the provided target value. The configFileType argument specifies the type of the configuration file (JSON or YAML). The file argument is the file path of the configuration file. The target argument should be a pointer to the value that the configuration data will be unmarshaled into. The defaults argument is a map of default values that will be set in the configuration if they are not present in the configuration file. The envPrefix argument is a string specifying the environment variables prefix to use when reading env vars (eg. "myPrefix" results in "MYPREFIX_MYPROP") If there is an error reading in the configuration file or unmarshaling the data, an error is returned.

Types

type ConfigFileType

type ConfigFileType uint8

ConfigFileType is a type for constants that represent the type of a configuration file.

const (
	// ConfigFileTypeJSON represents a JSON configuration file.
	ConfigFileTypeJSON ConfigFileType = iota
	// ConfigFileTypeYAML represents a YAML configuration file.
	ConfigFileTypeYAML
)

Jump to

Keyboard shortcuts

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