...
1
16
17 package value
18
19 import (
20 "sort"
21 "strings"
22 )
23
24
25 type Field struct {
26 Name string
27 Value Value
28 }
29
30
31
32 type FieldList []Field
33
34
35 func (f FieldList) Sort() {
36 if len(f) < 2 {
37 return
38 }
39 if len(f) == 2 {
40 if f[1].Name < f[0].Name {
41 f[0], f[1] = f[1], f[0]
42 }
43 return
44 }
45 sort.SliceStable(f, func(i, j int) bool {
46 return f[i].Name < f[j].Name
47 })
48 }
49
50
51 func (f FieldList) Less(rhs FieldList) bool {
52 return f.Compare(rhs) == -1
53 }
54
55
56
57 func (f FieldList) Compare(rhs FieldList) int {
58 i := 0
59 for {
60 if i >= len(f) && i >= len(rhs) {
61
62 return 0
63 }
64 if i >= len(f) {
65
66 return -1
67 }
68 if i >= len(rhs) {
69
70 return 1
71 }
72 if c := strings.Compare(f[i].Name, rhs[i].Name); c != 0 {
73 return c
74 }
75 if c := Compare(f[i].Value, rhs[i].Value); c != 0 {
76 return c
77 }
78
79 i++
80 }
81 }
82
83
84 func (f FieldList) Equals(rhs FieldList) bool {
85 if len(f) != len(rhs) {
86 return false
87 }
88 for i := range f {
89 if f[i].Name != rhs[i].Name {
90 return false
91 }
92 if !Equals(f[i].Value, rhs[i].Value) {
93 return false
94 }
95 }
96 return true
97 }
98
View as plain text