Skip to content

Commit 7c258d3

Browse files
authored
Fix duplicate struct issue (mholt#21)
Signed-off-by: Prasad Ghangal <[email protected]>
1 parent 2fade8a commit 7c258d3

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

testdata/example4.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
labels:
6+
app: nginx
7+
spec:
8+
replicas: 3
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.7.9
20+
ports:
21+
- containerPort: 80

yaml2go.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ type Yaml2Go struct {
1818
StructMap map[string]string
1919
}
2020

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+
2133
// AppendResult add lines to the result
2234
func (yg *Yaml2Go) AppendResult(structName string, line string) {
2335
yg.StructMap[structName] += line
@@ -47,8 +59,7 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
4759
return "", err
4860
}
4961

50-
yg.AppendResult("Yaml2Go", "// Yaml2Go\n")
51-
yg.AppendResult("Yaml2Go", "type Yaml2Go struct {\n")
62+
yg.NewStruct("Yaml2Go", "")
5263
for k, v := range obj {
5364
yg.Structify(structName, k, v, false)
5465
}
@@ -58,6 +69,7 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
5869
for _, value := range yg.StructMap {
5970
result += fmt.Sprintf("%s\n", value)
6071
}
72+
6173
// Convert result into go format
6274
goFormat, err := format.Source([]byte(result))
6375
if err != nil {
@@ -67,8 +79,11 @@ func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
6779
}
6880

6981
// Structify transforms map key values to struct fields
82+
// structName : parent struct name
83+
// k, v : fields in the struct
7084
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 {
7287
yg.AppendResult(structName, fmt.Sprintf("%s interface{} `yaml:\"%s\"`\n", goKeyFormat(k), k))
7388
return
7489
}
@@ -80,20 +95,20 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
8095
switch val := v.(type) {
8196
case map[interface{}]interface{}:
8297
key := goKeyFormat(k)
98+
newKey := key
8399
if !arrayElem {
84-
yg.AppendResult(structName, fmt.Sprintf("%s %s `yaml:\"%s\"`\n", key, key, k))
85100
// 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))
88103
}
89104
// If array of yaml objects
90105
for k1, v1 := range val {
91106
if _, ok := k1.(string); ok {
92-
yg.Structify(key, k1.(string), v1, false)
107+
yg.Structify(newKey, k1.(string), v1, false)
93108
}
94109
}
95110
if !arrayElem {
96-
yg.AppendResult(key, "}\n")
111+
yg.AppendResult(newKey, "}\n")
97112
}
98113
}
99114

@@ -111,14 +126,13 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
111126
// if nested object
112127
case map[interface{}]interface{}:
113128
key := goKeyFormat(k)
114-
yg.AppendResult(structName, fmt.Sprintf("%s []%s `yaml:\"%s\"`\n", key, key, k))
115129
// 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))
118132
for _, v1 := range val {
119-
yg.Structify(key, key, v1, true)
133+
yg.Structify(newKey, key, v1, true)
120134
}
121-
yg.AppendResult(key, "}\n")
135+
yg.AppendResult(newKey, "}\n")
122136
}
123137

124138
default:

0 commit comments

Comments
 (0)