helper.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package paladin
  2. import "time"
  3. // Bool return bool value.
  4. func Bool(v *Value, def bool) bool {
  5. b, err := v.Bool()
  6. if err != nil {
  7. return def
  8. }
  9. return b
  10. }
  11. // Int return int value.
  12. func Int(v *Value, def int) int {
  13. i, err := v.Int()
  14. if err != nil {
  15. return def
  16. }
  17. return i
  18. }
  19. // Int32 return int32 value.
  20. func Int32(v *Value, def int32) int32 {
  21. i, err := v.Int32()
  22. if err != nil {
  23. return def
  24. }
  25. return i
  26. }
  27. // Int64 return int64 value.
  28. func Int64(v *Value, def int64) int64 {
  29. i, err := v.Int64()
  30. if err != nil {
  31. return def
  32. }
  33. return i
  34. }
  35. // Float32 return float32 value.
  36. func Float32(v *Value, def float32) float32 {
  37. f, err := v.Float32()
  38. if err != nil {
  39. return def
  40. }
  41. return f
  42. }
  43. // Float64 return float32 value.
  44. func Float64(v *Value, def float64) float64 {
  45. f, err := v.Float64()
  46. if err != nil {
  47. return def
  48. }
  49. return f
  50. }
  51. // String return string value.
  52. func String(v *Value, def string) string {
  53. s, err := v.String()
  54. if err != nil {
  55. return def
  56. }
  57. return s
  58. }
  59. // Duration parses a duration string. A duration string is a possibly signed sequence of decimal numbers
  60. // each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  61. func Duration(v *Value, def time.Duration) time.Duration {
  62. dur, err := v.Duration()
  63. if err != nil {
  64. return def
  65. }
  66. return dur
  67. }