Skip to content

Commit 28cfac0

Browse files
committed
Address PR review feedback - attribute funcs
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent c0cbaf3 commit 28cfac0

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

pkg/devfile/parser/data/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type DevfileData interface {
2121

2222
GetAttributes() (attributes.Attributes, error)
2323
AddAttributes(key string, value interface{}) error
24-
UpdateAttributes(attr attributes.Attributes) error
24+
UpdateAttributes(key string, value interface{}) error
2525

2626
// parent related methods
2727

pkg/devfile/parser/data/v2/attributes.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,36 @@ func (d *DevfileV2) GetAttributes() (attributes.Attributes, error) {
1717
}
1818
}
1919

20-
// UpdateAttributes updates the devfile top level attributes
21-
func (d *DevfileV2) UpdateAttributes(attr attributes.Attributes) error {
20+
// UpdateAttributes updates the devfile top level attribute for the specific key, err out if key is absent
21+
func (d *DevfileV2) UpdateAttributes(key string, value interface{}) error {
22+
var err error
23+
2224
// This feature was introduced in 2.1.0; so any version 2.1.0 and up should use the 2.1.0 implementation
2325
switch d.SchemaVersion {
2426
case "2.0.0":
2527
return fmt.Errorf("top-level attributes is not supported in devfile schema version 2.0.0")
2628
default:
27-
d.Attributes = attr
29+
if d.Attributes.Exists(key) {
30+
d.Attributes.Put(key, value, &err)
31+
} else {
32+
return fmt.Errorf("cannot update top-level attribute, key %s is not present", key)
33+
}
2834
}
2935

30-
return nil
36+
return err
3137
}
3238

33-
// AddAttributes adds to the devfile top level attributes
39+
// AddAttributes adds to the devfile top level attributes, value will be overwritten if key is already present
3440
func (d *DevfileV2) AddAttributes(key string, value interface{}) error {
41+
var err error
42+
3543
// This feature was introduced in 2.1.0; so any version 2.1.0 and up should use the 2.1.0 implementation
3644
switch d.SchemaVersion {
3745
case "2.0.0":
3846
return fmt.Errorf("top-level attributes is not supported in devfile schema version 2.0.0")
3947
default:
40-
var err error
4148
d.Attributes.Put(key, value, &err)
42-
if err != nil {
43-
return err
44-
}
4549
}
4650

47-
return nil
51+
return err
4852
}

pkg/devfile/parser/data/v2/attributes_test.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ func TestGetAttributes(t *testing.T) {
6262

6363
func TestUpdateAttributes(t *testing.T) {
6464

65+
nestedValue := map[string]interface{}{
66+
"key1.1": map[string]interface{}{
67+
"key1.1.1": "value1.1.1",
68+
},
69+
}
70+
6571
tests := []struct {
66-
name string
67-
devfilev2 *DevfileV2
68-
attributes attributes.Attributes
69-
wantErr bool
72+
name string
73+
devfilev2 *DevfileV2
74+
key string
75+
value interface{}
76+
wantAttributes attributes.Attributes
77+
wantErr bool
7078
}{
7179
{
7280
name: "Schema 2.0.0 does not have attributes",
@@ -80,7 +88,25 @@ func TestUpdateAttributes(t *testing.T) {
8088
wantErr: true,
8189
},
8290
{
83-
name: "Schema 2.1.0 has attributes",
91+
name: "Schema 2.1.0 has the top-level key attribute",
92+
devfilev2: &DevfileV2{
93+
v1alpha2.Devfile{
94+
DevfileHeader: devfilepkg.DevfileHeader{
95+
SchemaVersion: "2.1.0",
96+
},
97+
DevWorkspaceTemplateSpec: v1alpha2.DevWorkspaceTemplateSpec{
98+
DevWorkspaceTemplateSpecContent: v1alpha2.DevWorkspaceTemplateSpecContent{
99+
Attributes: attributes.Attributes{}.PutString("key1", "value1").PutString("key2", "value2"),
100+
},
101+
},
102+
},
103+
},
104+
key: "key1",
105+
value: nestedValue,
106+
wantAttributes: attributes.Attributes{}.Put("key1", nestedValue, nil).PutString("key2", "value2"),
107+
},
108+
{
109+
name: "Schema 2.1.0 does not have the top-level key attribute",
84110
devfilev2: &DevfileV2{
85111
v1alpha2.Devfile{
86112
DevfileHeader: devfilepkg.DevfileHeader{
@@ -93,21 +119,24 @@ func TestUpdateAttributes(t *testing.T) {
93119
},
94120
},
95121
},
96-
attributes: attributes.Attributes{}.PutString("key3", "value3"),
122+
key: "key_invalid",
123+
value: nestedValue,
124+
wantErr: true,
97125
},
98126
}
99127
for _, tt := range tests {
100128
t.Run(tt.name, func(t *testing.T) {
101-
err := tt.devfilev2.UpdateAttributes(tt.attributes)
129+
err := tt.devfilev2.UpdateAttributes(tt.key, tt.value)
102130
if tt.wantErr == (err == nil) {
103131
t.Errorf("TestUpdateAttributes error - %v, wantErr %v", err, tt.wantErr)
104132
} else if err == nil {
105133
attributes, err := tt.devfilev2.GetAttributes()
106134
if err != nil {
107-
t.Errorf("TestUpdateAttributes error2 - %+v", err)
135+
t.Errorf("TestUpdateAttributes error - %+v", err)
136+
return
108137
}
109-
if !reflect.DeepEqual(attributes, tt.attributes) {
110-
t.Errorf("TestUpdateAttributes mismatch error - expected %+v, actual %+v", tt.attributes, attributes)
138+
if !reflect.DeepEqual(attributes, tt.wantAttributes) {
139+
t.Errorf("TestUpdateAttributes mismatch error - expected %+v, actual %+v", tt.wantAttributes, attributes)
111140
}
112141
}
113142
})
@@ -186,7 +215,8 @@ func TestAddAttributes(t *testing.T) {
186215
} else if err == nil {
187216
attributes, err := tt.devfilev2.GetAttributes()
188217
if err != nil {
189-
t.Errorf("TestAddAttributes error2 - %+v", err)
218+
t.Errorf("TestAddAttributes error - %+v", err)
219+
return
190220
}
191221
if !reflect.DeepEqual(attributes, tt.wantAttributes) {
192222
t.Errorf("TestAddAttributes mismatch error - expected %+v, actual %+v", tt.wantAttributes, attributes)

0 commit comments

Comments
 (0)