float.go 932 B

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