X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=internal%2Fgentools%2Fenumfixer%2Fenumfixer.go;fp=internal%2Fgentools%2Fenumfixer%2Fenumfixer.go;h=0000000000000000000000000000000000000000;hb=c9e08b2a2f647f9f870040570c5e71305f0fb5d2;hp=b44c55c4d97e62834e24a2325caa9d00f8ed9e77;hpb=8fcfa4ecde6d95ead33c34b0e7efc4933ad44444;p=nonrtric%2Fplt%2Fsme.git diff --git a/internal/gentools/enumfixer/enumfixer.go b/internal/gentools/enumfixer/enumfixer.go deleted file mode 100644 index b44c55c..0000000 --- a/internal/gentools/enumfixer/enumfixer.go +++ /dev/null @@ -1,115 +0,0 @@ -// - -// ========================LICENSE_START================================= -// O-RAN-SC -// %% -// Copyright (C) 2022: Nordix Foundation -// %% -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ========================LICENSE_END=================================== -// - -package main - -import ( - "flag" - "fmt" - "io/ioutil" - "log" - "os" - "path/filepath" - "reflect" - "strconv" - "strings" - - "gopkg.in/yaml.v2" -) - -type Enum struct { - Enum []string `yaml:"enum"` - Type string `yaml:"type"` - Description string `yaml:"description"` -} - -func main() { - var apiDir = flag.String("apidir", "", "Directory containing API definitions to fix") - flag.Parse() - err := filepath.Walk(*apiDir, fixEnums) - if err != nil { - fmt.Println(err) - } -} - -func fixEnums(path string, info os.FileInfo, _ error) error { - if !info.IsDir() && strings.HasSuffix(info.Name(), ".yaml") { - yamlFile, err := ioutil.ReadFile(path) - if err != nil { - log.Printf("yamlFile. Get err #%v ", err) - } - m := make(map[string]interface{}) - err = yaml.Unmarshal(yamlFile, m) - if err != nil { - log.Fatalf("Unmarshal: %v", err) - } - components := m["components"] - if components != nil { - cMap := components.(map[interface{}]interface{}) - schemas := cMap["schemas"].(map[interface{}]interface{}) - for typeName, typeDef := range schemas { - tDMap := typeDef.(map[interface{}]interface{}) - anyOf, ok := tDMap["anyOf"] - if ok { - aOSlice := anyOf.([]interface{}) - correctEnum := Enum{} - mapInterface := aOSlice[0].(map[interface{}]interface{}) - enumInterface := mapInterface["enum"] - if enumInterface != nil { - is := enumInterface.([]interface{}) - var enumVals []string - for i := 0; i < len(is); i++ { - if reflect.TypeOf(is[i]).Kind() == reflect.String { - enumVals = append(enumVals, is[i].(string)) - - } else if reflect.TypeOf(is[1]).Kind() == reflect.Int { - enumVals = append(enumVals, strconv.Itoa(is[i].(int))) - } - } - correctEnum.Enum = enumVals - correctEnum.Type = "string" - description := tDMap["description"] - if description != nil { - correctEnum.Description = description.(string) - } else { - if aOSlice[1] != nil { - mapInterface = aOSlice[1].(map[interface{}]interface{}) - description := mapInterface["description"] - if description != nil { - correctEnum.Description = description.(string) - } - } - } - schemas[typeName] = correctEnum - } - } - } - modM, err := yaml.Marshal(m) - if err != nil { - log.Printf("yamlFile. Get err #%v ", err) - } - err = ioutil.WriteFile(path, modM, 0644) - if err != nil { - log.Printf("yamlFile. Get err #%v ", err) - } - } - } - return nil -}