Adding Capif provider
[nonrtric/plt/sme.git] / provider / 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                         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"]
71                                         if ok {
72                                                 aOSlice := anyOf.([]interface{})
73                                                 correctEnum := Enum{}
74                                                 mapInterface := aOSlice[0].(map[interface{}]interface{})
75                                                 enumInterface := mapInterface["enum"]
76                                                 if enumInterface != nil {
77                                                         is := enumInterface.([]interface{})
78                                                         var enumVals []string
79                                                         for i := 0; i < len(is); i++ {
80                                                                 if reflect.TypeOf(is[i]).Kind() == reflect.String {
81                                                                         enumVals = append(enumVals, is[i].(string))
82
83                                                                 } else if reflect.TypeOf(is[1]).Kind() == reflect.Int {
84                                                                         enumVals = append(enumVals, strconv.Itoa(is[i].(int)))
85                                                                 }
86                                                         }
87                                                         correctEnum.Enum = enumVals
88                                                         correctEnum.Type = "string"
89                                                         description := tDMap["description"]
90                                                         if description != nil {
91                                                                 correctEnum.Description = description.(string)
92                                                         } else {
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)
98                                                                         }
99                                                                 }
100                                                         }
101                                                         schemas[typeName] = correctEnum
102                                                 }
103                                         }
104                                 }
105                                 modM, err := yaml.Marshal(m)
106                                 if err != nil {
107                                         log.Printf("yamlFile. Get err   #%v ", err)
108                                 }
109                                 err = ioutil.WriteFile(path, modM, 0644)
110                                 if err != nil {
111                                         log.Printf("yamlFile. Get err   #%v ", err)
112                                 }
113                         }
114                 }
115         }
116         return nil
117 }