blob: d66c47249254b829efd5f44a7f113ee3a18cb01c [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbarie14088962017-06-01 16:56:55 -040016package main
17
18import (
19 "github.com/opencord/voltha/netconf/translator/voltha"
20 "reflect"
21 "fmt"
22)
23
24var StructMap map[string]map[string]string
25
26func init() {
27 StructMap = make(map[string]map[string]string)
28}
29
30func addArrayType(newType string) {
31 if _, ok := StructMap[newType+"Array"]; !ok {
32 StructMap[newType+"Array"] = make(map[string]string)
33 StructMap[newType+"Array"]["items"] = newType + "*"
34 StructMap[newType+"Array"]["size"] = "int"
35 }
36
37}
38
39func addEnumType(newType string) {
40 if _, ok := StructMap[newType]; !ok {
41 StructMap[newType] = make(map[string]string)
42 StructMap[newType]["Type"] = "enum " + newType + "Enum"
43 StructMap[newType]["Value"] = "char*"
44 }
45}
46
47func traverseVolthaStructures(original reflect.Value) string {
48 field_type := ""
49
50 switch original.Kind() {
51
52 case reflect.Ptr:
53 field_type = traverseVolthaStructures(original.Elem())
54
55 case reflect.Interface:
56 addEnumType(original.Type().Name())
57 field_type = original.Type().Name()
58
59 case reflect.Struct:
60 for i := 0; i < original.NumField(); i += 1 {
61 field_type = ""
62 if original.Field(i).Kind() == reflect.Ptr {
63 newType := reflect.New(original.Type().Field(i).Type.Elem())
64 traverseVolthaStructures(reflect.Indirect(newType))
65 field_type = original.Type().Field(i).Type.Elem().Name() + "*"
66 } else {
67 field_type = traverseVolthaStructures(original.Field(i))
68 }
69 if _, ok := StructMap[original.Type().Name()]; !ok {
70 StructMap[original.Type().Name()] = make(map[string]string)
71 }
72 if _, ok := StructMap[original.Type().Name()][original.Type().Field(i).Name]; !ok {
73 if field_type == "" {
74 StructMap[original.Type().Name()][original.Type().Field(i).Name] =
75 string(original.Type().Field(i).Type.String())
76 } else {
77 StructMap[original.Type().Name()][original.Type().Field(i).Name] =
78 field_type
79 }
80 }
81 }
82
83 case reflect.Slice:
84 if original.Type().Elem().Kind() == reflect.Ptr {
85 newType := reflect.New(original.Type().Elem().Elem())
86 field_type = newType.Type().Elem().Name() + "Array"
87 traverseVolthaStructures(reflect.Indirect(newType))
88 addArrayType(newType.Type().Elem().Name())
89 } else {
90 field_type = original.Type().Elem().Kind().String() + "Array"
91 addArrayType(original.Type().Elem().Kind().String())
92 }
93
94 //case reflect.Map:
95 // for _, key := range original.MapKeys() {
96 // originalValue := original.MapIndex(key)
97 // }
98
99 case reflect.String:
100 field_type = "string"
101 break
102
103 default:
104 field_type = original.Kind().String()
105 }
106
107 return field_type
108}
109
110func main() {
111 traverseVolthaStructures(reflect.ValueOf(voltha.Voltha{}))
112
113 fmt.Printf("#ifndef VOLTHA_DEFS\n")
114 fmt.Printf("#define VOLTHA_DEFS\n")
115 fmt.Printf("\n#include <stdint.h>\n")
116 var attribute_type string
117 for k, v := range StructMap {
118 fmt.Printf("\ntypedef struct {\n")
119 for kk, vv := range v {
120 attribute_type = vv
121 switch vv {
122 case "string*":
123 attribute_type = "char**"
124 case "string":
125 attribute_type = "char*"
126 case "int32":
127 attribute_type = "int32_t"
128 case "uint8*":
129 fallthrough
130 case "uint8":
131 attribute_type = "uint8_t"
132 case "uint32*":
133 fallthrough
134 case "uint32":
135 attribute_type = "uint32_t"
136 case "uint64*":
137 fallthrough
138 case "uint64":
139 attribute_type = "uint64_t"
140 case "bool":
141 attribute_type = "int"
142 }
143 fmt.Printf("\t%s %s;\n", attribute_type, kk)
144 }
145 fmt.Printf("} %s;\n", k)
146 }
147 fmt.Printf("\n#endif\n")
148}