Skip to content

Commit c1734b6

Browse files
author
Antoine Pelisse
committed
Improve documentation and refactor value
1 parent 4753406 commit c1734b6

File tree

8 files changed

+410
-321
lines changed

8 files changed

+410
-321
lines changed

typed/union.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func normalizeUnions(w *mergingWalker) error {
3939
old = (*w.lhs).Map()
4040
}
4141
for _, union := range atom.Map.Unions {
42-
if err := newUnion(&union).Normalize(old, (*w.rhs).Map(), (value.ValueInterface{Value: *w.out}.Map())); err != nil {
42+
if err := newUnion(&union).Normalize(old, (*w.rhs).Map(), value.NewValueInterface(*w.out).Map()); err != nil {
4343
return err
4444
}
4545
}
@@ -62,7 +62,7 @@ func normalizeUnionsApply(w *mergingWalker) error {
6262
}
6363

6464
for _, union := range atom.Map.Unions {
65-
out := value.ValueInterface{Value: *w.out}
65+
out := value.NewValueInterface(*w.out)
6666
if err := newUnion(&union).NormalizeApply(old, (*w.rhs).Map(), out.Map()); err != nil {
6767
return err
6868
}

value/fields.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (f FieldList) Less(rhs FieldList) bool {
5252
return f.Compare(rhs) == -1
5353
}
5454

55-
// Less compares two lists lexically. The result will be 0 if f==rhs, -1
55+
// Compare compares two lists lexically. The result will be 0 if f==rhs, -1
5656
// if f < rhs, and +1 if f > rhs.
5757
func (f FieldList) Compare(rhs FieldList) int {
5858
i := 0

value/list.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,16 @@ limitations under the License.
1616

1717
package value
1818

19+
// List represents a list object.
1920
type List interface {
20-
Interface() []interface{}
21+
// Length returns how many items can be found in the map.
2122
Length() int
23+
// At returns the item at the given position in the map. It will
24+
// panic if the index is out of range.
2225
At(int) Value
2326
}
2427

25-
type ListInterface []interface{}
26-
27-
func (l ListInterface) Interface() []interface{} {
28-
return l
29-
}
30-
31-
func (l ListInterface) Length() int {
32-
return len(l)
33-
}
34-
35-
func (l ListInterface) At(i int) Value {
36-
return NewValueInterface(l[i])
37-
}
38-
39-
// Equals compares two lists lexically.
28+
// ListEquals compares two lists lexically.
4029
func ListEquals(lhs, rhs List) bool {
4130
if lhs.Length() != rhs.Length() {
4231
return false
@@ -56,12 +45,12 @@ func ListEquals(lhs, rhs List) bool {
5645
return true
5746
}
5847

59-
// Less compares two lists lexically.
48+
// ListLess compares two lists lexically.
6049
func ListLess(lhs, rhs List) bool {
6150
return ListCompare(lhs, rhs) == -1
6251
}
6352

64-
// Compare compares two lists lexically. The result will be 0 if l==rhs, -1
53+
// ListCompare compares two lists lexically. The result will be 0 if l==rhs, -1
6554
// if l < rhs, and +1 if l > rhs.
6655
func ListCompare(lhs, rhs List) int {
6756
i := 0

value/listinterface.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
type listInterface []interface{}
20+
21+
func (l listInterface) Length() int {
22+
return len(l)
23+
}
24+
25+
func (l listInterface) At(i int) Value {
26+
return NewValueInterface(l[i])
27+
}

value/map.go

Lines changed: 11 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@ import (
2121
"strings"
2222
)
2323

24+
// Map represents a Map or go structure.
2425
type Map interface {
26+
// Set changes or set the value of the given key.
2527
Set(key string, val Value)
28+
// Get returns the value for the given key, if present, or (nil, false) otherwise.
2629
Get(key string) (Value, bool)
30+
// Delete removes the key from the map.
2731
Delete(key string)
32+
// Equals compares the two maps, and return true if they are the same, false otherwise.
2833
Equals(other Map) bool
34+
// Iterate runs the given function for each key/value in the
35+
// map. Returning false in the closure prematurely stops the
36+
// iteration.
2937
Iterate(func(key string, value Value) bool) bool
38+
// Length returns the number of items in the map.
3039
Length() int
3140
}
3241

33-
// Less compares two maps lexically.
42+
// MapLess compares two maps lexically.
3443
func MapLess(lhs, rhs Map) bool {
3544
return MapCompare(lhs, rhs) == -1
3645
}
3746

38-
// Compare compares two maps lexically.
47+
// MapCompare compares two maps lexically.
3948
func MapCompare(lhs, rhs Map) int {
4049
lorder := make([]string, 0, lhs.Length())
4150
lhs.Iterate(func(key string, _ Value) bool {
@@ -78,121 +87,3 @@ func MapCompare(lhs, rhs Map) int {
7887
i++
7988
}
8089
}
81-
82-
type MapInterface map[interface{}]interface{}
83-
84-
func (m MapInterface) Set(key string, val Value) {
85-
m[key] = val.Interface()
86-
}
87-
88-
func (m MapInterface) Get(key string) (Value, bool) {
89-
if v, ok := m[key]; !ok {
90-
return nil, false
91-
} else {
92-
return NewValueInterface(v), true
93-
}
94-
}
95-
96-
func (m MapInterface) Delete(key string) {
97-
delete(m, key)
98-
}
99-
100-
func (m MapInterface) Iterate(fn func(key string, value Value) bool) bool {
101-
for k, v := range m {
102-
if ks, ok := k.(string); !ok {
103-
continue
104-
} else {
105-
vv := NewValueInterface(v)
106-
if !fn(ks, vv) {
107-
vv.Recycle()
108-
return false
109-
}
110-
vv.Recycle()
111-
}
112-
}
113-
return true
114-
}
115-
116-
func (m MapInterface) Length() int {
117-
return len(m)
118-
}
119-
120-
func (m MapInterface) Equals(other Map) bool {
121-
if m.Length() != other.Length() {
122-
return false
123-
}
124-
for k, v := range m {
125-
ks, ok := k.(string)
126-
if !ok {
127-
return false
128-
}
129-
vo, ok := other.Get(ks)
130-
if !ok {
131-
return false
132-
}
133-
vv := NewValueInterface(v)
134-
if !Equals(vv, vo) {
135-
vv.Recycle()
136-
vo.Recycle()
137-
return false
138-
}
139-
vo.Recycle()
140-
vv.Recycle()
141-
}
142-
return true
143-
}
144-
145-
type MapString map[string]interface{}
146-
147-
func (m MapString) Set(key string, val Value) {
148-
m[key] = val.Interface()
149-
}
150-
151-
func (m MapString) Get(key string) (Value, bool) {
152-
if v, ok := m[key]; !ok {
153-
return nil, false
154-
} else {
155-
return NewValueInterface(v), true
156-
}
157-
}
158-
159-
func (m MapString) Delete(key string) {
160-
delete(m, key)
161-
}
162-
163-
func (m MapString) Iterate(fn func(key string, value Value) bool) bool {
164-
for k, v := range m {
165-
vv := NewValueInterface(v)
166-
if !fn(k, vv) {
167-
vv.Recycle()
168-
return false
169-
}
170-
vv.Recycle()
171-
}
172-
return true
173-
}
174-
175-
func (m MapString) Length() int {
176-
return len(m)
177-
}
178-
179-
func (m MapString) Equals(other Map) bool {
180-
if m.Length() != other.Length() {
181-
return false
182-
}
183-
for k, v := range m {
184-
vo, ok := other.Get(k)
185-
if !ok {
186-
return false
187-
}
188-
vv := NewValueInterface(v)
189-
if !Equals(vv, vo) {
190-
vo.Recycle()
191-
vv.Recycle()
192-
return false
193-
}
194-
vo.Recycle()
195-
vv.Recycle()
196-
}
197-
return true
198-
}

value/mapinterface.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
type mapInterface map[interface{}]interface{}
20+
21+
func (m mapInterface) Set(key string, val Value) {
22+
m[key] = val.Interface()
23+
}
24+
25+
func (m mapInterface) Get(key string) (Value, bool) {
26+
if v, ok := m[key]; !ok {
27+
return nil, false
28+
} else {
29+
return NewValueInterface(v), true
30+
}
31+
}
32+
33+
func (m mapInterface) Delete(key string) {
34+
delete(m, key)
35+
}
36+
37+
func (m mapInterface) Iterate(fn func(key string, value Value) bool) bool {
38+
for k, v := range m {
39+
if ks, ok := k.(string); !ok {
40+
continue
41+
} else {
42+
vv := NewValueInterface(v)
43+
if !fn(ks, vv) {
44+
vv.Recycle()
45+
return false
46+
}
47+
vv.Recycle()
48+
}
49+
}
50+
return true
51+
}
52+
53+
func (m mapInterface) Length() int {
54+
return len(m)
55+
}
56+
57+
func (m mapInterface) Equals(other Map) bool {
58+
if m.Length() != other.Length() {
59+
return false
60+
}
61+
for k, v := range m {
62+
ks, ok := k.(string)
63+
if !ok {
64+
return false
65+
}
66+
vo, ok := other.Get(ks)
67+
if !ok {
68+
return false
69+
}
70+
vv := NewValueInterface(v)
71+
if !Equals(vv, vo) {
72+
vv.Recycle()
73+
vo.Recycle()
74+
return false
75+
}
76+
vo.Recycle()
77+
vv.Recycle()
78+
}
79+
return true
80+
}
81+
82+
type mapString map[string]interface{}
83+
84+
func (m mapString) Set(key string, val Value) {
85+
m[key] = val.Interface()
86+
}
87+
88+
func (m mapString) Get(key string) (Value, bool) {
89+
if v, ok := m[key]; !ok {
90+
return nil, false
91+
} else {
92+
return NewValueInterface(v), true
93+
}
94+
}
95+
96+
func (m mapString) Delete(key string) {
97+
delete(m, key)
98+
}
99+
100+
func (m mapString) Iterate(fn func(key string, value Value) bool) bool {
101+
for k, v := range m {
102+
vv := NewValueInterface(v)
103+
if !fn(k, vv) {
104+
vv.Recycle()
105+
return false
106+
}
107+
vv.Recycle()
108+
}
109+
return true
110+
}
111+
112+
func (m mapString) Length() int {
113+
return len(m)
114+
}
115+
116+
func (m mapString) Equals(other Map) bool {
117+
if m.Length() != other.Length() {
118+
return false
119+
}
120+
for k, v := range m {
121+
vo, ok := other.Get(k)
122+
if !ok {
123+
return false
124+
}
125+
vv := NewValueInterface(v)
126+
if !Equals(vv, vo) {
127+
vo.Recycle()
128+
vv.Recycle()
129+
return false
130+
}
131+
vo.Recycle()
132+
vv.Recycle()
133+
}
134+
return true
135+
}

0 commit comments

Comments
 (0)