unstruct is a library to read external values and decode them into the structs.
unstruct reads values from external sources using Source
interface.
It is very simple interface, so you can easily implement custom Source
.
import (
"fmt"
"os"
"github.com/aereal/unstruct"
)
func main() {
decoder := unstruct.NewDecoder(unstruct.NewEnvironmentSource())
var val struct {
ExampleString string
ExampleInt int
ExampleBool bool
}
_ = decoder.Decode(&val)
}
See examples on pkg.go.dev.
go get github.com/aereal/unstruct
The practical application may have various and complicated configuration.
If you build the configuration from external data sources, you may face issues like below:
- fetch from remote data sources
- the application must read secret data that like credentials from remote data sources using secure channels.
- initialization
- complicated configuration may have many fields; you must initialize all of them.
- validation:
- you have to check the existence of all of the mandatory fields.
- type conversions
- the application configuration may include some values that differs from strings, such as numbers, booleans, or anything else.
- external data sources may support just limited scalar types, so you have to convert/parse external values into the application configuration's values.
unstruct works as simple decoder that decodes external data into Go's values like encoding/json.
See LICENSE file.