@@ -18,6 +18,18 @@ type Yaml2Go struct {
18
18
StructMap map [string ]string
19
19
}
20
20
21
+ // NewStruct creates new entry in StructMap result
22
+ func (yg * Yaml2Go ) NewStruct (structName string , parent string ) string {
23
+ // If struct already present with the same name
24
+ // rename struct to ParentStructname
25
+ if _ , ok := yg .StructMap [structName ]; ok {
26
+ structName = goKeyFormat (parent ) + structName
27
+ }
28
+ yg .AppendResult (structName , fmt .Sprintf ("// %s\n " , structName ))
29
+ yg .StructMap [structName ] += fmt .Sprintf ("type %s struct {\n " , structName )
30
+ return structName
31
+ }
32
+
21
33
// AppendResult add lines to the result
22
34
func (yg * Yaml2Go ) AppendResult (structName string , line string ) {
23
35
yg .StructMap [structName ] += line
@@ -47,8 +59,7 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
47
59
return "" , err
48
60
}
49
61
50
- yg .AppendResult ("Yaml2Go" , "// Yaml2Go\n " )
51
- yg .AppendResult ("Yaml2Go" , "type Yaml2Go struct {\n " )
62
+ yg .NewStruct ("Yaml2Go" , "" )
52
63
for k , v := range obj {
53
64
yg .Structify (structName , k , v , false )
54
65
}
@@ -58,6 +69,7 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
58
69
for _ , value := range yg .StructMap {
59
70
result += fmt .Sprintf ("%s\n " , value )
60
71
}
72
+
61
73
// Convert result into go format
62
74
goFormat , err := format .Source ([]byte (result ))
63
75
if err != nil {
@@ -67,8 +79,11 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
67
79
}
68
80
69
81
// Structify transforms map key values to struct fields
82
+ // structName : parent struct name
83
+ // k, v : fields in the struct
70
84
func (yg * Yaml2Go ) Structify (structName , k string , v interface {}, arrayElem bool ) {
71
- if reflect .TypeOf (v ) == nil {
85
+
86
+ if reflect .TypeOf (v ) == nil || len (k ) == 0 {
72
87
yg .AppendResult (structName , fmt .Sprintf ("%s interface{} `yaml:\" %s\" `\n " , goKeyFormat (k ), k ))
73
88
return
74
89
}
@@ -80,20 +95,20 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
80
95
switch val := v .(type ) {
81
96
case map [interface {}]interface {}:
82
97
key := goKeyFormat (k )
98
+ newKey := key
83
99
if ! arrayElem {
84
- yg .AppendResult (structName , fmt .Sprintf ("%s %s `yaml:\" %s\" `\n " , key , key , k ))
85
100
// Create new structure
86
- yg .AppendResult (key , fmt . Sprintf ( "// %s \n " , key ) )
87
- yg .AppendResult (key , fmt .Sprintf ("type %s struct { \ n " , key ))
101
+ newKey = yg .NewStruct (key , structName )
102
+ yg .AppendResult (structName , fmt .Sprintf ("%s %s `yaml: \" %s \" ` \ n " , key , newKey , k ))
88
103
}
89
104
// If array of yaml objects
90
105
for k1 , v1 := range val {
91
106
if _ , ok := k1 .(string ); ok {
92
- yg .Structify (key , k1 .(string ), v1 , false )
107
+ yg .Structify (newKey , k1 .(string ), v1 , false )
93
108
}
94
109
}
95
110
if ! arrayElem {
96
- yg .AppendResult (key , "}\n " )
111
+ yg .AppendResult (newKey , "}\n " )
97
112
}
98
113
}
99
114
@@ -111,14 +126,13 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
111
126
// if nested object
112
127
case map [interface {}]interface {}:
113
128
key := goKeyFormat (k )
114
- yg .AppendResult (structName , fmt .Sprintf ("%s []%s `yaml:\" %s\" `\n " , key , key , k ))
115
129
// Create new structure
116
- yg .AppendResult (key , fmt . Sprintf ( "// %s \n " , key ) )
117
- yg .AppendResult (key , fmt .Sprintf ("type %s struct { \ n " , key ))
130
+ newKey := yg .NewStruct (key , structName )
131
+ yg .AppendResult (structName , fmt .Sprintf ("%s [] %s `yaml: \" %s \" ` \ n " , key , newKey , k ))
118
132
for _ , v1 := range val {
119
- yg .Structify (key , key , v1 , true )
133
+ yg .Structify (newKey , key , v1 , true )
120
134
}
121
- yg .AppendResult (key , "}\n " )
135
+ yg .AppendResult (newKey , "}\n " )
122
136
}
123
137
124
138
default :
0 commit comments