Skip to content

Commit ad6344a

Browse files
authored
Merge pull request #513 from sylr/templates
Improve import organization for generator and support more datadictionary types
2 parents 013d9ff + 656b6d9 commit ad6344a

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

cmd/generate-fix/internal/template_helpers.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,41 @@ func checkFieldTimeRequired(f *datadictionary.FieldDef) (required bool, err erro
9898
return
9999
}
100100

101-
func collectExtraImports(m *datadictionary.MessageDef) (imports []string, err error) {
102-
var timeRequired, decimalRequired bool
101+
func collectStandardImports(m *datadictionary.MessageDef) (imports []string, err error) {
102+
var timeRequired bool
103103
for _, f := range m.Fields {
104104
if !timeRequired {
105105
if timeRequired, err = checkFieldTimeRequired(f); err != nil {
106106
return
107107
}
108108
}
109109

110+
if timeRequired {
111+
break
112+
}
113+
}
114+
115+
if timeRequired {
116+
imports = append(imports, "time")
117+
}
118+
119+
return
120+
}
121+
122+
func collectExtraImports(m *datadictionary.MessageDef) (imports []string, err error) {
123+
var decimalRequired bool
124+
for _, f := range m.Fields {
110125
if !decimalRequired {
111126
if decimalRequired, err = checkFieldDecimalRequired(f); err != nil {
112127
return
113128
}
114129
}
115130

116-
if decimalRequired && timeRequired {
131+
if decimalRequired {
117132
break
118133
}
119134
}
120135

121-
if timeRequired {
122-
imports = append(imports, "time")
123-
}
124-
125136
if decimalRequired {
126137
imports = append(imports, "github.com/shopspring/decimal")
127138
}
@@ -201,7 +212,7 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
201212
fallthrough
202213
case "MONTHYEAR":
203214
fallthrough
204-
case "LOCALMKTDATE":
215+
case "LOCALMKTTIME", "LOCALMKTDATE":
205216
fallthrough
206217
case "TIME":
207218
fallthrough
@@ -225,6 +236,8 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
225236
fallthrough
226237
case "TZTIMESTAMP":
227238
fallthrough
239+
case "XID", "XIDREF":
240+
fallthrough
228241
case "STRING":
229242
quickfixType = "FIXString"
230243

@@ -239,6 +252,8 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
239252
fallthrough
240253
case "SEQNUM":
241254
fallthrough
255+
case "TAGNUM":
256+
fallthrough
242257
case "INT":
243258
quickfixType = "FIXInt"
244259

cmd/generate-fix/internal/templates.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func init() {
2424
"quickfixType": quickfixType,
2525
"quickfixValueType": quickfixValueType,
2626
"getGlobalFieldType": getGlobalFieldType,
27+
"collectStandardImports": collectStandardImports,
2728
"collectExtraImports": collectExtraImports,
2829
"checkIfDecimalImportRequiredForFields": checkIfDecimalImportRequiredForFields,
2930
"checkIfTimeImportRequiredForFields": checkIfTimeImportRequiredForFields,
@@ -112,7 +113,14 @@ func ({{ template "receiver" }} {{ $.Name }}) Has{{ .Name}}() bool {
112113
113114
{{ define "group_template" }}
114115
quickfix.GroupTemplate{
115-
{{- range $index, $field := . }}{{if $index}},{{end}}{{if $field.IsGroup }}New{{ $field.Name }}RepeatingGroup(){{else}}quickfix.GroupElement(tag.{{$field.Name}}){{ end }}{{ end }} }
116+
{{- range $index, $field := . }}
117+
{{- if $field.IsGroup }}
118+
New{{ $field.Name }}RepeatingGroup(),
119+
{{- else}}
120+
quickfix.GroupElement(tag.{{$field.Name}}),
121+
{{- end }}
122+
{{- end }}
123+
}
116124
{{- end }}
117125
118126
{{ define "field_args" }}
@@ -140,7 +148,11 @@ type {{ .Name }}RepeatingGroup struct {
140148
// New{{ .Name }}RepeatingGroup returns an initialized, {{ .Name }}RepeatingGroup.
141149
func New{{ .Name }}RepeatingGroup() {{ .Name }}RepeatingGroup {
142150
return {{ .Name }}RepeatingGroup{
143-
quickfix.NewRepeatingGroup(tag.{{ .Name }}, {{ template "group_template" .Fields }})}
151+
quickfix.NewRepeatingGroup(
152+
tag.{{ .Name }},
153+
{{- template "group_template" .Fields }},
154+
),
155+
}
144156
}
145157
146158
// Add create and append a new {{ .Name }} to this group.
@@ -161,12 +173,17 @@ func ({{ template "receiver" }} {{ .Name}}RepeatingGroup) Get(i int) {{ .Name }}
161173
{{ define "receiver" }}h{{ end }}
162174
package {{ .Package }}
163175
164-
import(
176+
import (
177+
{{- if collectStandardImports .MessageDef }}
178+
{{- range collectStandardImports .MessageDef }}
179+
"{{ . }}"
180+
{{- end }}{{ "\n" }}
181+
{{- end }}
182+
{{- if collectExtraImports .MessageDef }}
165183
{{- range collectExtraImports .MessageDef }}
166184
"{{ . }}"
185+
{{- end }}{{ "\n" }}
167186
{{- end }}
168-
169-
170187
"github.com/quickfixgo/quickfix"
171188
{{- if checkIfEnumImportRequired .MessageDef}}
172189
"{{ importRootPath }}/enum"
@@ -197,11 +214,17 @@ func NewHeader(header *quickfix.Header) (h Header) {
197214
{{ define "receiver" }}t{{ end }}
198215
package {{ .Package }}
199216
200-
import(
217+
import (
218+
{{- if collectStandardImports .MessageDef }}
219+
{{- range collectStandardImports .MessageDef }}
220+
"{{ . }}"
221+
{{- end }}{{ "\n" }}
222+
{{- end }}
223+
{{- if collectExtraImports .MessageDef }}
201224
{{- range collectExtraImports .MessageDef }}
202225
"{{ . }}"
226+
{{- end }}{{ "\n" }}
203227
{{- end }}
204-
205228
"github.com/quickfixgo/quickfix"
206229
{{- if checkIfEnumImportRequired .MessageDef}}
207230
"{{ importRootPath }}/enum"
@@ -225,11 +248,17 @@ type Trailer struct {
225248
{{ define "receiver" }}m{{ end }}
226249
package {{ .Package }}
227250
228-
import(
251+
import (
252+
{{- if collectStandardImports .MessageDef }}
253+
{{- range collectStandardImports .MessageDef }}
254+
"{{ . }}"
255+
{{- end }}{{ "\n" }}
256+
{{- end }}
257+
{{- if collectExtraImports .MessageDef }}
229258
{{- range collectExtraImports .MessageDef }}
230259
"{{ . }}"
260+
{{- end }}{{ "\n" }}
231261
{{- end }}
232-
233262
"github.com/quickfixgo/quickfix"
234263
{{- if checkIfEnumImportRequired .MessageDef}}
235264
"{{ importRootPath }}/enum"
@@ -297,23 +326,25 @@ func Route(router RouteOut) (string, string, quickfix.MessageRoute) {
297326

298327
TagTemplate = template.Must(template.New("Tag").Parse(`
299328
package tag
300-
import("github.com/quickfixgo/quickfix")
329+
import "github.com/quickfixgo/quickfix"
301330
302331
const (
303332
{{- range .}}
304-
{{ .Name }} quickfix.Tag = {{ .Tag }}
333+
{{ .Name }} quickfix.Tag = {{ .Tag }}
305334
{{- end }}
306335
)
307336
`))
308337

309338
FieldTemplate = template.Must(template.New("Field").Funcs(tmplFuncs).Parse(`
310339
package field
311-
import(
340+
import (
341+
{{ if checkIfTimeImportRequiredForFields . }}"time"{{ end }}
342+
343+
{{ if checkIfDecimalImportRequiredForFields . }}"github.com/shopspring/decimal"{{ end }}
344+
312345
"github.com/quickfixgo/quickfix"
313346
"{{ importRootPath }}/enum"
314347
"{{ importRootPath }}/tag"
315-
{{ if checkIfDecimalImportRequiredForFields . }} "github.com/shopspring/decimal" {{ end }}
316-
{{ if checkIfTimeImportRequiredForFields . }} "time" {{ end }}
317348
)
318349
319350
{{ range . }}

0 commit comments

Comments
 (0)