sfconfig

package module
v0.0.0-...-37b5f89 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 12 Imported by: 0

README

sfconfig

Load configuration from multiple sources in Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldSet

func FieldSet(field *Field, v string) error

FieldSet sets field value from the given string value. It converts the string value in a sane way and is useful or environment variables or flags which are by nature in string types.

func StructName

func StructName(s interface{}) string

func ToSnakeCase

func ToSnakeCase(str string) string

Types

type DefaultTagLoader

type DefaultTagLoader struct {
	// DefaultTagName is the default tag name for struct fields to define
	// default values for a field.
	DefaultTagName string
}

func (*DefaultTagLoader) Load

func (t *DefaultTagLoader) Load(s interface{}) error

type EnvironmentLoader

type EnvironmentLoader struct {
	// Prefix prepends given string to every environment variable
	// FIELDNAME will be {PREFIX}_FIELDNAME
	Prefix string

	// CamelCase adds a separator for field names in camelcase form. A
	// fieldname of "AccessKey" would generate an environment name of
	// "{PREFIX}_ACCESSKEY". If CamelCase is enabled, the environment name
	// will be generated in the form of "{PREFIX}_ACCESS_KEY"
	CamelCase bool

	EnvTagName string
}

func (*EnvironmentLoader) Load

func (e *EnvironmentLoader) Load(s interface{}) error

Load loads the source into the config defined by struct s

type Field

type Field struct {
	// contains filtered or unexported fields
}

func StructFields

func StructFields(s interface{}) []*Field

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields. This is particular handy to get the fields of a nested struct. It panics if field is not exported or if field's kind is not struct

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is not initialized (has a zero value). It panics if the field is not exported.

func (*Field) Kind

func (f *Field) Kind() reflect.Kind

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

func (f *Field) Tag(key string) string

Tag returns the value associated with key in the tag string. If there is no such key in the tag, Tag returns the empty string.

func (*Field) TagLookup

func (f *Field) TagLookup(key string) (string, bool)

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the underlying value of the field. It panics if the field is not exported.

type JSONLoader

type JSONLoader struct {
	ConfigFile string
	Reader     io.Reader
}

func (*JSONLoader) Load

func (j *JSONLoader) Load(s interface{}) error

Load loads the source into the config defined by struct s. Defaults to using the Reader if provided, otherwise tries to read from the file

type Loader

type Loader interface {
	// Load loads the source into the config defined by struct s
	Load(s interface{}) error
}

type RequiredValidator

type RequiredValidator struct {
	//  TagName holds the validator tag name. The default is "required"
	TagName string

	// TagValue holds the expected value of the validator. The default is "true"
	TagValue string
}

func (*RequiredValidator) Validate

func (e *RequiredValidator) Validate(s interface{}) error

Validate validates the given struct against field's zero values. If intentionally, the value of a field is `zero-valued`(e.g false, 0, "") required tag should not be set for that field.

type SFConfig

type SFConfig struct {
	// contains filtered or unexported fields
}

func New

func New() *SFConfig

func NewDefaultConfig

func NewDefaultConfig(configFile string, envPrefix string) *SFConfig

func (*SFConfig) AddLoader

func (c *SFConfig) AddLoader(l Loader)

func (*SFConfig) AddValidator

func (c *SFConfig) AddValidator(v Validator)

func (*SFConfig) Load

func (c *SFConfig) Load(conf interface{})

func (*SFConfig) MustLoad

func (c *SFConfig) MustLoad(conf interface{})

func (*SFConfig) MustValidate

func (c *SFConfig) MustValidate(conf interface{})

type Validator

type Validator interface {
	// Validate validates the config struct
	Validate(s interface{}) error
}

type YAMLLoader

type YAMLLoader struct {
	ConfigFile string
	Reader     io.Reader
}

YAMLLoader satisfies the loader interface. It loads the configuration from the given yaml file.

func (*YAMLLoader) Load

func (y *YAMLLoader) Load(s interface{}) error

Load loads the source into the config defined by struct s. Defaults to using the Reader if provided, otherwise tries to read from the file

Jump to

Keyboard shortcuts

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