Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created October 18, 2017 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/7e079294410c740e3008cb2de42f1cda to your computer and use it in GitHub Desktop.
Save fujiwara/7e079294410c740e3008cb2de42f1cda to your computer and use it in GitHub Desktop.
main.Config{Foo:main.MaybeBool{b:false, isSet:true}, Bar:main.MaybeBool{b:false, isSet:false}}
package main
import (
"errors"
"fmt"
yaml "gopkg.in/yaml.v2"
)
type MaybeBool struct {
b bool
isSet bool
}
func (b *MaybeBool) UnmarshalYAML(unmarshal func(interface{}) error) error {
var aux interface{}
if err := unmarshal(&aux); err != nil {
return err
}
if x, ok := aux.(bool); ok {
b.b = x
b.isSet = true
} else {
return errors.New("not a bool value")
}
return nil
}
type Config struct {
Foo MaybeBool `yaml:"foo"`
Bar MaybeBool `yaml:"bar"`
}
func main() {
var conf Config
yaml.Unmarshal([]byte("foo: false\n"), &conf)
fmt.Printf("%#v\n", conf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment