Skip to content

Commit 9818ffe

Browse files
author
Kevin Wiesmüller
committed
rename value conversion methods
Renames all conversion methods for value.Value e.g. `Map()` -> `AsMap()` This removes the implementation of `String() string`. Without this change, calling String() on a value would have behaved differently than usually expected in go.
1 parent ed118bb commit 9818ffe

File tree

6 files changed

+58
-58
lines changed

6 files changed

+58
-58
lines changed

fieldpath/fromvalue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (w *objectWalker) walk() {
5555
case w.value.IsList():
5656
// If the list were atomic, we'd break here, but we don't have
5757
// a schema, so we can't tell.
58-
list := w.value.List()
58+
list := w.value.AsList()
5959
for i := 0; i < list.Length(); i++ {
6060
w2 := *w
6161
w2.path = append(w.path, GuessBestListPathElement(i, list.At(i)))
@@ -67,7 +67,7 @@ func (w *objectWalker) walk() {
6767
// If the map/struct were atomic, we'd break here, but we don't
6868
// have a schema, so we can't tell.
6969

70-
w.value.Map().Iterate(func(k string, val value.Value) bool {
70+
w.value.AsMap().Iterate(func(k string, val value.Value) bool {
7171
w2 := *w
7272
w2.path = append(w.path, PathElement{FieldName: &k})
7373
w2.value = val
@@ -106,7 +106,7 @@ func GuessBestListPathElement(index int, item value.Value) PathElement {
106106

107107
var keys value.FieldList
108108
for _, name := range AssociativeListCandidateFieldNames {
109-
f, ok := item.Map().Get(name)
109+
f, ok := item.AsMap().Get(name)
110110
if !ok {
111111
continue
112112
}

typed/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func listValue(val value.Value) (value.List, error) {
162162
if !val.IsList() {
163163
return nil, fmt.Errorf("expected list, got %v", val)
164164
}
165-
return val.List(), nil
165+
return val.AsList(), nil
166166
}
167167

168168
// Returns the map, or an error. Reminder: nil is a valid map and might be returned.
@@ -177,7 +177,7 @@ func mapValue(val value.Value) (value.Map, error) {
177177
if !val.IsMap() {
178178
return nil, fmt.Errorf("expected map, got %v", val)
179179
}
180-
return val.Map(), nil
180+
return val.AsMap(), nil
181181
}
182182

183183
func keyedAssociativeListItemToPathElement(list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
@@ -192,7 +192,7 @@ func keyedAssociativeListItemToPathElement(list *schema.List, index int, child v
192192
}
193193
keyMap := value.FieldList{}
194194
for _, fieldName := range list.Keys {
195-
if val, ok := child.Map().Get(fieldName); ok {
195+
if val, ok := child.AsMap().Get(fieldName); ok {
196196
keyMap = append(keyMap, value.Field{Name: fieldName, Value: val})
197197
} else {
198198
return pe, fmt.Errorf("associative list with keys has an element that omits key field %q", fieldName)

typed/remove.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors {
4242
}
4343

4444
func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
45-
l := w.value.List()
45+
l := w.value.AsList()
4646

4747
// If list is null, empty, or atomic just return
4848
if l == nil || l.Length() == 0 || t.ElementRelationship == schema.Atomic {
@@ -70,7 +70,7 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
7070
}
7171

7272
func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
73-
m := w.value.Map()
73+
m := w.value.AsMap()
7474

7575
// If map is null, empty, or atomic just return
7676
if m == nil || m.Length() == 0 || t.ElementRelationship == schema.Atomic {

typed/union.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func normalizeUnions(w *mergingWalker) error {
3636

3737
var old value.Map
3838
if w.lhs != nil && !w.lhs.IsNull() {
39-
old = w.lhs.Map()
39+
old = w.lhs.AsMap()
4040
}
4141
for _, union := range atom.Map.Unions {
42-
if err := newUnion(&union).Normalize(old, w.rhs.Map(), value.NewValueInterface(*w.out).Map()); err != nil {
42+
if err := newUnion(&union).Normalize(old, w.rhs.AsMap(), value.NewValueInterface(*w.out).AsMap()); err != nil {
4343
return err
4444
}
4545
}
@@ -58,12 +58,12 @@ func normalizeUnionsApply(w *mergingWalker) error {
5858

5959
var old value.Map
6060
if w.lhs != nil && !w.lhs.IsNull() {
61-
old = w.lhs.Map()
61+
old = w.lhs.AsMap()
6262
}
6363

6464
for _, union := range atom.Map.Unions {
6565
out := value.NewValueInterface(*w.out)
66-
if err := newUnion(&union).NormalizeApply(old, w.rhs.Map(), out.Map()); err != nil {
66+
if err := newUnion(&union).NormalizeApply(old, w.rhs.AsMap(), out.AsMap()); err != nil {
6767
return err
6868
}
6969
*w.out = out.Unstructured()
@@ -126,7 +126,7 @@ func (d *discriminator) Get(m value.Map) discriminated {
126126
if !val.IsString() {
127127
return ""
128128
}
129-
return discriminated(val.String())
129+
return discriminated(val.AsString())
130130
}
131131

132132
type fieldsSet map[field]struct{}

value/value.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,30 @@ type Value interface {
5252
// IsMap returns true if the Value is null, false otherwise.
5353
IsNull() bool
5454

55-
// Map converts the Value into a Map (or panic if the type
55+
// AsMap converts the Value into a Map (or panic if the type
5656
// doesn't allow it).
57-
Map() Map
58-
// List converts the Value into a List (or panic if the type
57+
AsMap() Map
58+
// AsList converts the Value into a List (or panic if the type
5959
// doesn't allow it).
60-
List() List
61-
// Bool converts the Value into a bool (or panic if the type
60+
AsList() List
61+
// AsBool converts the Value into a bool (or panic if the type
6262
// doesn't allow it).
63-
Bool() bool
64-
// Int converts the Value into an int64 (or panic if the type
63+
AsBool() bool
64+
// AsInt converts the Value into an int64 (or panic if the type
6565
// doesn't allow it).
66-
Int() int64
67-
// Float converts the Value into a float64 (or panic if the type
66+
AsInt() int64
67+
// AsFloat converts the Value into a float64 (or panic if the type
6868
// doesn't allow it).
69-
Float() float64
70-
// String converts the Value into a string (or panic if the type
69+
AsFloat() float64
70+
// AsString converts the Value into a string (or panic if the type
7171
// doesn't allow it).
72-
String() string
72+
AsString() string
7373

74-
// Returns a value of this type that is no longer needed. The
74+
// Recycle returns a value of this type that is no longer needed. The
7575
// value shouldn't be used after this call.
7676
Recycle()
7777

78-
// Converts the Value into an Unstructured interface{}.
78+
// Unstructured converts the Value into an Unstructured interface{}.
7979
Unstructured() interface{}
8080
}
8181

@@ -131,49 +131,49 @@ func Equals(lhs, rhs Value) bool {
131131
if lhs.IsFloat() || rhs.IsFloat() {
132132
var lf float64
133133
if lhs.IsFloat() {
134-
lf = lhs.Float()
134+
lf = lhs.AsFloat()
135135
} else if lhs.IsInt() {
136-
lf = float64(lhs.Int())
136+
lf = float64(lhs.AsInt())
137137
} else {
138138
return false
139139
}
140140
var rf float64
141141
if rhs.IsFloat() {
142-
rf = rhs.Float()
142+
rf = rhs.AsFloat()
143143
} else if rhs.IsInt() {
144-
rf = float64(rhs.Int())
144+
rf = float64(rhs.AsInt())
145145
} else {
146146
return false
147147
}
148148
return lf == rf
149149
}
150150
if lhs.IsInt() {
151151
if rhs.IsInt() {
152-
return lhs.Int() == rhs.Int()
152+
return lhs.AsInt() == rhs.AsInt()
153153
}
154154
return false
155155
}
156156
if lhs.IsString() {
157157
if rhs.IsString() {
158-
return lhs.String() == rhs.String()
158+
return lhs.AsString() == rhs.AsString()
159159
}
160160
return false
161161
}
162162
if lhs.IsBool() {
163163
if rhs.IsBool() {
164-
return lhs.Bool() == rhs.Bool()
164+
return lhs.AsBool() == rhs.AsBool()
165165
}
166166
return false
167167
}
168168
if lhs.IsList() {
169169
if rhs.IsList() {
170-
return ListEquals(lhs.List(), rhs.List())
170+
return ListEquals(lhs.AsList(), rhs.AsList())
171171
}
172172
return false
173173
}
174174
if lhs.IsMap() {
175175
if rhs.IsMap() {
176-
return lhs.Map().Equals(rhs.Map())
176+
return lhs.AsMap().Equals(rhs.AsMap())
177177
}
178178
return false
179179
}
@@ -187,29 +187,29 @@ func Equals(lhs, rhs Value) bool {
187187
return true
188188
}
189189

190-
// String returns a human-readable representation of the value.
190+
// ToString returns a human-readable representation of the value.
191191
func ToString(v Value) string {
192192
if v.IsNull() {
193193
return "null"
194194
}
195195
switch {
196196
case v.IsFloat():
197-
return fmt.Sprintf("%v", v.Float())
197+
return fmt.Sprintf("%v", v.AsFloat())
198198
case v.IsInt():
199-
return fmt.Sprintf("%v", v.Int())
199+
return fmt.Sprintf("%v", v.AsInt())
200200
case v.IsString():
201-
return fmt.Sprintf("%q", v.String())
201+
return fmt.Sprintf("%q", v.AsString())
202202
case v.IsBool():
203-
return fmt.Sprintf("%v", v.Bool())
203+
return fmt.Sprintf("%v", v.AsBool())
204204
case v.IsList():
205205
strs := []string{}
206-
for i := 0; i < v.List().Length(); i++ {
207-
strs = append(strs, ToString(v.List().At(i)))
206+
for i := 0; i < v.AsList().Length(); i++ {
207+
strs = append(strs, ToString(v.AsList().At(i)))
208208
}
209209
return "[" + strings.Join(strs, ",") + "]"
210210
case v.IsMap():
211211
strs := []string{}
212-
v.Map().Iterate(func(k string, v Value) bool {
212+
v.AsMap().Iterate(func(k string, v Value) bool {
213213
strs = append(strs, fmt.Sprintf("%v=%v", k, ToString(v)))
214214
return true
215215
})
@@ -233,15 +233,15 @@ func Compare(lhs, rhs Value) int {
233233
if !rhs.IsFloat() {
234234
// Extra: compare floats and ints numerically.
235235
if rhs.IsInt() {
236-
return FloatCompare(lhs.Float(), float64(rhs.Int()))
236+
return FloatCompare(lhs.AsFloat(), float64(rhs.AsInt()))
237237
}
238238
return -1
239239
}
240-
return FloatCompare(lhs.Float(), rhs.Float())
240+
return FloatCompare(lhs.AsFloat(), rhs.AsFloat())
241241
} else if rhs.IsFloat() {
242242
// Extra: compare floats and ints numerically.
243243
if lhs.IsInt() {
244-
return FloatCompare(float64(lhs.Int()), rhs.Float())
244+
return FloatCompare(float64(lhs.AsInt()), rhs.AsFloat())
245245
}
246246
return 1
247247
}
@@ -250,7 +250,7 @@ func Compare(lhs, rhs Value) int {
250250
if !rhs.IsInt() {
251251
return -1
252252
}
253-
return IntCompare(lhs.Int(), rhs.Int())
253+
return IntCompare(lhs.AsInt(), rhs.AsInt())
254254
} else if rhs.IsInt() {
255255
return 1
256256
}
@@ -259,7 +259,7 @@ func Compare(lhs, rhs Value) int {
259259
if !rhs.IsString() {
260260
return -1
261261
}
262-
return strings.Compare(lhs.String(), rhs.String())
262+
return strings.Compare(lhs.AsString(), rhs.AsString())
263263
} else if rhs.IsString() {
264264
return 1
265265
}
@@ -268,7 +268,7 @@ func Compare(lhs, rhs Value) int {
268268
if !rhs.IsBool() {
269269
return -1
270270
}
271-
return BoolCompare(lhs.Bool(), rhs.Bool())
271+
return BoolCompare(lhs.AsBool(), rhs.AsBool())
272272
} else if rhs.IsBool() {
273273
return 1
274274
}
@@ -277,15 +277,15 @@ func Compare(lhs, rhs Value) int {
277277
if !rhs.IsList() {
278278
return -1
279279
}
280-
return ListCompare(lhs.List(), rhs.List())
280+
return ListCompare(lhs.AsList(), rhs.AsList())
281281
} else if rhs.IsList() {
282282
return 1
283283
}
284284
if lhs.IsMap() {
285285
if !rhs.IsMap() {
286286
return -1
287287
}
288-
return MapCompare(lhs.Map(), rhs.Map())
288+
return MapCompare(lhs.AsMap(), rhs.AsMap())
289289
} else if rhs.IsMap() {
290290
return 1
291291
}

value/valueunstructured.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (v valueUnstructured) IsMap() bool {
4949
return false
5050
}
5151

52-
func (v valueUnstructured) Map() Map {
52+
func (v valueUnstructured) AsMap() Map {
5353
if v.Value == nil {
5454
panic("invalid nil")
5555
}
@@ -70,7 +70,7 @@ func (v valueUnstructured) IsList() bool {
7070
return ok
7171
}
7272

73-
func (v valueUnstructured) List() List {
73+
func (v valueUnstructured) AsList() List {
7474
return listUnstructured(v.Value.([]interface{}))
7575
}
7676

@@ -85,7 +85,7 @@ func (v valueUnstructured) IsFloat() bool {
8585
return false
8686
}
8787

88-
func (v valueUnstructured) Float() float64 {
88+
func (v valueUnstructured) AsFloat() float64 {
8989
if f, ok := v.Value.(float32); ok {
9090
return float64(f)
9191
}
@@ -117,7 +117,7 @@ func (v valueUnstructured) IsInt() bool {
117117
return false
118118
}
119119

120-
func (v valueUnstructured) Int() int64 {
120+
func (v valueUnstructured) AsInt() int64 {
121121
if i, ok := v.Value.(int); ok {
122122
return int64(i)
123123
} else if i, ok := v.Value.(int8); ok {
@@ -146,7 +146,7 @@ func (v valueUnstructured) IsString() bool {
146146
return ok
147147
}
148148

149-
func (v valueUnstructured) String() string {
149+
func (v valueUnstructured) AsString() string {
150150
return v.Value.(string)
151151
}
152152

@@ -158,7 +158,7 @@ func (v valueUnstructured) IsBool() bool {
158158
return ok
159159
}
160160

161-
func (v valueUnstructured) Bool() bool {
161+
func (v valueUnstructured) AsBool() bool {
162162
return v.Value.(bool)
163163
}
164164

0 commit comments

Comments
 (0)