Documentation
¶
Overview ¶
Package unstruct is a library to read external values and decode them into the structs.
Example ¶
package main
import (
"fmt"
"os"
"github.com/aereal/unstruct"
)
func main() {
decoder := unstruct.NewDecoder(unstruct.NewEnvironmentSource())
var val struct {
ExampleString string
ExampleInt int
ExampleBool bool
}
os.Setenv("EXAMPLE_STRING", "str")
os.Setenv("EXAMPLE_INT", "42")
os.Setenv("EXAMPLE_BOOL", "true")
if err := decoder.Decode(&val); err != nil {
panic(err)
}
fmt.Printf("%#v", val)
}
Output: struct { ExampleString string; ExampleInt int; ExampleBool bool }{ExampleString:"str", ExampleInt:42, ExampleBool:true}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidTarget = errors.New("target must be a pointer to the struct") ErrValueNotFound = errors.New("no value found") )
Functions ¶
This section is empty.
Types ¶
type DecodeFieldError ¶
type DecodeFieldError struct {
// Field is a struct field that error occurs.
Field reflect.StructField
// Err is an error that occurs.
Err error
}
DecodeFieldError is an error type represents the decode failure on the field.
func (*DecodeFieldError) Error ¶
func (e *DecodeFieldError) Error() string
func (*DecodeFieldError) Is ¶
func (e *DecodeFieldError) Is(other error) bool
func (*DecodeFieldError) Unwrap ¶
func (e *DecodeFieldError) Unwrap() error
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder runs data sources against given target value and completes target value.
func NewDecoder ¶
type EnvironmentSource ¶
type EnvironmentSource struct {
// contains filtered or unexported fields
}
EnvironmentSource is a Source that reads values from environment variables.
func NewEnvironmentSource ¶
func NewEnvironmentSource(opts ...EnvironmentSourceOption) *EnvironmentSource
type EnvironmentSourceOption ¶
type EnvironmentSourceOption func(*environmentSourceConfig)
func WithPrefix ¶
func WithPrefix(prefix string) EnvironmentSourceOption
WithPrefix indicates the EnvironmentSource fetches environment variables with given prefix.
func WithSliceDelimiter ¶
func WithSliceDelimiter(delimiter string) EnvironmentSourceOption
type MapSource ¶ added in v0.3.0
type MapSource struct {
// contains filtered or unexported fields
}
MapSource is a Source that fills values from given the map.
func NewMapSource ¶ added in v0.3.0
type Path ¶
type Path []reflect.StructField
type Source ¶
Source is an interface that fetches the values from external sources and fills into given target.
type UnsupportedTypeError ¶
type UnsupportedTypeError = internal.UnsupportedTypeError
UnsupportedTypeError is an error type represents target value type is not supported by Source.