Skip to content

Commit c09935c

Browse files
Add error types for visit map and sequence assertions (#2941)
## Why Used in #2940. Since required validation queries against a static list of patterns, it's possible that the pattern it is querying is not defined in the bundle config tree. ## Tests Existing tests pass.
1 parent 3289f90 commit c09935c

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

libs/dyn/pattern.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dyn
22

33
import (
4+
"errors"
45
"fmt"
56
"slices"
67
)
@@ -66,11 +67,39 @@ func AnyKey() patternComponent {
6667
return anyKeyComponent{}
6768
}
6869

70+
type expectedMapError struct {
71+
p Path
72+
v Value
73+
}
74+
75+
func (e expectedMapError) Error() string {
76+
return fmt.Sprintf("expected a map at %q, found %s", e.p, e.v.Kind())
77+
}
78+
79+
func IsExpectedMapError(err error) bool {
80+
var target expectedMapError
81+
return errors.As(err, &target)
82+
}
83+
84+
type expectedSequenceError struct {
85+
p Path
86+
v Value
87+
}
88+
89+
func (e expectedSequenceError) Error() string {
90+
return fmt.Sprintf("expected a sequence at %q, found %s", e.p, e.v.Kind())
91+
}
92+
93+
func IsExpectedSequenceError(err error) bool {
94+
var target expectedSequenceError
95+
return errors.As(err, &target)
96+
}
97+
6998
// This function implements the patternComponent interface.
7099
func (c anyKeyComponent) visit(v Value, prefix Path, suffix Pattern, opts visitOptions) (Value, error) {
71100
m, ok := v.AsMap()
72101
if !ok {
73-
return InvalidValue, fmt.Errorf("expected a map at %q, found %s", prefix, v.Kind())
102+
return InvalidValue, expectedMapError{p: prefix, v: v}
74103
}
75104

76105
m = m.Clone()
@@ -105,7 +134,7 @@ func AnyIndex() patternComponent {
105134
func (c anyIndexComponent) visit(v Value, prefix Path, suffix Pattern, opts visitOptions) (Value, error) {
106135
s, ok := v.AsSequence()
107136
if !ok {
108-
return InvalidValue, fmt.Errorf("expected a sequence at %q, found %s", prefix, v.Kind())
137+
return InvalidValue, expectedSequenceError{p: prefix, v: v}
109138
}
110139

111140
s = slices.Clone(s)

libs/dyn/visit.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ func IsIndexOutOfBoundsError(err error) bool {
5454
return errors.As(err, &target)
5555
}
5656

57+
type expectedMapToIndexError struct {
58+
p Path
59+
v Value
60+
}
61+
62+
func (e expectedMapToIndexError) Error() string {
63+
return fmt.Sprintf("expected a map to index %q, found %s", e.p, e.v.Kind())
64+
}
65+
66+
func IsExpectedMapToIndexError(err error) bool {
67+
var target expectedMapToIndexError
68+
return errors.As(err, &target)
69+
}
70+
71+
type expectedSequenceToIndexError struct {
72+
p Path
73+
v Value
74+
}
75+
76+
func (e expectedSequenceToIndexError) Error() string {
77+
return fmt.Sprintf("expected a sequence to index %q, found %s", e.p, e.v.Kind())
78+
}
79+
80+
func IsExpectedSequenceToIndexError(err error) bool {
81+
var target expectedSequenceToIndexError
82+
return errors.As(err, &target)
83+
}
84+
5785
type visitOptions struct {
5886
// The function to apply to the value once found.
5987
//
@@ -98,7 +126,7 @@ func (component pathComponent) visit(v Value, prefix Path, suffix Pattern, opts
98126
case KindNil:
99127
return InvalidValue, cannotTraverseNilError{path}
100128
default:
101-
return InvalidValue, fmt.Errorf("expected a map to index %q, found %s", path, v.Kind())
129+
return InvalidValue, expectedMapToIndexError{p: path, v: v}
102130
}
103131

104132
m := v.MustMap()
@@ -137,7 +165,7 @@ func (component pathComponent) visit(v Value, prefix Path, suffix Pattern, opts
137165
case KindNil:
138166
return InvalidValue, cannotTraverseNilError{path}
139167
default:
140-
return InvalidValue, fmt.Errorf("expected a sequence to index %q, found %s", path, v.Kind())
168+
return InvalidValue, expectedSequenceToIndexError{p: path, v: v}
141169
}
142170

143171
s := v.MustSequence()

0 commit comments

Comments
 (0)