int.go 938 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package muesli
  2. import "fmt"
  3. // IntValue are Muesli integer values
  4. type IntValue int64
  5. // IntType is the type of IntValue
  6. const IntType = TypeValue("Int")
  7. func (val IntValue) String() string {
  8. return fmt.Sprintf("%d", int64(val))
  9. }
  10. func (v IntValue) Type() TypeValue { return IntType }
  11. func (from IntValue) Convert(to interface{}) error {
  12. switch toPtr := to.(type) {
  13. case *string:
  14. (*toPtr) = from.String()
  15. case *int8:
  16. (*toPtr) = int8(from)
  17. case *int16:
  18. (*toPtr) = int16(from)
  19. case *int32:
  20. (*toPtr) = int32(from)
  21. case *int64:
  22. (*toPtr) = int64(from)
  23. case *int:
  24. (*toPtr) = int(from)
  25. case *bool:
  26. (*toPtr) = (from != 0)
  27. case *float32:
  28. (*toPtr) = float32(from)
  29. case *float64:
  30. (*toPtr) = float64(from)
  31. case *IntValue:
  32. (*toPtr) = from
  33. case *Value:
  34. (*toPtr) = from
  35. default:
  36. return NewErrorValuef("Cannot convert IntValue value %v to %v", from, to)
  37. }
  38. return nil
  39. }