config

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 11 Imported by: 4

README

An Extensible Config Package, supporting the usage idioms of Spring Boot, dotnet and Node "config" in Go

Language Badge
release notes

Introduction

An extensible config package, supporting the usage elements of Spring Boot and Node "config", including:

  • json, yaml and Java property files,
  • cascading value over-rides using, GO_ENV, GO_APP_INSTANCE and GO_PROFILES_ACTIVE
  • placeholder resolution (or variable expansion),
  • encrypted values (via github.com/alt-golang/gosypt),
  • url fetching
  • environment variables (via config.get("env.MY_VAR"),
  • command line parameters (via config.get("args.MY_ARG")
  • and default (or fallback) values,

Usage

To use the module, import the module as so:

import  ( "github.com/alt-golang/config") ;

config.Get('key');
config.Get('nested.key');
config.GetAs('nested.key',&SomeStruct);
config.GetWithDefault('unknown','use this instead'); // this does not throw an error
File Loading and Precedence

The module follows the file loading and precedence rules of the popular Node config defaults, with additional rules in the style of Spring Boot.

By default, files are loaded from the working directory, or from the config folder if a default., production., or local-development.** file is detected (after the Node "config" idiom). The NODE_CONFIG_DIR variable can be specified to change the default.

Files are over-ridden from the indicated folder in the following order:

  • config.( json | yml | yaml | props | properties )
  • default.( json | yml | yaml | props | properties )
  • application.( json | yml | yaml | props | properties )
  • appsettings.( json | yml | yaml | props | properties )
  • {GO_ENV}.( json | yml | yaml | props | properties )
  • appsettings.{GO_ENV}.( json | yml | yaml | props | properties )
  • {GO_ENV}-{GO_APP_INSTANCE}.( json | yml | yaml | props | properties )
  • appsettings.{GO_ENV}.{GO_APP_INSTANCE}.( json | yml | yaml | props | properties )
  • application-{GO_PROFILES_ACTIVE[0]}.( json | yml | yaml | props | properties )
  • application-{GO_PROFILES_ACTIVE[1]}.( json | yml | yaml | props | properties )
  • environment variables (over-ridden into env)
  • commandline arguments (over-ridden into args)

Environment variables and command line arguments, will over-ride values found in files, for example env.MY_VAR=someValue in a application.properties file, or

local-development.yaml

env:
  MY_VAR: someValue

will be over-ridden only if it exists on the host system, negating the need for setting local development environment variables or arguments.

Placeholders, encrypted values, and remote config

Config values that include the common ${placeholder} syntax, will resolve the inline placeholders, so the config.get('placeholder')' path below will return start.one.two.end.

Config values that start with the prefix enc. will be decrypted with the gosypt package port, with the AES 16,32,64 byte passphrase being sourced from the GO_CONFIG_PASSPHRASE environment variable.

Config values that start with the prefix url. are be fetched and resolved , and HTTP options can be specified as in the example config file.

config.yaml


  "key": "value"
  "one" : "one"
  "placeholder": "start.${one}.${nested.two}.end"
  "placeholderEncrypted": "start.${nested.encrypted}.end"
  "nested" : 
    "key" : "value"
    "two" : "two"
    "placeholder": "start.${one}.${nested.two}.end"
    "encrypted" : "enc.pxQ6z9s/LRpGB+4ddJ8bsq8RqELmhVU2"
    "encryptedWithSecret" : "enc./emLGkD3cbfqoSPijGZ0jh1p1SYIHQeJ"
  "jsonplaceholder": 
    "todos": "url.https://jsonplaceholder.typicode.com/todos/1"
  "fetchWithOpts" : 
    "url": "url.https://jsonplaceholder.typicode.com/todos/1"
    "method": "get",
    "body": "{}"
    "headers":
      "Authorization": "Basic dXNlcjpwYXNz"
      "Content-Type": "application/json"

License

May be freely distributed under the MIT license.

Copyright (c) 2022 Craig Parravicini

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.0.6"

Functions

func AssignAs added in v1.0.6

func AssignAs(config map[string]interface{}, object interface{})

func Get

func Get(path string) (interface{}, error)

func GetAs added in v1.0.6

func GetAs(path string, object any) error

func GetWithDefault

func GetWithDefault(path string, defaultValue interface{}) (interface{}, error)

func Has

func Has(path string) bool

func ResolverMapValuesDeep

func ResolverMapValuesDeep(values interface{}, path string, callback ResolverCallBack) interface{}

Types

type Config

type Config interface {
	Has(path string) bool
	Get(path string) (interface{}, error)
	GetAs(path string, object any) (any, error)
	GetWithDefault(path string, defaultValue interface{}) (interface{}, error)
}

func Factory added in v1.0.1

func Factory(config map[string]interface{}) Config

func GetConfig

func GetConfig() Config

func GetConfigFromDir added in v1.0.1

func GetConfigFromDir(dir string) Config

func GetServiceConfigFromDir added in v1.0.4

func GetServiceConfigFromDir(dir string) Config

func ServiceFactory added in v1.0.4

func ServiceFactory(config map[string]interface{}) Config

type DefaultConfig

type DefaultConfig struct {
	Config
	// contains filtered or unexported fields
}

func (DefaultConfig) Get

func (config DefaultConfig) Get(path string) (interface{}, error)

func (DefaultConfig) GetWithDefault

func (config DefaultConfig) GetWithDefault(path string, defaultValue interface{}) (interface{}, error)

func (DefaultConfig) Has

func (config DefaultConfig) Has(path string) bool

type DefaultResolver

type DefaultResolver struct {
	Resolver
}

func (DefaultResolver) Callback

func (defaultResolver DefaultResolver) Callback(value interface{}, path string) interface{}

func (DefaultResolver) Resolve

func (defaultResolver DefaultResolver) Resolve(object interface{}, path string) (interface{}, error)

type DefaultSelector

type DefaultSelector struct {
	Selector
	// contains filtered or unexported fields
}

func (DefaultSelector) Matches

func (defaultSelector DefaultSelector) Matches(value string) bool

func (DefaultSelector) ResolveValue

func (defaultSelector DefaultSelector) ResolveValue(value string) string

type DelegatingConfig

type DelegatingConfig struct {
	Config
	// contains filtered or unexported fields
}

func (DelegatingConfig) Get

func (config DelegatingConfig) Get(path string) (interface{}, error)

func (DelegatingConfig) GetWithDefault

func (config DelegatingConfig) GetWithDefault(path string, defaultValue interface{}) (interface{}, error)

func (DelegatingConfig) Has

func (config DelegatingConfig) Has(path string) bool

type DelegatingResolver

type DelegatingResolver struct {
	Resolver
	// contains filtered or unexported fields
}

func (DelegatingResolver) Resolve

func (delegatingResolver DelegatingResolver) Resolve(object interface{}, path string) (interface{}, error)

type GosyptDecryptor

type GosyptDecryptor struct {
	Resolver
	// contains filtered or unexported fields
}

func (GosyptDecryptor) Callback

func (gosyptDecryptor GosyptDecryptor) Callback(value interface{}, path string) interface{}

func (GosyptDecryptor) Resolve

func (gosyptDecryptor GosyptDecryptor) Resolve(object interface{}, path string) (interface{}, error)

type PlaceHolderResolver

type PlaceHolderResolver struct {
	Resolver
	// contains filtered or unexported fields
}

func (PlaceHolderResolver) Callback

func (placeHolderResolver PlaceHolderResolver) Callback(value interface{}, path string) interface{}

func (PlaceHolderResolver) Resolve

func (placeHolderResolver PlaceHolderResolver) Resolve(object interface{}, path string) (interface{}, error)

type PlaceHolderSelector

type PlaceHolderSelector struct {
	Selector
}

func (PlaceHolderSelector) Matches

func (placeHolderSelector PlaceHolderSelector) Matches(value string) bool

func (PlaceHolderSelector) ResolveValue

func (placeHolderSelector PlaceHolderSelector) ResolveValue(value string) string

type PrefixSelector

type PrefixSelector struct {
	Selector
	// contains filtered or unexported fields
}

func (PrefixSelector) Matches

func (prefixSelector PrefixSelector) Matches(value string) bool

func (PrefixSelector) ResolveValue

func (prefixSelector PrefixSelector) ResolveValue(value string) string

type Resolver

type Resolver interface {
	Resolve(object interface{}, path string) (interface{}, error)
	CallBack(interface{}, string) interface{}
}

type ResolverCallBack

type ResolverCallBack func(interface{}, string) interface{}

type Selector

type Selector interface {
	Matches(value string) bool
	ResolveValue(value string) string
}

type URLResolver added in v1.0.2

type URLResolver struct {
	Resolver
	// contains filtered or unexported fields
}

func (URLResolver) Callback added in v1.0.2

func (URLResolver URLResolver) Callback(value interface{}, path string) interface{}

func (URLResolver) FetchUrlResponseData added in v1.0.2

func (URLResolver URLResolver) FetchUrlResponseData(url string, method string, body string, headers map[string]interface{}) ([]byte, error)

func (URLResolver) Resolve added in v1.0.2

func (URLResolver URLResolver) Resolve(object interface{}, path string) (interface{}, error)

type ValueResolvingConfig

type ValueResolvingConfig struct {
	Config
	// contains filtered or unexported fields
}

func (ValueResolvingConfig) Get

func (valueResolvingConfig ValueResolvingConfig) Get(path string) (interface{}, error)

func (ValueResolvingConfig) GetWithDefault

func (valueResolvingConfig ValueResolvingConfig) GetWithDefault(path string, defaultValue interface{}) (interface{}, error)

func (ValueResolvingConfig) Has

func (valueResolvingConfig ValueResolvingConfig) Has(path string) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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