type.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package muesli
  2. // TypeValue is used to model the type of a value in the Muesli type system.
  3. // This is simply a string so this can be used as a hash key, however,
  4. // the name of a type must always begin with an upper case letter.
  5. type TypeValue string
  6. const (
  7. // TypeType is the type of TypeValue itself.
  8. TypeType = TypeValue("Type")
  9. // AnyType is not really a type, but a wildcard that matches any type
  10. AnyType = TypeValue("Any")
  11. // ZeroType in not really a type, but means that the type is missing,
  12. // and is conventiently the zero value for TypeValue
  13. ZeroType = TypeValue("")
  14. )
  15. func (val TypeValue) String() string {
  16. return string(val)
  17. }
  18. func (v TypeValue) Type() TypeValue { return TypeType }
  19. var _ Value = TypeValue("")
  20. func (from TypeValue) Convert(to interface{}) error {
  21. switch toPtr := to.(type) {
  22. case *string:
  23. (*toPtr) = from.String()
  24. case *TypeValue:
  25. (*toPtr) = from
  26. case *Value:
  27. (*toPtr) = from
  28. default:
  29. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  30. }
  31. return nil
  32. }