sanitizedParameters.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package govaluate
  2. // sanitizedParameters is a wrapper for Parameters that does sanitization as
  3. // parameters are accessed.
  4. type sanitizedParameters struct {
  5. orig Parameters
  6. }
  7. func (p sanitizedParameters) Get(key string) (interface{}, error) {
  8. value, err := p.orig.Get(key)
  9. if err != nil {
  10. return nil, err
  11. }
  12. // should be converted to fixed point?
  13. if isFixedPoint(value) {
  14. return castFixedPoint(value), nil
  15. }
  16. return value, nil
  17. }
  18. func isFixedPoint(value interface{}) bool {
  19. switch value.(type) {
  20. case uint8:
  21. return true
  22. case uint16:
  23. return true
  24. case uint32:
  25. return true
  26. case uint64:
  27. return true
  28. case int8:
  29. return true
  30. case int16:
  31. return true
  32. case int32:
  33. return true
  34. case int64:
  35. return true
  36. case int:
  37. return true
  38. }
  39. return false
  40. }
  41. func castFixedPoint(value interface{}) float64 {
  42. switch value.(type) {
  43. case uint8:
  44. return float64(value.(uint8))
  45. case uint16:
  46. return float64(value.(uint16))
  47. case uint32:
  48. return float64(value.(uint32))
  49. case uint64:
  50. return float64(value.(uint64))
  51. case int8:
  52. return float64(value.(int8))
  53. case int16:
  54. return float64(value.(int16))
  55. case int32:
  56. return float64(value.(int32))
  57. case int64:
  58. return float64(value.(int64))
  59. case int:
  60. return float64(value.(int))
  61. }
  62. return 0.0
  63. }