Skip to content

Commit 3b6dee9

Browse files
apelisseAntoine Pelisse
authored andcommitted
Re-organize the bigger value package
Added some comments to undocumented methods, and also split the now too big value.go file into smaller files.
1 parent 4a1a177 commit 3b6dee9

File tree

5 files changed

+425
-334
lines changed

5 files changed

+425
-334
lines changed

value/fields.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package value
18+
19+
import (
20+
"sort"
21+
"strings"
22+
)
23+
24+
// Field is an individual key-value pair.
25+
type Field struct {
26+
Name string
27+
Value Value
28+
}
29+
30+
// FieldList is a list of key-value pairs. Each field is expected to
31+
// have a different name.
32+
type FieldList []Field
33+
34+
// Sort sorts the field list by Name.
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+
// Less compares two lists lexically.
51+
func (f FieldList) Less(rhs FieldList) bool {
52+
return f.Compare(rhs) == -1
53+
}
54+
55+
// Less compares two lists lexically. The result will be 0 if f==rhs, -1
56+
// if f < rhs, and +1 if f > rhs.
57+
func (f FieldList) Compare(rhs FieldList) int {
58+
i := 0
59+
for {
60+
if i >= len(f) && i >= len(rhs) {
61+
// Maps are the same length and all items are equal.
62+
return 0
63+
}
64+
if i >= len(f) {
65+
// F is shorter.
66+
return -1
67+
}
68+
if i >= len(rhs) {
69+
// RHS is shorter.
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+
// The items are equal; continue.
79+
i++
80+
}
81+
}

value/list.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package value
18+
19+
func IsList(v Value) bool {
20+
if v == nil {
21+
return false
22+
}
23+
_, ok := v.([]interface{})
24+
return ok
25+
}
26+
27+
func ValueList(v Value) []interface{} {
28+
return v.([]interface{})
29+
}
30+
31+
// Equals compares two lists lexically.
32+
func ListEquals(lhs, rhs []interface{}) bool {
33+
if len(lhs) != len(rhs) {
34+
return false
35+
}
36+
37+
for i, lv := range lhs {
38+
if !Equals(lv, rhs[i]) {
39+
return false
40+
}
41+
}
42+
return true
43+
}
44+
45+
// Less compares two lists lexically.
46+
func ListLess(lhs, rhs []interface{}) bool {
47+
return ListCompare(lhs, rhs) == -1
48+
}
49+
50+
// Compare compares two lists lexically. The result will be 0 if l==rhs, -1
51+
// if l < rhs, and +1 if l > rhs.
52+
func ListCompare(lhs, rhs []interface{}) int {
53+
i := 0
54+
for {
55+
if i >= len(lhs) && i >= len(rhs) {
56+
// Lists are the same length and all items are equal.
57+
return 0
58+
}
59+
if i >= len(lhs) {
60+
// LHS is shorter.
61+
return -1
62+
}
63+
if i >= len(rhs) {
64+
// RHS is shorter.
65+
return 1
66+
}
67+
if c := Compare(lhs[i], rhs[i]); c != 0 {
68+
return c
69+
}
70+
// The items are equal; continue.
71+
i++
72+
}
73+
}

value/map.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package value
18+
19+
import (
20+
"fmt"
21+
"sort"
22+
"strings"
23+
)
24+
25+
// Equals compares two maps lexically.
26+
func MapEquals(lhs, rhs map[string]interface{}) bool {
27+
if len(lhs) != len(rhs) {
28+
return false
29+
}
30+
for k, vl := range lhs {
31+
vr, ok := rhs[k]
32+
if !ok {
33+
return false
34+
}
35+
if !Equals(vl, vr) {
36+
return false
37+
}
38+
}
39+
return true
40+
}
41+
42+
// Less compares two maps lexically.
43+
func MapLess(lhs, rhs map[string]interface{}) bool {
44+
return Compare(lhs, rhs) == -1
45+
}
46+
47+
// Compare compares two maps lexically.
48+
func MapCompare(lhs, rhs map[string]interface{}) int {
49+
lorder := make([]string, 0, len(lhs))
50+
for key := range lhs {
51+
lorder = append(lorder, key)
52+
}
53+
sort.Strings(lorder)
54+
rorder := make([]string, 0, len(rhs))
55+
for key := range rhs {
56+
rorder = append(rorder, key)
57+
}
58+
sort.Strings(rorder)
59+
60+
i := 0
61+
for {
62+
if i >= len(lorder) && i >= len(rorder) {
63+
// Maps are the same length and all items are equal.
64+
return 0
65+
}
66+
if i >= len(lorder) {
67+
// LHS is shorter.
68+
return -1
69+
}
70+
if i >= len(rorder) {
71+
// RHS is shorter.
72+
return 1
73+
}
74+
if c := strings.Compare(lorder[i], rorder[i]); c != 0 {
75+
return c
76+
}
77+
if c := Compare(lhs[lorder[i]], rhs[lorder[i]]); c != 0 {
78+
return c
79+
}
80+
// The items are equal; continue.
81+
i++
82+
}
83+
}
84+
85+
func IsMap(v Value) bool {
86+
if _, ok := v.(map[string]interface{}); ok {
87+
return true
88+
} else if _, ok := v.(map[interface{}]interface{}); ok {
89+
return true
90+
}
91+
return false
92+
}
93+
94+
func ValueMap(v Value) map[string]interface{} {
95+
if v == nil {
96+
return map[string]interface{}{}
97+
}
98+
switch t := v.(type) {
99+
case map[string]interface{}:
100+
return t
101+
case map[interface{}]interface{}:
102+
m := make(map[string]interface{}, len(t))
103+
for key, value := range t {
104+
if ks, ok := key.(string); ok {
105+
m[ks] = value
106+
}
107+
}
108+
return m
109+
}
110+
panic(fmt.Errorf("not a map: %#v", v))
111+
}

0 commit comments

Comments
 (0)