interface.go 627 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package optional
  2. // Optional represents a generic optional type, stored as an interface{}.
  3. type Interface struct {
  4. isSet bool
  5. value interface{}
  6. }
  7. func NewInterface(value interface{}) Interface {
  8. return Interface{
  9. true,
  10. value,
  11. }
  12. }
  13. // EmptyInterface returns a new Interface that does not have a value set.
  14. func EmptyInterface() Interface {
  15. return Interface{
  16. false,
  17. nil,
  18. }
  19. }
  20. func (b Interface) IsSet() bool {
  21. return b.isSet
  22. }
  23. func (b Interface) Value() interface{} {
  24. return b.value
  25. }
  26. func (b Interface) Default(defaultValue interface{}) interface{} {
  27. if b.isSet {
  28. return b.value
  29. }
  30. return defaultValue
  31. }