Skip to content

Commit 91816e9

Browse files
author
dmitriy kalinin
committed
show sibling keys in missing map key err
1 parent 5097ab2 commit 91816e9

File tree

7 files changed

+66
-15
lines changed

7 files changed

+66
-15
lines changed

patch/errs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package patch
22

33
import (
44
"fmt"
5+
"sort"
6+
"strings"
57
)
68

79
type opMismatchTypeErr struct {
@@ -22,3 +24,28 @@ func (e opMismatchTypeErr) Error() string {
2224
errMsg := "Expected to find %s at path '%s' but found '%T'"
2325
return fmt.Sprintf(errMsg, e.type_, e.path, e.obj)
2426
}
27+
28+
type opMissingMapKeyErr struct {
29+
key string
30+
path Pointer
31+
obj map[interface{}]interface{}
32+
}
33+
34+
func (e opMissingMapKeyErr) Error() string {
35+
errMsg := "Expected to find a map key '%s' for path '%s' (%s)"
36+
return fmt.Sprintf(errMsg, e.key, e.path, e.siblingKeysErrStr())
37+
}
38+
39+
func (e opMissingMapKeyErr) siblingKeysErrStr() string {
40+
if len(e.obj) == 0 {
41+
return "found no other map keys"
42+
}
43+
var keys []string
44+
for key, _ := range e.obj {
45+
if keyStr, ok := key.(string); ok {
46+
keys = append(keys, keyStr)
47+
}
48+
}
49+
sort.Sort(sort.StringSlice(keys))
50+
return "found map keys: '" + strings.Join(keys, "', '") + "'"
51+
}

patch/find_op.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ func (op FindOp) Apply(doc interface{}) (interface{}, error) {
9292

9393
obj, found = typedObj[typedToken.Key]
9494
if !found && !typedToken.Optional {
95-
errMsg := "Expected to find a map key '%s' for path '%s'"
96-
return nil, fmt.Errorf(errMsg, typedToken.Key, NewPointer(tokens[:i+2]))
95+
return nil, opMissingMapKeyErr{typedToken.Key, NewPointer(tokens[:i+2]), typedObj}
9796
}
9897

9998
if isLast {

patch/find_op_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,25 @@ var _ = Describe("FindOp.Apply", func() {
272272
_, err := FindOp{Path: MustNewPointerFromString("/abc/efg")}.Apply(doc)
273273
Expect(err).To(HaveOccurred())
274274
Expect(err.Error()).To(Equal(
275-
"Expected to find a map key 'abc' for path '/abc'"))
275+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'xyz')"))
276276
})
277277

278278
It("returns an error if key does not exist", func() {
279-
doc := map[interface{}]interface{}{"xyz": "xyz"}
279+
doc := map[interface{}]interface{}{"xyz": "xyz", 123: "xyz", "other-xyz": "xyz"}
280+
281+
_, err := FindOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
282+
Expect(err).To(HaveOccurred())
283+
Expect(err.Error()).To(Equal(
284+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'other-xyz', 'xyz')"))
285+
})
286+
287+
It("returns an error without other found keys when there are no keys and key does not exist", func() {
288+
doc := map[interface{}]interface{}{}
280289

281290
_, err := FindOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
282291
Expect(err).To(HaveOccurred())
283292
Expect(err.Error()).To(Equal(
284-
"Expected to find a map key 'abc' for path '/abc'"))
293+
"Expected to find a map key 'abc' for path '/abc' (found no other map keys)"))
285294
})
286295

287296
It("returns nil for missing key if key is not expected to exist", func() {

patch/remove_op.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ func (op RemoveOp) Apply(doc interface{}) (interface{}, error) {
9797
return doc, nil
9898
}
9999

100-
errMsg := "Expected to find a map key '%s' for path '%s'"
101-
return nil, fmt.Errorf(errMsg, typedToken.Key, NewPointer(tokens[:i+2]))
100+
return nil, opMissingMapKeyErr{typedToken.Key, NewPointer(tokens[:i+2]), typedObj}
102101
}
103102

104103
if isLast {

patch/remove_op_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,25 @@ var _ = Describe("RemoveOp.Apply", func() {
255255
_, err := RemoveOp{Path: MustNewPointerFromString("/abc/efg")}.Apply(doc)
256256
Expect(err).To(HaveOccurred())
257257
Expect(err.Error()).To(Equal(
258-
"Expected to find a map key 'abc' for path '/abc'"))
258+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'xyz')"))
259259
})
260260

261261
It("returns an error if key does not exist", func() {
262-
doc := map[interface{}]interface{}{"xyz": "xyz"}
262+
doc := map[interface{}]interface{}{"xyz": "xyz", 123: "xyz", "other-xyz": "xyz"}
263+
264+
_, err := RemoveOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
265+
Expect(err).To(HaveOccurred())
266+
Expect(err.Error()).To(Equal(
267+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'other-xyz', 'xyz')"))
268+
})
269+
270+
It("returns an error without other found keys when there are no keys and key does not exist", func() {
271+
doc := map[interface{}]interface{}{}
263272

264273
_, err := RemoveOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
265274
Expect(err).To(HaveOccurred())
266275
Expect(err.Error()).To(Equal(
267-
"Expected to find a map key 'abc' for path '/abc'"))
276+
"Expected to find a map key 'abc' for path '/abc' (found no other map keys)"))
268277
})
269278

270279
It("returns an error if it's not a map when key is being accessed", func() {

patch/replace_op.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ func (op ReplaceOp) Apply(doc interface{}) (interface{}, error) {
102102

103103
obj, found = typedObj[typedToken.Key]
104104
if !found && !typedToken.Optional {
105-
errMsg := "Expected to find a map key '%s' for path '%s'"
106-
return nil, fmt.Errorf(errMsg, typedToken.Key, NewPointer(tokens[:i+2]))
105+
return nil, opMissingMapKeyErr{typedToken.Key, NewPointer(tokens[:i+2]), typedObj}
107106
}
108107

109108
if isLast {

patch/replace_op_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,25 @@ var _ = Describe("ReplaceOp.Apply", func() {
326326
_, err := ReplaceOp{Path: MustNewPointerFromString("/abc/efg")}.Apply(doc)
327327
Expect(err).To(HaveOccurred())
328328
Expect(err.Error()).To(Equal(
329-
"Expected to find a map key 'abc' for path '/abc'"))
329+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'xyz')"))
330330
})
331331

332332
It("returns an error if key does not exist", func() {
333-
doc := map[interface{}]interface{}{"xyz": "xyz"}
333+
doc := map[interface{}]interface{}{"xyz": "xyz", 123: "xyz", "other-xyz": "xyz"}
334+
335+
_, err := ReplaceOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
336+
Expect(err).To(HaveOccurred())
337+
Expect(err.Error()).To(Equal(
338+
"Expected to find a map key 'abc' for path '/abc' (found map keys: 'other-xyz', 'xyz')"))
339+
})
340+
341+
It("returns an error without other found keys when there are no keys and key does not exist", func() {
342+
doc := map[interface{}]interface{}{}
334343

335344
_, err := ReplaceOp{Path: MustNewPointerFromString("/abc")}.Apply(doc)
336345
Expect(err).To(HaveOccurred())
337346
Expect(err.Error()).To(Equal(
338-
"Expected to find a map key 'abc' for path '/abc'"))
347+
"Expected to find a map key 'abc' for path '/abc' (found no other map keys)"))
339348
})
340349

341350
It("creates missing key if key is not expected to exist", func() {

0 commit comments

Comments
 (0)