Skip to content

Commit 99a73ac

Browse files
committed
Address PR reviews 1
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 2b86504 commit 99a73ac

11 files changed

+304
-208
lines changed

pkg/apis/workspaces/v1alpha2/keyed_implementations.go

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1alpha2
22

33
import (
4+
"fmt"
45
"reflect"
56
)
67

@@ -18,3 +19,23 @@ func extractKeys(keyedList interface{}) []Keyed {
1819
}
1920
return keys
2021
}
22+
23+
// CheckDuplicateKeys checks if duplicate keys are present in the devfile objects
24+
func CheckDuplicateKeys(keyedList interface{}) error {
25+
seen := map[string]bool{}
26+
value := reflect.ValueOf(keyedList)
27+
for i := 0; i < value.Len(); i++ {
28+
elem := value.Index(i)
29+
if elem.CanInterface() {
30+
i := elem.Interface()
31+
if keyed, ok := i.(Keyed); ok {
32+
key := keyed.Key()
33+
if seen[key] {
34+
return fmt.Errorf("duplicate key: %s", key)
35+
}
36+
seen[key] = true
37+
}
38+
}
39+
}
40+
return nil
41+
}

pkg/validation/commands.go

+18-21
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,48 @@ package validation
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76

87
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
98
)
109

11-
// ValidateCommands validates the devfile commands:
12-
// 1. if the command id is all numeric, an error is returned
13-
// 2. if there are commands with duplicate IDs, an error is returned
14-
// 2. checks if its either a valid command
15-
// 3. checks if commands belonging to a specific group obeys the rule of 1 default command
10+
// ValidateCommands validates the devfile commands and checks:
11+
// 1. the command id is not all numeric
12+
// 2. there are no duplicate command ids
13+
// 3. the command type is not invalid
14+
// 4. commands belonging to a specific group obeys the rule of 1 default command
1615
func ValidateCommands(commands []v1alpha2.Command, components []v1alpha2.Component) (err error) {
17-
processedCommands := make(map[string]string, len(commands))
18-
groupCommandMap := make(map[v1alpha2.CommandGroup][]v1alpha2.Command)
16+
// processedCommands := make(map[string]string, len(commands))
17+
groupKindCommandMap := make(map[v1alpha2.CommandGroupKind][]v1alpha2.Command)
1918
commandMap := getCommandsMap(commands)
2019

20+
err = v1alpha2.CheckDuplicateKeys(commands)
21+
if err != nil {
22+
return err
23+
}
24+
2125
for _, command := range commands {
2226
// Check if command id is all numeric
2327
if isInt(command.Id) {
2428
return &InvalidNameOrIdError{id: command.Id, resourceType: "command"}
2529
}
2630

27-
// Check if the command is in the list of already processed commands
28-
// If there's a hit, it means more than one command share the same ID and we should error out
29-
if _, exists := processedCommands[command.Id]; exists {
30-
return &InvalidCommandError{commandId: command.Id, reason: "duplicate commands present with the same id"}
31-
}
32-
processedCommands[command.Id] = command.Id
33-
3431
parentCommands := make(map[string]string)
3532
err = validateCommand(command, parentCommands, commandMap, components)
3633
if err != nil {
3734
return err
3835
}
3936

40-
commandGroup := *getGroup(command)
41-
if !reflect.DeepEqual(commandGroup, v1alpha2.CommandGroup{}) {
42-
groupCommandMap[commandGroup] = append(groupCommandMap[commandGroup], command)
37+
commandGroup := getGroup(command)
38+
if commandGroup != nil {
39+
groupKindCommandMap[commandGroup.Kind] = append(groupKindCommandMap[commandGroup.Kind], command)
4340
}
4441
}
4542

4643
groupErrors := ""
47-
for group, commands := range groupCommandMap {
44+
for groupKind, commands := range groupKindCommandMap {
4845
if err = validateGroup(commands); err != nil {
49-
groupErrors += fmt.Sprintf("\ncommand group %s error - %s", group.Kind, err.Error())
46+
groupErrors += fmt.Sprintf("\ncommand group %s error - %s", groupKind, err.Error())
5047
}
5148
}
5249

@@ -156,7 +153,7 @@ func validateCommandComponent(command v1alpha2.Command, components []v1alpha2.Co
156153
}
157154

158155
// validateCompositeCommand checks that the specified composite command is valid. The command:
159-
// 1. should not reference itself via s subcommand
156+
// 1. should not reference itself via a subcommand
160157
// 2. should not indirectly reference itself via a subcommand which is a composite command
161158
// 3. should reference a valid devfile command
162159
// 4. should have a valid exec sub command

0 commit comments

Comments
 (0)