blob: c84fdda199678abe9e6b190018545244b516ac3f [file] [log] [blame]
Stephane Barbarie14088962017-06-01 16:56:55 -04001package main
2
3import (
4 "github.com/opencord/voltha/netconf/translator/voltha"
5 "reflect"
6 "fmt"
7)
8
9var StructMap map[string]map[string]string
10
11func init() {
12 StructMap = make(map[string]map[string]string)
13}
14
15func addArrayType(newType string) {
16 if _, ok := StructMap[newType+"Array"]; !ok {
17 StructMap[newType+"Array"] = make(map[string]string)
18 StructMap[newType+"Array"]["items"] = newType + "*"
19 StructMap[newType+"Array"]["size"] = "int"
20 }
21
22}
23
24func addEnumType(newType string) {
25 if _, ok := StructMap[newType]; !ok {
26 StructMap[newType] = make(map[string]string)
27 StructMap[newType]["Type"] = "enum " + newType + "Enum"
28 StructMap[newType]["Value"] = "char*"
29 }
30}
31
32func traverseVolthaStructures(original reflect.Value) string {
33 field_type := ""
34
35 switch original.Kind() {
36
37 case reflect.Ptr:
38 field_type = traverseVolthaStructures(original.Elem())
39
40 case reflect.Interface:
41 addEnumType(original.Type().Name())
42 field_type = original.Type().Name()
43
44 case reflect.Struct:
45 for i := 0; i < original.NumField(); i += 1 {
46 field_type = ""
47 if original.Field(i).Kind() == reflect.Ptr {
48 newType := reflect.New(original.Type().Field(i).Type.Elem())
49 traverseVolthaStructures(reflect.Indirect(newType))
50 field_type = original.Type().Field(i).Type.Elem().Name() + "*"
51 } else {
52 field_type = traverseVolthaStructures(original.Field(i))
53 }
54 if _, ok := StructMap[original.Type().Name()]; !ok {
55 StructMap[original.Type().Name()] = make(map[string]string)
56 }
57 if _, ok := StructMap[original.Type().Name()][original.Type().Field(i).Name]; !ok {
58 if field_type == "" {
59 StructMap[original.Type().Name()][original.Type().Field(i).Name] =
60 string(original.Type().Field(i).Type.String())
61 } else {
62 StructMap[original.Type().Name()][original.Type().Field(i).Name] =
63 field_type
64 }
65 }
66 }
67
68 case reflect.Slice:
69 if original.Type().Elem().Kind() == reflect.Ptr {
70 newType := reflect.New(original.Type().Elem().Elem())
71 field_type = newType.Type().Elem().Name() + "Array"
72 traverseVolthaStructures(reflect.Indirect(newType))
73 addArrayType(newType.Type().Elem().Name())
74 } else {
75 field_type = original.Type().Elem().Kind().String() + "Array"
76 addArrayType(original.Type().Elem().Kind().String())
77 }
78
79 //case reflect.Map:
80 // for _, key := range original.MapKeys() {
81 // originalValue := original.MapIndex(key)
82 // }
83
84 case reflect.String:
85 field_type = "string"
86 break
87
88 default:
89 field_type = original.Kind().String()
90 }
91
92 return field_type
93}
94
95func main() {
96 traverseVolthaStructures(reflect.ValueOf(voltha.Voltha{}))
97
98 fmt.Printf("#ifndef VOLTHA_DEFS\n")
99 fmt.Printf("#define VOLTHA_DEFS\n")
100 fmt.Printf("\n#include <stdint.h>\n")
101 var attribute_type string
102 for k, v := range StructMap {
103 fmt.Printf("\ntypedef struct {\n")
104 for kk, vv := range v {
105 attribute_type = vv
106 switch vv {
107 case "string*":
108 attribute_type = "char**"
109 case "string":
110 attribute_type = "char*"
111 case "int32":
112 attribute_type = "int32_t"
113 case "uint8*":
114 fallthrough
115 case "uint8":
116 attribute_type = "uint8_t"
117 case "uint32*":
118 fallthrough
119 case "uint32":
120 attribute_type = "uint32_t"
121 case "uint64*":
122 fallthrough
123 case "uint64":
124 attribute_type = "uint64_t"
125 case "bool":
126 attribute_type = "int"
127 }
128 fmt.Printf("\t%s %s;\n", attribute_type, kk)
129 }
130 fmt.Printf("} %s;\n", k)
131 }
132 fmt.Printf("\n#endif\n")
133}