Proper printing for wildcards

Signed-off-by: Hasan Turken <turkenh@gmail.com>
This commit is contained in:
Hasan Turken 2021-10-04 15:19:14 +03:00
parent 77b66f3d77
commit 47bff13a91
No known key found for this signature in database
GPG Key ID: D7AA042F8F8B488E
4 changed files with 14 additions and 3 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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": {