Skip to content

Commit 95f3627

Browse files
committed
Merge pull request #74 from cbusbey/gen_setters
Gen setters
2 parents 1f1bff0 + 4ac9ac6 commit 95f3627

File tree

1,108 files changed

+22330
-5701
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,108 files changed

+22330
-5701
lines changed

_gen/generate-components/main.go

Lines changed: 45 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
67
"github.com/quickfixgo/quickfix/_gen"
@@ -36,129 +37,14 @@ func packageString() (s string) {
3637
return
3738
}
3839

39-
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-
48-
if field.Required {
49-
s += fmt.Sprintf("//%v is a required field for %v.\n", field.Name, componentName)
50-
} else {
51-
s += fmt.Sprintf("//%v is a non-required field for %v.\n", field.Name, componentName)
52-
}
53-
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-
63-
goType := ""
64-
switch field.Type {
65-
case "MULTIPLESTRINGVALUE", "MULTIPLEVALUESTRING":
66-
fallthrough
67-
case "MULTIPLECHARVALUE":
68-
fallthrough
69-
case "CHAR":
70-
fallthrough
71-
case "CURRENCY":
72-
fallthrough
73-
case "DATA":
74-
fallthrough
75-
case "MONTHYEAR":
76-
fallthrough
77-
case "LOCALMKTDATE":
78-
fallthrough
79-
case "EXCHANGE":
80-
fallthrough
81-
case "LANGUAGE":
82-
fallthrough
83-
case "XMLDATA":
84-
fallthrough
85-
case "COUNTRY":
86-
fallthrough
87-
case "UTCTIMEONLY":
88-
fallthrough
89-
case "UTCDATEONLY":
90-
fallthrough
91-
case "UTCDATE":
92-
fallthrough
93-
case "TZTIMEONLY":
94-
fallthrough
95-
case "TZTIMESTAMP":
96-
fallthrough
97-
case "STRING":
98-
goType = "string"
99-
100-
case "BOOLEAN":
101-
goType = "bool"
102-
103-
case "LENGTH":
104-
fallthrough
105-
case "DAYOFMONTH":
106-
fallthrough
107-
case "NUMINGROUP":
108-
fallthrough
109-
case "SEQNUM":
110-
fallthrough
111-
case "INT":
112-
goType = "int"
113-
114-
case "TIME":
115-
fallthrough
116-
case "UTCTIMESTAMP":
117-
imports["time"] = true
118-
goType = "time.Time"
119-
120-
case "QTY":
121-
fallthrough
122-
case "AMT":
123-
fallthrough
124-
case "PRICE":
125-
fallthrough
126-
case "PRICEOFFSET":
127-
fallthrough
128-
case "PERCENTAGE":
129-
fallthrough
130-
case "FLOAT":
131-
goType = "float64"
132-
133-
default:
134-
fmt.Printf("Unknown type '%v' for tag '%v'\n", field.Type, field.Tag)
135-
}
136-
137-
fixTags := strconv.Itoa(field.Tag)
138-
if field.Tag == 8 {
139-
if fixSpec.Major == 4 {
140-
fixTags = fmt.Sprintf("%v,default=FIX.%v.%v", fixTags, fixSpec.Major, fixSpec.Minor)
141-
} else {
142-
fixTags = fixTags + ",default=FIXT.1.1"
143-
}
144-
}
145-
146-
if field.Required {
147-
s += fmt.Sprintf("%v %v `fix:\"%v\"`\n", field.Name, goType, fixTags)
148-
} else {
149-
s += fmt.Sprintf("%v *%v `fix:\"%v\"`\n", field.Name, goType, fixTags)
150-
}
151-
return
152-
}
153-
15440
func genComponentImports() (fileOut string) {
15541

15642
if len(imports) == 0 {
15743
return
15844
}
15945

16046
fileOut += "import(\n"
161-
for i, _ := range imports {
47+
for i := range imports {
16248
fileOut += fmt.Sprintf("\"%v\"\n", i)
16349
}
16450
fileOut += ")\n"
@@ -184,11 +70,25 @@ func collectGroups(parent string, field *datadictionary.FieldDef, groups []group
18470
return groups
18571
}
18672

73+
func writeFieldDeclaration(field *datadictionary.FieldDef, componentName string) string {
74+
switch {
75+
case field.IsComponent():
76+
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Component.Name))] = true
77+
case !field.IsGroup():
78+
goType := gen.FixFieldTypeToGoType(field.Type)
79+
if goType == "time.Time" {
80+
imports["time"] = true
81+
}
82+
}
83+
84+
return gen.WriteFieldDeclaration(fixSpec.Major, fixSpec.Minor, field, componentName)
85+
}
86+
18787
func genGroupDeclaration(field *datadictionary.FieldDef, parent string) (fileOut string) {
18888
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name, parent)
18989
fileOut += fmt.Sprintf("type %v struct {\n", field.Name)
19090
for _, groupField := range field.ChildFields {
191-
fileOut += writeField(groupField, field.Name)
91+
fileOut += writeFieldDeclaration(groupField, field.Name)
19292
}
19393

