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 if _, ok := cMap["schemas"].(map[interface{}]interface{}); ok {
67 schemas := cMap["schemas"].(map[interface{}]interface{})
68 for typeName, typeDef := range schemas {
69 tDMap := typeDef.(map[interface{}]interface{})
70 anyOf, ok := tDMap["anyOf"]
72 aOSlice := anyOf.([]interface{})
74 mapInterface := aOSlice[0].(map[interface{}]interface{})
75 enumInterface := mapInterface["enum"]
76 if enumInterface != nil {
77 is := enumInterface.([]interface{})
79 for i := 0; i < len(is); i++ {
80 if reflect.TypeOf(is[i]).Kind() == reflect.String {
81 enumVals = append(enumVals, is[i].(string))
83 } else if reflect.TypeOf(is[1]).Kind() == reflect.Int {
84 enumVals = append(enumVals, strconv.Itoa(is[i].(int)))
87 correctEnum.Enum = enumVals
88 correctEnum.Type = "string"
89 description := tDMap["description"]
90 if description != nil {
91 correctEnum.Description = description.(string)
93 if aOSlice[1] != nil {
94 mapInterface = aOSlice[1].(map[interface{}]interface{})
95 description := mapInterface["description"]
96 if description != nil {
97 correctEnum.Description = description.(string)
101 schemas[typeName] = correctEnum
105 modM, err := yaml.Marshal(m)
107 log.Printf("yamlFile. Get err #%v ", err)
109 err = ioutil.WriteFile(path, modM, 0644)
111 log.Printf("yamlFile. Get err #%v ", err)