Skip to content

Commit 1103496

Browse files
committed
Adapt conversion code for v1alpha2 attributes field
* Add conversion functions for going from map[string]string to attributes.Attributes * Implement conversion of command attributes for Commands * Adapt tests to handle new incompatibility (parent commands cannot have attributes) Signed-off-by: Angel Misevski <[email protected]>
1 parent 1b3fea0 commit 1103496

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package v1alpha1
2+
3+
import (
4+
"fmt"
5+
"github.com/devfile/api/pkg/attributes"
6+
)
7+
8+
func convertAttributesTo_v1alpha2(src map[string]string, dest *attributes.Attributes) {
9+
dest.FromStringMap(src)
10+
}
11+
12+
func convertAttributesFrom_v1alpha2(src *attributes.Attributes, dest map[string]string) error {
13+
if dest == nil {
14+
return fmt.Errorf("trying to insert into a nil map")
15+
}
16+
var err error
17+
stringAttributes := src.Strings(&err)
18+
if err != nil {
19+
return err
20+
}
21+
for k, v := range stringAttributes {
22+
dest[k] = v
23+
}
24+
return nil
25+
}
26+
27+
func getCommandAttributes(command *Command) map[string]string {
28+
switch {
29+
case command.Exec != nil:
30+
return command.Exec.Attributes
31+
case command.Apply != nil:
32+
return command.Apply.Attributes
33+
case command.VscodeTask != nil:
34+
return command.VscodeTask.Attributes
35+
case command.VscodeLaunch != nil:
36+
return command.VscodeLaunch.Attributes
37+
case command.Composite != nil:
38+
return command.Composite.Attributes
39+
case command.Custom != nil:
40+
return command.Custom.Attributes
41+
}
42+
return nil
43+
}

pkg/apis/workspaces/v1alpha1/commands_conversion.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v1alpha1
22

33
import (
44
"encoding/json"
5+
"github.com/devfile/api/pkg/attributes"
56

67
"github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
78
)
@@ -19,6 +20,25 @@ func convertCommandTo_v1alpha2(src *Command, dest *v1alpha2.Command) error {
1920
if err != nil {
2021
return err
2122
}
23+
var srcAttributes map[string]string
24+
switch {
25+
case src.Exec != nil:
26+
srcAttributes = src.Exec.Attributes
27+
case src.Apply != nil:
28+
srcAttributes = src.Apply.Attributes
29+
case src.VscodeTask != nil:
30+
srcAttributes = src.VscodeTask.Attributes
31+
case src.VscodeLaunch != nil:
32+
srcAttributes = src.VscodeLaunch.Attributes
33+
case src.Composite != nil:
34+
srcAttributes = src.Composite.Attributes
35+
case src.Custom != nil:
36+
srcAttributes = src.Custom.Attributes
37+
}
38+
if srcAttributes != nil {
39+
dest.Attributes = attributes.Attributes{}
40+
convertAttributesTo_v1alpha2(srcAttributes, &dest.Attributes)
41+
}
2242
dest.Id = id
2343
return nil
2444
}
@@ -30,19 +50,37 @@ func convertCommandFrom_v1alpha2(src *v1alpha2.Command, dest *Command) error {
3050
return err
3151
}
3252
err = json.Unmarshal(jsonCommand, dest)
53+
if err != nil {
54+
return err
55+
}
56+
var destAttributes map[string]string
57+
if src.Attributes != nil {
58+
destAttributes = make(map[string]string)
59+
err = convertAttributesFrom_v1alpha2(&src.Attributes, destAttributes)
60+
if err != nil {
61+
return err
62+
}
63+
}
64+
3365
switch {
3466
case dest.Apply != nil:
67+
dest.Apply.Attributes = destAttributes
3568
dest.Apply.Id = id
3669
case dest.Composite != nil:
70+
dest.Composite.Attributes = destAttributes
3771
dest.Composite.Id = id
3872
case dest.Custom != nil:
73+
dest.Custom.Attributes = destAttributes
3974
dest.Custom.Id = id
4075
case dest.Exec != nil:
76+
dest.Exec.Attributes = destAttributes
4177
dest.Exec.Id = id
4278
case dest.VscodeLaunch != nil:
79+
dest.VscodeLaunch.Attributes = destAttributes
4380
dest.VscodeLaunch.Id = id
4481
case dest.VscodeTask != nil:
82+
dest.VscodeTask.Attributes = destAttributes
4583
dest.VscodeTask.Id = id
4684
}
47-
return nil
85+
return err
4886
}

pkg/apis/workspaces/v1alpha1/conversion_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,33 @@ var parentComponentFuzzFunc = func(component *Component, c fuzz.Continue) {
129129

130130
var parentCommandFuzzFunc = func(command *Command, c fuzz.Continue) {
131131
// Do not generate Custom commands for Parents
132+
// Also: commands in Parents cannot have attributes.
132133
switch c.Intn(5) {
133134
case 0:
134135
c.Fuzz(&command.Apply)
136+
if command.Apply != nil {
137+
command.Apply.Attributes = nil
138+
}
135139
case 1:
136140
c.Fuzz(&command.Composite)
141+
if command.Composite != nil {
142+
command.Composite.Attributes = nil
143+
}
137144
case 2:
138145
c.Fuzz(&command.Exec)
146+
if command.Exec != nil {
147+
command.Exec.Attributes = nil
148+
}
139149
case 3:
140150
c.Fuzz(&command.VscodeLaunch)
151+
if command.VscodeLaunch != nil {
152+
command.VscodeLaunch.Attributes = nil
153+
}
141154
case 4:
142155
c.Fuzz(&command.VscodeTask)
156+
if command.VscodeTask != nil {
157+
command.VscodeTask.Attributes = nil
158+
}
143159
}
144160
}
145161

0 commit comments

Comments
 (0)