confobject

package module
v0.0.0-...-c3e33be Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 9 Imported by: 0

README

ConfObject

Reloadable, validated configuration from many - sometimes uncontrollable - sources

Build Status GoDoc

Motivation

I had to deal with configuration values that came from a lot of different places and circumstances:

  • a config file or env vars
  • some json or yaml file - possibly stored in a db
  • some of these i had control over - others could be modified by a user in an app that i do not necessarily must have access to to employ validation or control the way things are stored

I also needed:

  • default values, "NOT NULL" like constraints, and validation that involves dependencies between different config values
  • the ability to reload the configs from all its sources in its original order in an easy way

Features

Usage

Cfg := struct {
	Config

	BoolSetting   bool    `default:"true"`
	StringSetting string  `default:"foo" json:"string_setting"`
	IntSetting    int     `required:"false" default:"23" should:"BeGreaterThanOrEqualTo_FloatSetting"`
	FloatSetting  float64 `default:"1.681"`

	SingleValueConfig struct {
		StringSetting string  `default:"bar" json:"single_value_config__string_setting"`
		IntSetting    int     `required:"false" default:"42" should:"BeGreaterThanOrEqualTo_.FloatSetting"`
		FloatSetting  float64 `should:"BeLessThanOrEqualTo_IntSetting"`
	}

	SliceConfig struct {
		StringSliceSetting []string  `default:"foo,bar"`
		IntSliceSetting    []int     `default:"23,42"`
		FloatSliceSetting  []float64 `default:"1.394,1.112"`
	}

	NestedValueConfig struct {
		StringSetting          string `default:"NestedOuterFoo"`
		InnerNestedValueConfig struct {
			StringSetting string `default:"NestedInnerFoo"`
		}
	}

	StringSet *set.StringSet `alias:someNameICantControl`
	IntSet    *set.IntSet
}{}

err = cobj.InitConfig(&Cfg, []cobj.InitFunc{
	cobj.InitFunc{
		F:           loadConfFile,
		ExitOnError: true,
	},
	cobj.InitFunc{
		F:           setupThisOtherThing,
		ExitOnError: false,
	},
}...)


func loadConfFile() (err error) {
	conf := make(map[string]string)

    ...

	err = Cfg.Set(conf)
	return
}

Limitations

TODOs

  • finish writing this file
  • godoc
  • clean up and extend the tests

Questions?

Ping me on twitter

Documentation

Index

Constants

View Source
const (
	ENV_PREFIX = "CONFOBJ_"

	TAG_DEFAULT         = "default"
	TAG_REQUIRED        = "required"
	TAG_ALIAS           = "alias"
	TAG_ASSERTION       = "should"
	TAG_ASSERTION_SEP   = " "
	TAG_ASSERTION_VALUE = ":"
	TAG_ASSERTION_FIELD = "_"
)

Variables

View Source
var (
	TestMode bool
)

Functions

func Assert

func Assert(actual interface{}, assert Assertion, expected ...interface{}) (bool, string)

func InitConfig

func InitConfig(c interface{}, initFuncs ...InitFunc) (err error)

func StructFields

func StructFields(iface interface{}) (names []string, types map[string]string, tags map[string]reflect.StructTag)

Types

type Assertion

type Assertion func(actual interface{}, expectedList ...interface{}) string

type Config

type Config struct {
	MainConfig             reflect.Value
	ConfigValues           map[string]interface{}
	Initialized            bool
	ConfigKeys             *set.StringSet
	KeyAliases             *set.StringSet
	AliasKeyMap            map[string]string
	ConfigTypes            map[string]string
	ConfigTags             map[string]reflect.StructTag
	PanicOnAssignmentError bool
	Assertions             map[string]map[string][]Assertion

	LogSet bool
	// contains filtered or unexported fields
}

func (*Config) FieldForKey

func (c *Config) FieldForKey(key string) (field reflect.Value, err error)

func (*Config) ReInit

func (c *Config) ReInit() (err error)

func (*Config) Set

func (c *Config) Set(configData interface{}, prependKeys ...string) (err error)

func (Config) String

func (c Config) String() string

func (*Config) Validate

func (c *Config) Validate() (err error)

type InitFunc

type InitFunc struct {
	F           func() (err error)
	ExitOnError bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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