-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
149 lines (125 loc) · 2.79 KB
/
source.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
package gen
import (
"bytes"
"fmt"
"go/format"
"io"
"strings"
)
type Source struct {
//CodeBase include the member definitions in Base
*CodeBase
//dir specifies the location of the source file.
dir string
//pkg specifies the package name inside which the source file will be created.
pkg string
//name of the source file
name string
//structs has the list of structures in this source file.
structs map[string]*Struct
functions map[string]*Function
//imports
imports []*Import // TODO handle imports as is can be duplicate need to handle during generation
}
type Import struct {
PkgName string
Alias string
}
// AddImport function will add import to the source file.
func (s *Source) AddImport(pkgName, alias string) *Source {
i := &Import{
PkgName: pkgName,
Alias: alias,
}
s.imports = append(s.imports, i)
return s
}
//Struct creates a new struct for the source.
func (s *Source) Struct(name string) *Struct {
if str, ok := s.structs[name]; ok {
return str
} else {
strct := &Struct{
name: name,
members: make(map[string]*members),
source: s,
Base: &Base{
GoGen: s.GoGen,
Comment: "",
},
}
s.structs[name] = strct
return strct
}
}
func (s *Source) Func(name string) *Function {
if f, ok := s.functions[name]; ok {
return f
} else {
f := &Function{
funcSignature: &funcSignature{
fnName: name,
args: nil,
receiver: nil,
ret: nil,
Base: &Base{
GoGen: s.GoGen,
Comment: "",
},
},
funcBody: nil,
}
s.functions[name] = f
return f
}
}
func (s *Source) Generate(writer io.Writer) {
var buf bytes.Buffer
s.WriteComments(&buf)
buf.WriteString("package ")
buf.WriteString(s.pkg)
//TODO update the new line with const in all source files
buf.WriteString("\n\n")
if len(s.imports) > 0 {
buf.WriteString("import (\n") //import open
cleanImports(s.imports)
for _, i := range s.imports {
if len(i.Alias) > 0 {
buf.WriteString(i.Alias)
buf.WriteString(" ")
}
buf.WriteString(i.PkgName)
buf.WriteString("\n")
}
buf.WriteString(")\n") // import close
}
// Prepare and write Constants
if len(s.ConstantsSlice) > 0 {
//for _, c := range s.Constants {
//
//}
}
// Prepare and write structs
for n, str := range s.structs {
//TODO update println with log statements
fmt.Println("generating struct defn for struct name " + n)
b := new(strings.Builder)
str.Generate(b)
buf.WriteString(b.String())
}
buf.WriteString("\n\n")
// Prepare and write functions
for n, fn := range s.functions {
fmt.Println("generating function with name " + n)
b := new(strings.Builder)
fn.Generate(b)
buf.WriteString(b.String())
}
p, err := format.Source(buf.Bytes())
if err != nil {
panic(err.Error())
}
writer.Write(p)
}
func cleanImports(i []*Import) {
}