Skip to content

Commit bd8ba50

Browse files
committed
fix: support zero array notations
1 parent 47607bb commit bd8ba50

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

internal/pkg/gitops/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ func unwrapYaml(yaml map[string]interface{}, notion string) (string, error) {
4949
if r.MatchString(k) {
5050
idx := r.FindString(k)[:1]
5151

52-
if len(idx) > 0 {
52+
if len(idx) >= 0 {
5353
i, err := strconv.ParseInt(idx, 10, 8)
5454

5555
if err != nil {
5656
return "", err
5757
}
5858

59-
k = k[:len(k)-int(i)]
59+
k = k[:len(k)-len(fmt.Sprintf("[%d]", i))]
6060

6161
if a, ok := d[k].([]interface{}); ok {
6262
if b, ok := a[i].(map[string]interface{}); ok {

internal/pkg/gitops/version_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestReadCurrentVersion(t *testing.T) {
3030
{
3131
name: "invalid yaml",
3232
args: args{
33-
f: []byte("wibble&&3..\nfff"),
33+
f: []byte("wibble&&3..\nfff"),
3434
notation: "image.tag",
3535
},
3636
want: "",
@@ -39,7 +39,7 @@ func TestReadCurrentVersion(t *testing.T) {
3939
{
4040
name: "simple yaml",
4141
args: args{
42-
f: createSimpleYaml("v1.2.99"),
42+
f: createSimpleYaml("v1.2.99"),
4343
notation: "image.tag",
4444
},
4545
want: "v1.2.99",
@@ -48,7 +48,7 @@ func TestReadCurrentVersion(t *testing.T) {
4848
{
4949
name: "invalid notation to yaml",
5050
args: args{
51-
f: createSimpleYaml("v1.2.99"),
51+
f: createSimpleYaml("v1.2.99"),
5252
notation: "image.nope",
5353
},
5454
want: "",
@@ -57,12 +57,21 @@ func TestReadCurrentVersion(t *testing.T) {
5757
{
5858
name: "array notation",
5959
args: args{
60-
f: createSimpleArrayYaml("v1.9"),
60+
f: createSimpleArrayYaml("v1.9"),
6161
notation: "images[3].tag",
6262
},
6363
want: "v1.9",
6464
wantErr: false,
6565
},
66+
{
67+
name: "helm example",
68+
args: args{
69+
f: createHelmYaml("v1.9"),
70+
notation: "dependencies[0].version",
71+
},
72+
want: "v1.9",
73+
wantErr: false,
74+
},
6675
}
6776

6877
fmt.Println(string(createSimpleArrayYaml("v1.2.99")))
@@ -106,7 +115,30 @@ func createSimpleArrayYaml(t string) []byte {
106115
marshal, err := yaml.Marshal(&struct {
107116
Images []tag `yaml:"images"`
108117
}{
109-
Images: []tag{{Tag: "v1"},{Tag: "v2"},{Tag: "v3"},{Tag: t}},
118+
Images: []tag{{Tag: "v1"}, {Tag: "v2"}, {Tag: "v3"}, {Tag: t}},
119+
})
120+
if err != nil {
121+
return nil
122+
}
123+
124+
return marshal
125+
}
126+
127+
func createHelmYaml(t string) []byte {
128+
type dependencies struct {
129+
Name string `yaml:"name"`
130+
Version string `yaml:"version"`
131+
Repository string `yaml:"repository"`
132+
}
133+
134+
marshal, err := yaml.Marshal(&struct {
135+
Dependencies []dependencies `yaml:"dependencies"`
136+
}{
137+
Dependencies: []dependencies{{
138+
Name: "example-test",
139+
Version: t,
140+
Repository: "gsdevme/test",
141+
}},
110142
})
111143
if err != nil {
112144
return nil

0 commit comments

Comments
 (0)