-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
57 lines (45 loc) · 1.16 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gohtml
import "strings"
type StructDef struct {
Name string
Fields []Field
}
func addField(templateName string, structs []StructDef, field Field) []StructDef {
structName := strings.Join(field.Path, "")
// find and append struct (if it exists)
for i := range structs {
if structs[i].Name == structName {
fieldExists := false
for j, f := range structs[i].Fields {
if f.Name == field.Name {
fieldExists = true
// update to use the more specific type
if f.Type == "any" && field.Type != "any" {
structs[i].Fields[j].Type = field.Type
}
break
}
}
if !fieldExists {
structs[i].Fields = append(structs[i].Fields, field)
}
return structs
}
}
// create new struct def
structs = append(structs, StructDef{
Name: structName,
Fields: []Field{field},
})
// create missing links in data model
if len(field.Path) > 1 && !strings.HasPrefix(field.Type, "[]") {
parentField := Field{
Path: field.Path[:len(field.Path)-1],
Name: field.Path[len(field.Path)-1],
Type: "*" + structName,
}
// find and add field here
structs = addField(templateName, structs, parentField)
}
return structs
}