forked from freeconf/yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_gen_main.go
155 lines (147 loc) · 3.25 KB
/
core_gen_main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// +build ignore
// core_gen generates boilerplate functions for meta structs looking
// at specfic field names and generating functions based on that.
package main
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"text/template"
)
func main() {
elems := buildElements()
tmplFile, err := os.Open("core_gen.in")
if err != nil {
panic(err)
}
tmplSrc, err := ioutil.ReadAll(tmplFile)
if err != nil {
panic(err)
}
t, err := template.New("code_gen").Parse(string(tmplSrc))
if err != nil {
panic(err)
}
codeGen, err := os.Create("./core_gen.go")
if err != nil {
panic(err)
}
defer codeGen.Close()
if err := t.Execute(codeGen, elems); err != nil {
panic(err)
}
}
type elem struct {
Name string
Parent bool
Description bool
Ident bool
Extensions bool
SecondaryExtensions bool
Status bool
DataDefinitions bool
Groupings bool
Typedefs bool
Musts bool
When bool
IfFeatures bool
Actions bool
Notifications bool
Config bool
Mandatory bool
ListDetails bool
Type bool
Scope bool
Recursable bool
Clone bool
Augments bool
}
func buildElements() []*elem {
fset := token.NewFileSet()
src, err := parser.ParseFile(fset, "./core.go", nil, 0)
if err != nil {
panic(err)
}
var v visitor
ast.Walk(&v, src)
return v.elems
}
type visitor struct {
ident string
elem *elem
elems []*elem
}
func (v *visitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
switch d := n.(type) {
case *ast.Ident:
v.ident = d.Name
case *ast.StructType:
v.elem = &elem{
Name: v.ident,
}
v.elems = append(v.elems, v.elem)
switch v.elem.Name {
case "Rpc", "Must", "Leaf", "LeafList", "Any", "Uses", "Choice", "ChoiceCase":
v.elem.Clone = true
}
case *ast.Field:
// if specfic field names are found on structure, we flag
// this struct as such which activates parts of the template
// this assumes a specific field name convention is followed
if len(d.Names) > 0 {
switch d.Names[0].Name {
case "parent":
v.elem.Parent = true
case "ident":
v.elem.Ident = true
case "desc":
v.elem.Description = true
case "extensions":
v.elem.Extensions = true
case "secondaryExtensions":
v.elem.SecondaryExtensions = true
case "status":
v.elem.Status = true
case "dataDefs":
v.elem.DataDefinitions = true
v.elem.Clone = true
case "groupings":
v.elem.Groupings = true
case "typedefs":
v.elem.Typedefs = true
case "musts":
v.elem.Musts = true
case "when":
v.elem.When = true
case "ifs":
v.elem.IfFeatures = true
case "actions":
v.elem.Actions = true
case "notifications":
v.elem.Notifications = true
// assumes config too
case "mandatory":
v.elem.Mandatory = true
case "configPtr":
v.elem.Config = true
// assumes max and unbounded too
case "minElements":
v.elem.ListDetails = true
case "dtype":
v.elem.Type = true
case "scope":
v.elem.Scope = true
case "recursive":
v.elem.Recursable = true
case "augments":
v.elem.Augments = true
}
}
}
return v
}