This library has two major goals:
- Provide data types to be used during incoming and outgoing data mapping for optional values
- Provide safe and sufficient mapping functions to work with optional values
Simple declare optional.
data type in your struct
type Configuration struct {
Hostname optional.String `json:"hostname"`
Port optional.Int `json:"port"`
}
func main() {
bts, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
var c Configuration
if err := json.Unmarshal(bts, &v); err != nil {
panic(err)
}
host := c.Hostname.OrElse("localhost")
port := c.Port.OrElse(3306)
}
- JSON
- SQL
- YAML compatible with https://github.com/go-yaml/yaml
- TOML decoding only, compatible with https://github.com/BurntSushi/toml
optional.Bool
- for booleansoptional.Int
- for integersoptional.String
- for stringsoptional.Float64
- for floatsoptional.Time
- fortime.Time
, mapping usingoptional.TimeUnixSeconds
optional.Duration
- fortime.Duration
, mapping usingoptional.DurationSeconds
,optional.DurationMillis
oroptional.DurationMinutes
Get
- returns value from optional, but panics if optional is emptyOrElse(els)
- returns value from optional or providedels
value if optional is emptyIsPresent
- returnstrue
if optional contains vaue,false
otherwiseIfPresent(func)
- invokes providedfunc
over value from optional if optional not emptyFilter(predicate)
- applies predicate over optional valueMapToString(func)
,MapToInt(func)
,MapToFloat(func)
,MapToBool(func)
- maps optional value to other type usingfunc