19494
fileOut += "}\n"
@@ -217,14 +117,20 @@ func genHeader(header *datadictionary.MessageDef) {
217117
delayOut += fmt.Sprintf("//Header is the %v Header type\n", pkg)
218118
delayOut += "type Header struct {\n"
219119
for _, field := range header.FieldsInDeclarationOrder {
220-
delayOut += writeField(field, "Header")
120+
delayOut += writeFieldDeclaration(field, "Header")
221121
}
222122
delayOut += "}\n"
223123

224124
fileOut := packageString()
225125
fileOut += genComponentImports()
226126
fileOut += delayOut
227127

128+
writer := new(bytes.Buffer)
129+
if err := gen.WriteFieldSetters(writer, "Header", header.FieldsInDeclarationOrder); err != nil {
130+
panic(err)
131+
}
132+
fileOut += writer.String()
133+
228134
gen.WriteFile(path.Join(pkg, "header.go"), fileOut)
229135
}
230136

@@ -234,10 +140,16 @@ func genTrailer(trailer *datadictionary.MessageDef) {
234140
fileOut += fmt.Sprintf("//Trailer is the %v Trailer type\n", pkg)
235141
fileOut += "type Trailer struct {\n"
236142
for _, field := range trailer.FieldsInDeclarationOrder {
237-
fileOut += writeField(field, "Trailer")
143+
fileOut += writeFieldDeclaration(field, "Trailer")
238144
}
239145
fileOut += "}\n"
240146

147+
writer := new(bytes.Buffer)
148+
if err := gen.WriteFieldSetters(writer, "Trailer", trailer.FieldsInDeclarationOrder); err != nil {
149+
panic(err)
150+
}
151+
fileOut += writer.String()
152+
241153
gen.WriteFile(path.Join(pkg, "trailer.go"), fileOut)
242154
}
243155

@@ -246,21 +158,31 @@ func genComponent(name string, component *datadictionary.Component) {
246158

247159
//delay output to determine imports
248160
delayOut := genGroupDeclarations(name, component.Fields)
249-
delayOut += fmt.Sprintf("//Component is a %v %v Component\n", pkg, name)
250-
delayOut += "type Component struct {\n"
161+
delayOut += fmt.Sprintf("//%v is a %v Component\n", name, pkg)
162+
delayOut += fmt.Sprintf("type %v struct {\n", name)
251163
for _, field := range component.Fields {
252-
delayOut += writeField(field, name)
164+
delayOut += writeFieldDeclaration(field, name)
253165
}
254166
delayOut += "}\n"
255167

256168
fileOut := fmt.Sprintf("package %v\n", strings.ToLower(name))
257169
fileOut += genComponentImports()
258170
fileOut += delayOut
259-
fileOut += "func New() *Component { return new(Component)}\n"
171+
172+
fileOut += genComponentSetters(component)
260173

261174
gen.WriteFile(path.Join(pkg, strings.ToLower(name), name+".go"), fileOut)
262175
}
263176

177+
func genComponentSetters(component *datadictionary.Component) string {
178+
writer := new(bytes.Buffer)
179+
if err := gen.WriteFieldSetters(writer, component.Name, component.Fields); err != nil {
180+
panic(err)
181+
}
182+
183+
return writer.String()
184+
}
185+
264186
func main() {
265187
flag.Usage = usage
266188
flag.Parse()

0 commit comments

Comments
 (0)