Documentation
¶
Index ¶
Constants ¶
const ( JSON = ".json" YML = ".yml" YAML = ".yaml" TOML = ".toml" ENV = ".env" )
Supported file extensions
Variables ¶
var ( // ErrUnsupportedType indicates unsupported struct field ErrUnsupportedType = errors.New("unsupported type") // ErrUnsupportedFileExt indicated unsupported file format // Only ".json", ".yml", ".yaml" and ".env" file types are supported ErrUnsupportedFileExt = errors.New("unsupported file extension") // ErrUnSettableField indicated unexported struct field ErrUnSettableField = errors.New("unSettable field") // ErrKeyNotFound is returned when no value found with specified key ErrKeyNotFound = errors.New("key not found") // ErrRequiredField indicates that Field is required but no value is provided ErrRequiredField = errors.New("field is required") // ErrParsing is returned in case of bad value ErrParsing = errors.New("failed parsing") // ErrValueOverflow indicates value overflow ErrValueOverflow = errors.New("value overflow") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Providers are applied at the order specified
// If multiple values are provided for a field, last one will get applied
Providers []Provider
// contains filtered or unexported fields
}
Config loads values from specified providers into given struct
func (*Config) AddProvider ¶
AddProvider adds a Provider to Providers list
type ConfigErrors ¶
type ConfigErrors []error
ConfigErrors is collection of errors during populating the input struct
func (ConfigErrors) Error ¶
func (ce ConfigErrors) Error() string
type ConfigTags ¶
type ConfigTags struct {
// Key to be used by providers to retrieve the needed value, defaults to field name.
// Use "-" to ignore the field.
Config string
// json tag for json files
Json string
// yaml tag for yaml files
Yaml string
// toml tag for toml files
Toml string
// Default value for field.
Default string
// Specify if value should be present, defaults to false.
Required bool
// Specify if field should be ignored, defaults to false.
Ignore bool
// Specify if value should be expanded from env, defaults to false.
Expand bool
// Separator to be used for slice/array items, defaults to " ".
Separator string
// Format to be used for parsing time strings, defaults to time.RFC3339.
Format string
}
ConfigTags indicates possible tags All of them are optional
type EnvProvider ¶
type EnvProvider struct {
// Prefix is used when finding values from environment variables, defaults to ""
Prefix string
// SnakeCase specifies whether to convert field names to snake_case or not, defaults to true
SnakeCase bool
// UpperCase specifies whether to convert field names to UPPERCASE or not, defaults to true
UpperCase bool
// FieldSeparator is used to separate field names, defaults to "_"
FieldSeparator string
// Source is used to retrieve environment variables
// It can be either a path to a file or empty string, if empty OS will be used
Source string
// Whether to report error if env file is not found, defaults to false
Required bool
}
EnvProvider loads values from environment variables to provided struct
func (*EnvProvider) Fill ¶
func (ep *EnvProvider) Fill(in *Input) error
Fill takes struct fields and fills their values
type Field ¶
type Field struct {
// Field value
Value reflect.Value
// Field tags
Tags *ConfigTags
// Slice of field names from root of struct all the way down to the field
Path []string
// IsSet specifies whether field value is set by one of the providers
IsSet bool
}
Field information
type FileProvider ¶
type FileProvider struct {
// Path to file
FilePath string
// File will be decoded based on extension
// .json, .yml(.yaml), .env and .toml file extensions are supported
FileExt string
// Whether to report error if file is not found, defaults to false
Required bool
}
FileProvider loads values from file to provided struct
func NewFileProvider ¶
func NewFileProvider(path string) *FileProvider
NewFileProvider creates a new FileProvider from specified path
func (*FileProvider) Fill ¶
func (fp *FileProvider) Fill(in *Input) error
Fill takes struct fields and and checks if their value is set
func (*FileProvider) UnmarshalStruct ¶
func (fp *FileProvider) UnmarshalStruct(i interface{}) error
UnmarshalStruct takes a struct pointer and loads values from provided file into it
type Input ¶
type Input struct {
// Struct name is used for error messages
Name string
// Fields information
Fields []*Field
}
Input stores information about given struct
type InvalidInputError ¶
An InvalidInputError describes an invalid argument passed to Into function The argument must be a non-nil struct pointer
func (*InvalidInputError) Error ¶
func (e *InvalidInputError) Error() string
type Provider ¶
type Provider interface {
Name() string
}
Provider is used to provide values It can implement either Unmarshaler or Filler interface or both Name method is used for error messages
type Unmarshaler ¶
type Unmarshaler interface {
UnmarshalStruct(i interface{}) (err error)
}
Unmarshaler can be implemented by providers to receive struct pointer and unmarshal values into it