Proper printing for wildcards
Signed-off-by: Hasan Turken <turkenh@gmail.com>
This commit is contained in:
parent
77b66f3d77
commit
47bff13a91
|
|
@ -78,7 +78,7 @@ func (sg Segments) String() string {
|
|||
for _, s := range sg {
|
||||
switch s.Type {
|
||||
case SegmentField:
|
||||
if strings.ContainsRune(s.Field, period) {
|
||||
if s.Field == wildcard || strings.ContainsRune(s.Field, period) {
|
||||
b.WriteString(fmt.Sprintf("[%s]", s.Field))
|
||||
continue
|
||||
}
|
||||
|
|
@ -138,6 +138,8 @@ const (
|
|||
period = '.'
|
||||
leftBracket = '['
|
||||
rightBracket = ']'
|
||||
|
||||
wildcard = "*"
|
||||
)
|
||||
|
||||
type itemType int
|
||||
|
|
|
|||
|
|
@ -56,6 +56,15 @@ func TestSegments(t *testing.T) {
|
|||
},
|
||||
want: "data[.config.yml]",
|
||||
},
|
||||
"Wildcard": {
|
||||
s: Segments{
|
||||
Field("spec"),
|
||||
Field("containers"),
|
||||
FieldOrIndex("*"),
|
||||
Field("name"),
|
||||
},
|
||||
want: "spec.containers[*].name",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
|
|
|
|||
|
|
@ -157,7 +157,7 @@ func expandWildcards(data interface{}, segments Segments) ([]Segments, error) {
|
|||
it := data
|
||||
for i, current := range segments {
|
||||
// wildcards are regular fields with "*" as string
|
||||
if current.Type == SegmentField && current.Field == "*" {
|
||||
if current.Type == SegmentField && current.Field == wildcard {
|
||||
switch mapOrArray := it.(type) {
|
||||
case []interface{}:
|
||||
for ix := range mapOrArray {
|
||||
|
|
|
|||
|
|
@ -942,7 +942,7 @@ func TestExpandWildcards(t *testing.T) {
|
|||
path: "spec.containers[0].name[*]",
|
||||
data: []byte(`{"spec":{"containers":[{"name":"cool"}]}}`),
|
||||
want: want{
|
||||
err: errors.Wrapf(errors.Errorf("%q: unexpected wildcard usage", "spec.containers[0].name"), "cannot expand wildcards for segments: %q", "spec.containers[0].name.*"),
|
||||
err: errors.Wrapf(errors.Errorf("%q: unexpected wildcard usage", "spec.containers[0].name"), "cannot expand wildcards for segments: %q", "spec.containers[0].name[*]"),
|
||||
},
|
||||
},
|
||||
"NotAnArray": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue