Add generated code
[nonrtric/plt/sme.git] / internal / gentools / enumfixer / enumfixer.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package main
22
23 import (
24         "flag"
25         "fmt"
26         "io/ioutil"
27         "log"
28         "os"
29         "path/filepath"
30         "reflect"
31         "strconv"
32         "strings"
33
34         "gopkg.in/yaml.v2"
35 )
36
37 type Enum struct {
38         Enum        []string `yaml:"enum"`
39         Type        string   `yaml:"type"`
40         Description string   `yaml:"description"`
41 }
42
43 func main() {
44         var apiDir = flag.String("apidir", "", "Directory containing API definitions to fix")
45         flag.Parse()
46         err := filepath.Walk(*apiDir, fixEnums)
47         if err != nil {
48                 fmt.Println(err)
49         }
50 }
51
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)
55                 if err != nil {
56                         log.Printf("yamlFile. Get err   #%v ", err)
57                 }
58                 m := make(map[string]interface{})
59                 err = yaml.Unmarshal(yamlFile, m)
60                 if err != nil {
61                         log.Fatalf("Unmarshal: %v", err)
62                 }
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"]
70                                 if ok {
71                                         aOSlice := anyOf.([]interface{})
72                                         correctEnum := Enum{}
73                                         mapInterface := aOSlice[0].(map[interface{}]interface{})
74                                         enumInterface := mapInterface["enum"]
75                                         if enumInterface != nil {
76                                                 is := enumInterface.([]interface{})
77                                                 var enumVals []string
78                                                 for i := 0; i < len(is); i++ {
79                                                         if reflect.TypeOf(is[i]).Kind() == reflect.String {
80                                                                 enumVals = append(enumVals, is[i].(string))
81
82                                                         } else if reflect.TypeOf(is[1]).Kind() == reflect.Int {
83                                                                 enumVals = append(enumVals, strconv.Itoa(is[i].(int)))
84                                                         }
85                                                 }
86                                                 correctEnum.Enum = enumVals
87                                                 correctEnum.Type = "string"
88                                                 description := tDMap["description"]
89                                                 if description != nil {
90                                                         correctEnum.Description = description.(string)
91                                                 } else {
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)
97                                                                 }
98                                                         }
99                                                 }
100                                                 schemas[typeName] = correctEnum
101                                         }
102                                 }
103                         }
104                         modM, err := yaml.Marshal(m)
105                         if err != nil {
106                                 log.Printf("yamlFile. Get err   #%v ", err)
107                         }
108                         err = ioutil.WriteFile(path, modM, 0644)
109                         if err != nil {
110                                 log.Printf("yamlFile. Get err   #%v ", err)
111                         }
112                 }
113         }
114         return nil
115 }