datum.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package data
  2. import "fmt"
  3. type Datum interface {
  4. Compare(other Datum) (int, error)
  5. }
  6. type Datums []Datum
  7. type DUint16 uint16
  8. type DUint32 uint32
  9. type DUint64 uint64
  10. type dNull struct{}
  11. var (
  12. DNull Datum = dNull{}
  13. )
  14. func (d dNull) Compare(other Datum) (int, error) {
  15. if other == DNull {
  16. return 0, nil
  17. }
  18. return -1, nil
  19. }
  20. func NewDUint16(d DUint16) *DUint16 {
  21. return &d
  22. }
  23. func NewDUint32(d DUint32) *DUint32 {
  24. return &d
  25. }
  26. func NewDUint64(d DUint64) *DUint64 {
  27. return &d
  28. }
  29. func (d *DUint16) Compare(other Datum) (int, error) {
  30. if other == DNull {
  31. return 1, nil
  32. }
  33. thisV := *d
  34. var otherV DUint16
  35. switch t := other.(type) {
  36. case *DUint16:
  37. otherV = *t
  38. default:
  39. return 0, fmt.Errorf("unsupported")
  40. }
  41. if thisV < otherV {
  42. return -1, nil
  43. }
  44. if thisV > otherV {
  45. return 1, nil
  46. }
  47. return 0, nil
  48. }
  49. func (d *DUint32) Compare(other Datum) (int, error) {
  50. if other == DNull {
  51. return 1, nil
  52. }
  53. thisV := *d
  54. var otherV DUint32
  55. switch t := other.(type) {
  56. case *DUint32:
  57. otherV = *t
  58. }
  59. if thisV < otherV {
  60. return -1, nil
  61. }
  62. if thisV > otherV {
  63. return 1, nil
  64. }
  65. return 0, nil
  66. }
  67. func (d *DUint64) Compare(other Datum) (int, error) {
  68. if other == DNull {
  69. return 1, nil
  70. }
  71. thisV := *d
  72. var otherV DUint64
  73. switch t := other.(type) {
  74. case *DUint64:
  75. otherV = *t
  76. }
  77. if thisV < otherV {
  78. return -1, nil
  79. }
  80. if thisV > otherV {
  81. return 1, nil
  82. }
  83. return 0, nil
  84. }