Created
October 18, 2017 07:12
-
-
Save fujiwara/7e079294410c740e3008cb2de42f1cda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main.Config{Foo:main.MaybeBool{b:false, isSet:true}, Bar:main.MaybeBool{b:false, isSet:false}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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