checkKeys provides a generic way to apply some validation on the content of each type of top-level list contained in the `toplevelListContainers` passed in argument. For each type of top-level list, the `keysSets` argument that will be passed to the `doCheck` function contains the the key sets that
(doCheck checkFn, toplevelListContainers ...dw.TopLevelListContainer)
| 35 | // contains the the key sets that correspond to the `toplevelListContainers` passed to this method, |
| 36 | // in the same order. |
| 37 | func checkKeys(doCheck checkFn, toplevelListContainers ...dw.TopLevelListContainer) error { |
| 38 | var errors *multierror.Error |
| 39 | |
| 40 | // intermediate storage for the conversion []map[string]KeyedList -> map[string][]sets.String |
| 41 | listTypeToKeys := map[string][]sets.String{} |
| 42 | |
| 43 | // Flatten []map[string]KeyedList -> map[string][]KeyedList based on map keys and convert each KeyedList |
| 44 | // into a sets.String |
| 45 | for _, topLevelListContainer := range toplevelListContainers { |
| 46 | topLevelList := topLevelListContainer.GetToplevelLists() |
| 47 | for listType, listElem := range topLevelList { |
| 48 | listTypeToKeys[listType] = append(listTypeToKeys[listType], sets.NewString(listElem.GetKeys()...)) |
| 49 | } |
| 50 | |
| 51 | value := reflect.ValueOf(topLevelListContainer) |
| 52 | |
| 53 | var variableValue reflect.Value |
| 54 | var attributeValue reflect.Value |
| 55 | |
| 56 | // toplevelListContainers can contain either a pointer or a struct and needs to be safeguarded when using reflect |
| 57 | if value.Kind() == reflect.Ptr { |
| 58 | variableValue = value.Elem().FieldByName("Variables") |
| 59 | attributeValue = value.Elem().FieldByName("Attributes") |
| 60 | } else { |
| 61 | variableValue = value.FieldByName("Variables") |
| 62 | attributeValue = value.FieldByName("Attributes") |
| 63 | } |
| 64 | |
| 65 | if variableValue.IsValid() && variableValue.Kind() == reflect.Map { |
| 66 | mapIter := variableValue.MapRange() |
| 67 | |
| 68 | var variableKeys []string |
| 69 | for mapIter.Next() { |
| 70 | k := mapIter.Key() |
| 71 | v := mapIter.Value() |
| 72 | if k.Kind() != reflect.String || v.Kind() != reflect.String { |
| 73 | return fmt.Errorf("unable to fetch top-level Variables, top-level Variables should be map of strings") |
| 74 | } |
| 75 | variableKeys = append(variableKeys, k.String()) |
| 76 | } |
| 77 | listTypeToKeys["Variables"] = append(listTypeToKeys["Variables"], sets.NewString(variableKeys...)) |
| 78 | } |
| 79 | |
| 80 | if attributeValue.IsValid() && attributeValue.CanInterface() { |
| 81 | attributes, ok := attributeValue.Interface().(attributesPkg.Attributes) |
| 82 | if !ok { |
| 83 | return fmt.Errorf("unable to fetch top-level Attributes from the devfile data") |
| 84 | } |
| 85 | var attributeKeys []string |
| 86 | for k := range attributes { |
| 87 | attributeKeys = append(attributeKeys, k) |
| 88 | } |
| 89 | listTypeToKeys["Attributes"] = append(listTypeToKeys["Attributes"], sets.NewString(attributeKeys...)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | for listType, keySets := range listTypeToKeys { |
| 94 | errors = multierror.Append(errors, doCheck(listType, keySets)...) |
no test coverage detected