You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.0 KiB

  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 dNull struct{}
  10. var (
  11. DNull Datum = dNull{}
  12. )
  13. func (d dNull) Compare(other Datum) (int, error) {
  14. if other == DNull {
  15. return 0, nil
  16. }
  17. return -1, nil
  18. }
  19. func NewDUint16(d DUint16) *DUint16 {
  20. return &d
  21. }
  22. func NewDUint32(d DUint32) *DUint32 {
  23. return &d
  24. }
  25. func (d *DUint16) Compare(other Datum) (int, error) {
  26. if other == DNull {
  27. return 1, nil
  28. }
  29. thisV := *d
  30. var otherV DUint16
  31. switch t := other.(type) {
  32. case *DUint16:
  33. otherV = *t
  34. default:
  35. return 0, fmt.Errorf("unsupported")
  36. }
  37. if thisV < otherV {
  38. return -1, nil
  39. }
  40. if thisV > otherV {
  41. return 1, nil
  42. }
  43. return 0, nil
  44. }
  45. func (d *DUint32) Compare(other Datum) (int, error) {
  46. if other == DNull {
  47. return 1, nil
  48. }
  49. thisV := *d
  50. var otherV DUint32
  51. switch t := other.(type) {
  52. case *DUint32:
  53. otherV = *t
  54. }
  55. if thisV < otherV {
  56. return -1, nil
  57. }
  58. if thisV > otherV {
  59. return 1, nil
  60. }
  61. return 0, nil
  62. }