You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
754 B

  1. package config
  2. import "strconv"
  3. // Flag represents a nullable boolean value that is considered null until
  4. // either parsed from YAML or merged in from another Flag value.
  5. //
  6. type Flag struct {
  7. True bool
  8. set bool
  9. }
  10. // UnmarshalJSON implements json.Unmarshaler to parse the underlying boolean
  11. // value and detect that the Flag should no longer be considered null.
  12. //
  13. func (flag *Flag) UnmarshalJSON(unmarshal []byte) error {
  14. var err error
  15. flag.True, err = strconv.ParseBool(string(unmarshal))
  16. if err != nil {
  17. return err
  18. }
  19. flag.set = true
  20. return nil
  21. }
  22. // Merge takes another flag and, if set, merged its boolean value into this
  23. // one.
  24. //
  25. func (flag *Flag) Merge(flag2 Flag) {
  26. if flag2.set {
  27. flag.True = flag2.True
  28. flag.set = true
  29. }
  30. }