Skip to content

Commit 8afaa4b

Browse files
committed
Merge pull request #56 from cbusbey/uber_gen
massive code gen
2 parents e7415db + e718ce3 commit 8afaa4b

File tree

1,104 files changed

+61800
-398235
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,104 files changed

+61800
-398235
lines changed

_gen/generate-components/main.go

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
var (
1515
pkg string
1616
fixSpec *datadictionary.DataDictionary
17+
imports map[string]bool
1718
)
1819

1920
func usage() {
@@ -36,12 +37,29 @@ func packageString() (s string) {
3637
}
3738

3839
func writeField(field *datadictionary.FieldDef, componentName string) (s string) {
40+
if field.IsComponent() {
41+
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Component.Name))] = true
42+
43+
s += fmt.Sprintf("//%v Component\n", field.Component.Name)
44+
s += fmt.Sprintf("%v %v.Component\n", field.Component.Name, strings.ToLower(field.Component.Name))
45+
return
46+
}
47+
3948
if field.Required {
4049
s += fmt.Sprintf("//%v is a required field for %v.\n", field.Name, componentName)
4150
} else {
4251
s += fmt.Sprintf("//%v is a non-required field for %v.\n", field.Name, componentName)
4352
}
4453

54+
if field.IsGroup() {
55+
if field.Required {
56+
s += fmt.Sprintf("%v []%v `fix:\"%v\"`\n", field.Name, field.Name, field.Tag)
57+
} else {
58+
s += fmt.Sprintf("%v []%v `fix:\"%v,omitempty\"`\n", field.Name, field.Name, field.Tag)
59+
}
60+
return
61+
}
62+
4563
goType := ""
4664
switch field.Type {
4765
case "MULTIPLESTRINGVALUE", "MULTIPLEVALUESTRING":
@@ -70,6 +88,8 @@ func writeField(field *datadictionary.FieldDef, componentName string) (s string)
7088
fallthrough
7189
case "UTCDATEONLY":
7290
fallthrough
91+
case "UTCDATE":
92+
fallthrough
7393
case "TZTIMEONLY":
7494
fallthrough
7595
case "TZTIMESTAMP":
@@ -94,6 +114,7 @@ func writeField(field *datadictionary.FieldDef, componentName string) (s string)
94114
case "TIME":
95115
fallthrough
96116
case "UTCTIMESTAMP":
117+
imports["time"] = true
97118
goType = "time.Time"
98119

99120
case "QTY":
@@ -130,26 +151,85 @@ func writeField(field *datadictionary.FieldDef, componentName string) (s string)
130151
return
131152
}
132153

133-
func genComponentImports() string {
134-
fileOut := "import \"time\"\n"
135-
return fileOut
154+
func genComponentImports() (fileOut string) {
155+
156+
if len(imports) == 0 {
157+
return
158+
}
159+
160+
fileOut += "import(\n"
161+
for i, _ := range imports {
162+
fileOut += fmt.Sprintf("\"%v\"\n", i)
163+
}
164+
fileOut += ")\n"
165+
166+
return
167+
}
168+
169+
type group struct {
170+
parent string
171+
field *datadictionary.FieldDef
172+
}
173+
174+
func collectGroups(parent string, field *datadictionary.FieldDef, groups []group) []group {
175+
if !field.IsGroup() {
176+
return groups
177+
}
178+
179+
groups = append(groups, group{parent, field})
180+
for _, childField := range field.ChildFields {
181+
groups = collectGroups(field.Name, childField, groups)
182+
}
183+
184+
return groups
185+
}
186+
187+
func genGroupDeclaration(field *datadictionary.FieldDef, parent string) (fileOut string) {
188+
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name, parent)
189+
fileOut += fmt.Sprintf("type %v struct {\n", field.Name)
190+
for _, groupField := range field.ChildFields {
191+
fileOut += writeField(groupField, field.Name)
192+
}
193+
194+
fileOut += "}\n"
195+
196+
return
197+
}
198+
199+
func genGroupDeclarations(name string, fields []*datadictionary.FieldDef) (fileOut string) {
200+
groups := []group{}
201+
for _, field := range fields {
202+
groups = collectGroups(name, field, groups)
203+
}
204+
205+
for _, group := range groups {
206+
fileOut += genGroupDeclaration(group.field, group.parent)
207+
}
208+
209+
return
136210
}
137211

138212
func genHeader(header *datadictionary.MessageDef) {
139-
fileOut := packageString()
140-
fileOut += genComponentImports()
213+
imports = make(map[string]bool)
141214

142-
fileOut += fmt.Sprintf("//Header is the %v Header type\n", pkg)
143-
fileOut += "type Header struct {\n"
215+
//delay field output to determine imports
216+
delayOut := genGroupDeclarations("Header", header.FieldsInDeclarationOrder)
217+
delayOut += fmt.Sprintf("//Header is the %v Header type\n", pkg)
218+
delayOut += "type Header struct {\n"
144219
for _, field := range header.FieldsInDeclarationOrder {
145-
fileOut += writeField(field, "Header")
220+
delayOut += writeField(field, "Header")
146221
}
147-
fileOut += "}\n"
222+
delayOut += "}\n"
223+
224+
fileOut := packageString()
225+
fileOut += genComponentImports()
226+
fileOut += delayOut
148227

149228
gen.WriteFile(path.Join(pkg, "header.go"), fileOut)
150229
}
151230

152231
func genTrailer(trailer *datadictionary.MessageDef) {
232+
imports = make(map[string]bool)
153233
fileOut := packageString()
154234
fileOut += fmt.Sprintf("//Trailer is the %v Trailer type\n", pkg)
155235
fileOut += "type Trailer struct {\n"
@@ -161,6 +241,26 @@ func genTrailer(trailer *datadictionary.MessageDef) {
161241
gen.WriteFile(path.Join(pkg, "trailer.go"), fileOut)
162242
}
163243

244+
func genComponent(name string, component *datadictionary.Component) {
245+
imports = make(map[string]bool)
246+
247+
//delay output to determine imports
248+
delayOut := genGroupDeclarations(name, component.Fields)
249+
delayOut += fmt.Sprintf("//Component is a %v %v Component\n", pkg, name)
250+
delayOut += "type Component struct {\n"
251+
for _, field := range component.Fields {
252+
delayOut += writeField(field, name)
253+
}
254+
delayOut += "}\n"
255+
256+
fileOut := fmt.Sprintf("package %v\n", strings.ToLower(name))
257+
fileOut += genComponentImports()
258+
fileOut += delayOut
259+
fileOut += "func New() *Component { return new(Component)}\n"
260+
261+
gen.WriteFile(path.Join(pkg, strings.ToLower(name), name+".go"), fileOut)
262+
}
263+
164264
func main() {
165265
flag.Usage = usage
166266
flag.Parse()
@@ -193,4 +293,8 @@ func main() {
193293
genHeader(fixSpec.Header)
194294
genTrailer(fixSpec.Trailer)
195295
}
296+
297+
for name, component := range fixSpec.Components {
298+
genComponent(name, component)
299+
}
196300
}

_gen/generate-fields/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func genFields() {
7777
fallthrough
7878
case "LOCALMKTDATE":
7979
fallthrough
80+
case "TIME":
81+
fallthrough
82+
case "DATE":
83+
fallthrough
8084
case "EXCHANGE":
8185
fallthrough
8286
case "LANGUAGE":
@@ -179,6 +183,7 @@ func main() {
179183
spec, err := datadictionary.Parse(dataDict)
180184

181185
if err != nil {
186+
fmt.Println(dataDict)
182187
panic(err)
183188
}
184189

0 commit comments

Comments
 (0)