2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
38 Enum []string `yaml:"enum"`
39 Type string `yaml:"type"`
40 Description string `yaml:"description"`
44 var apiDir = flag.String("apidir", "", "Directory containing API definitions to fix")
46 err := filepath.Walk(*apiDir, fixEnums)
52 func fixEnums(path string, info os.FileInfo, _ error) error {
53 if !info.IsDir() && strings.HasSuffix(info.Name(), ".yaml") {
54 yamlFile, err := ioutil.ReadFile(path)
56 log.Printf("yamlFile. Get err #%v ", err)
58 m := make(map[string]interface{})
59 err = yaml.Unmarshal(yamlFile, m)
61 log.Fatalf("Unmarshal: %v", err)
63 components := m["components"]
64 if components != nil {
65 cMap := components.(map[interface{}]interface{})
66 schemas := cMap["schemas"].(map[interface{}]interface{})
67 for typeName, typeDef := range schemas {
68 tDMap := typeDef.(map[interface{}]interface{})
69 anyOf, ok := tDMap["anyOf"]
71 aOSlice := anyOf.([]interface{})
73 mapInterface := aOSlice[0].(map[interface{}]interface{})
74 enumInterface := mapInterface["enum"]
75 if enumInterface != nil {
76 is := enumInterface.([]interface{})
78 for i := 0; i < len(is); i++ {
79 if reflect.TypeOf(is[i]).Kind() == reflect.String {
80 enumVals = append(enumVals, is[i].(string))
82 } else if reflect.TypeOf(is[1]).Kind() == reflect.Int {
83 enumVals = append(enumVals, strconv.Itoa(is[i].(int)))
86 correctEnum.Enum = enumVals
87 correctEnum.Type = "string"
88 description := tDMap["description"]
89 if description != nil {
90 correctEnum.Description = description.(string)
92 if aOSlice[1] != nil {
93 mapInterface = aOSlice[1].(map[interface{}]interface{})
94 description := mapInterface["description"]
95 if description != nil {
96 correctEnum.Description = description.(string)
100 schemas[typeName] = correctEnum
104 modM, err := yaml.Marshal(m)
106 log.Printf("yamlFile. Get err #%v ", err)
108 err = ioutil.WriteFile(path, modM, 0644)
110 log.Printf("yamlFile. Get err #%v ", err)