/
buildergenerator.go
165 lines (146 loc) · 3.85 KB
/
buildergenerator.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
156
157
158
159
160
161
162
163
164
165
package main
import (
"bufio"
"bytes"
"flag"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"strings"
"text/template"
)
//Struct defines the field set array for the struct
type Struct struct {
Name string
Fields []Field
}
//Field defines the Fields pf
type Field struct {
FieldName string
FieldType string
}
var (
fileName = flag.String("f", "", "The source file path")
structName = flag.String("t", "", "The name of the struct for which the files has to be generated")
)
func getTemplate() (temp *template.Template, err error) {
var hanlder = `
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
// Autogenerated by buildergenerator
package goexoml
{{$name := .Name}}
{{range $index, $field := .Fields}}
//Set{{$field.FieldName}} sets {{$field.FieldName}} for {{$name}} struct instance
func ({{$name | receiver}} *{{$name}}) Set{{$field.FieldName}}({{$field.FieldName | lower}} {{$field.FieldType}}) *{{$name}}{
{{$name | receiver}}.{{$field.FieldName}} = {{$field.FieldName | lower}}
return {{$name | receiver}}
}
{{end}}
//New{{.Name}} return a new {{.Name}} pointer
func New{{.Name}}() *{{.Name}} {
return new({{.Name}})
}
//I{{.Name}} The interface that satisfies all the methods for this struct
//I{{.Name}} asserts implementation of setters for all the fields of {{.Name}}
type I{{.Name}} interface {
{{range $index, $field := .Fields}}Set{{$field.FieldName}}({{$field.FieldName | lower}} {{$field.FieldType}}) *{{$name}};{{end}}
}
//Add{{.Name}} appends the verb to response
func (r *Response) Add{{.Name}}({{.Name|lower}} I{{.Name}}) *Response {
r.Response = append(r.Response,{{.Name|lower}})
return r
}
`
// Create a new template and parse the letter into it.
t := template.
Must(template.
New("builder").
Funcs(template.FuncMap{"lower": strings.ToLower, "receiver": func(s string) string { return "__" + strings.ToLower(s) + "__" }}).
Parse(hanlder))
temp = t
return
}
func generateCode(t *template.Template, str Struct) (code []byte, err error) {
var bW bytes.Buffer
w := bufio.NewWriter(&bW)
err = t.Execute(w, str)
if err != nil {
return
}
err = w.Flush()
if err != nil {
return
}
//format the bytes from bufffer
code, err = format.Source(bW.Bytes())
return
}
func generateBuilder(str Struct) (err error) {
var t *template.Template
t, err = getTemplate()
if err != nil {
return
}
//create the template into a bytes Buffer
var code []byte
code, err = generateCode(t, str)
if err != nil {
return
}
//write to a file
var f *os.File
f, err = os.Create(strings.ToLower(str.Name) + "_builder.go")
if err != nil {
return
}
_, err = bytes.NewReader(code).WriteTo(f)
if err != nil {
return
}
return
}
func structFields(structName string, f *ast.File, src string) (fs []Field) {
fields := f.Scope.Lookup(structName).Decl.(*ast.TypeSpec).Type.(*ast.StructType).Fields
for _, field := range fields.List {
if field.Names[0].String() != "XMLName" {
fs = append(fs, Field{FieldName: field.Names[0].String(), FieldType: src[field.Type.Pos()-1 : field.Type.End()]})
}
}
return
}
func getStructInfo(filename string, structName string) (str Struct, err error) {
var bf bytes.Buffer
fset := token.NewFileSet()
var file *os.File
file, err = os.Open(filename)
if err != nil {
return
}
bf.ReadFrom(file)
b := bf.Bytes()
var f *ast.File
f, err = parser.ParseFile(fset, "", b, 0)
if err != nil {
return
}
str.Name = structName
str.Fields = structFields(structName, f, string(b))
return
}
func createStruct() (str Struct, err error) {
flag.Parse()
str, err = getStructInfo(*fileName, *structName)
return
}
func main() {
str, err := createStruct()
if err != nil {
log.Fatal("Error happened creating the resource information", err.Error())
}
if err = generateBuilder(str); err != nil {
log.Fatalln("Error happened generating the builder ", err.Error())
}
}