config

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2025 License: MIT Imports: 10 Imported by: 0

README

Test Status

Configurator Library for GO

This library allow you to load configurations from flags, environment, json file and much more through custom interface implementation.

type ConfigData struct {
  Host string
  Port int
}

var configs ConfigData
_, err := config.Default(&configs,
  config.Add("host", "localhost", "Server hostname"),
  config.Add("port", 3000, "Server port"),
)

if err != nil {
  log.Fatal(err)
}

println("Server Host: ", configs.Host)
println("Server Port: ", configs.Port)

Documentation

Index

Constants

View Source
const ReaderEnv = "env"
View Source
const ReaderFlag = "flag"
View Source
const ReaderJSON = "json"

Variables

Full list of default configuration readers available

Functions

func AnyToBool

func AnyToBool(val any) (bool, bool)

typecast from any to bool

func AnyToInt

func AnyToInt(val any) (int64, bool)

typecast from any to int

func CastSliceValue

func CastSliceValue(value any, tp reflect.Kind) ([]any, bool)

cast value to slice of type

func CastValue

func CastValue(value any, tp reflect.Kind) (any, bool)

cast value to type if needed

func FlattenMap

func FlattenMap(data map[string]any, prefix string) map[string]any

flatten nest map data to dot separated key data

func ResolveBytes

func ResolveBytes(r Reader, opts *Options, path string, reader io.Reader) ([]byte, error)

resolve byte data from file or reader

func ResolvePath

func ResolvePath(r Reader, opts *Options, path string) string

resolve file path for config file

func ToSnakeCase

func ToSnakeCase(s string) string

convert string to snake case

Types

type BaseReader

type BaseReader struct{}

base reader to extend with (optional)

func (BaseReader) ConfigExtension

func (r BaseReader) ConfigExtension() string

file extension for related reader

func (BaseReader) Parse

func (r BaseReader) Parse(_ *Options, _ []*Field) error

placeholder parser func since not every reader needs to parse

type Config

type Config struct {
	Options *Options
	Values  map[string]any
	// contains filtered or unexported fields
}

Configuration management struct

func Default

func Default(data any, cbs ...WithConfigCallback) (*Config, error)

Default creates instance of configurations using default settings and option to add configs

func DefaultWithOpts

func DefaultWithOpts(data any, ocbs []WithOptionCallback, cbs ...WithConfigCallback) (*Config, error)

DefaultWithOpts creates instance of configurations using default settings with support to add config and options

func New

func New(opts *Options, cbs ...WithConfigCallback) (*Config, error)

Create new instance of configurations with support for setup callbacks

func (*Config) Add

func (c *Config) Add(field *Field)

Add new field for config read

func (*Config) AddReader

func (c *Config) AddReader(reader Reader)

Add new configuration reader instance

func (*Config) Get

func (c *Config) Get(name string) Reader

Read the stored configuration reader

func (*Config) Parse

func (c *Config) Parse(data any) error

Parse the configurations based of registered fields

type EnvReader

type EnvReader struct {
	BaseReader
}

Configuration Reader for Environment Variables

func NewEnvReader

func NewEnvReader() *EnvReader

func (EnvReader) Configurator

func (r EnvReader) Configurator() string

func (EnvReader) Get

func (r EnvReader) Get(opts *Options, field *Field) (any, bool)

type Field

type Field struct {
	Name  string
	Usage string
	Value any
	Kind  reflect.Kind
	Alias []string
	List  bool
	Null  bool
}

Field definition struct for loading config

func NewField

func NewField[T any](name string, value T, usage string) (*Field, error)

Create new instance of field and resolve configs

func (*Field) AddAlias

func (f *Field) AddAlias(a string) *Field

Add field alias (e.g. env keys)

func (*Field) ValueAsBool

func (f *Field) ValueAsBool() bool

Convert value to bool

func (*Field) ValueAsInt

func (f *Field) ValueAsInt() int

Convert value to int

func (*Field) ValueAsString

func (f *Field) ValueAsString() string

Convert value to string

type FlagReader

type FlagReader struct {
	BaseReader
	// contains filtered or unexported fields
}

Configuration Reader for Flags(Commandline) inputs

func NewFlagReader

func NewFlagReader() *FlagReader

func (*FlagReader) Configurator

func (r *FlagReader) Configurator() string

func (*FlagReader) Get

func (r *FlagReader) Get(_ *Options, field *Field) (any, bool)

func (*FlagReader) Parse

func (r *FlagReader) Parse(_ *Options, fields []*Field) error

type JSONReader

type JSONReader struct {
	BaseReader
	// contains filtered or unexported fields
}

Configuration Reader for JSON Data/File

func NewJSONReader

func NewJSONReader() *JSONReader

Create JSON configuration reader using file

func NewJSONReaderIO

func NewJSONReaderIO(reader io.Reader) *JSONReader

Create JSON configuration reader using io.Reader

func (*JSONReader) ConfigExtension

func (r *JSONReader) ConfigExtension() string

func (*JSONReader) Configurator

func (r *JSONReader) Configurator() string

func (*JSONReader) Get

func (r *JSONReader) Get(_ *Options, field *Field) (any, bool)

func (*JSONReader) Parse

func (r *JSONReader) Parse(opts *Options, _ []*Field) error

type Options

type Options struct {
	Strict   bool
	Prefix   string
	Filename string
	Priority []string
}

Options struct for configurator

func NewOptions

func NewOptions(cbs ...WithOptionCallback) *Options

Make new options instance using callbacks

type Reader

type Reader interface {
	Configurator() string
	ConfigExtension() string
	Parse(opts *Options, fields []*Field) error
	Get(opts *Options, field *Field) (any, bool)
}

configuration reader interface

type WithConfigCallback

type WithConfigCallback func(*Config) error

Callback type for configuration setup

func Add

func Add(name string, value any, usage string) WithConfigCallback

Callback shortcut to register new field

func Register

func Register(reader Reader) WithConfigCallback

Callback to register new reader instance

type WithOptionCallback

type WithOptionCallback func(*Options)

Callback type for options setup

func UseStrict

func UseStrict(s bool) WithOptionCallback

Set readers to strict mode (experimental)

func WithFilename

func WithFilename(name string) WithOptionCallback

Set filename for config file

func WithPrefix

func WithPrefix(prefix string) WithOptionCallback

Set prefix for keys

func WithPriority

func WithPriority(priorities ...string) WithOptionCallback

Set priority of config readers

Jump to

Keyboard shortcuts

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