Skip to content

Commit a6c4bb9

Browse files
committed
Improve output of cmd/generate-fix
Signed-off-by: Sylvain Rabot <[email protected]>
1 parent 7945e4b commit a6c4bb9

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
@@ -77,30 +77,41 @@ func checkFieldTimeRequired(f *datadictionary.FieldDef) (required bool, err erro
7777
return
7878
}
7979

80-
func collectExtraImports(m *datadictionary.MessageDef) (imports []string, err error) {
81-
var timeRequired, decimalRequired bool
80+
func collectStandardImports(m *datadictionary.MessageDef) (imports []string, err error) {
81+
var timeRequired bool
8282
for _, f := range m.Fields {
8383
if !timeRequired {
8484
if timeRequired, err = checkFieldTimeRequired(f); err != nil {
8585
return
8686
}
8787
}
8888

89+
if timeRequired {
90+
break
91+
}
92+
}
93+
94+
if timeRequired {
95+
imports = append(imports, "time")
96+
}
97+
98+
return
99+
}
100+
101+
func collectExtraImports(m *datadictionary.MessageDef) (imports []string, err error) {
102+
var decimalRequired bool
103+
for _, f := range m.Fields {
89104
if !decimalRequired {
90105
if decimalRequired, err = checkFieldDecimalRequired(f); err != nil {
91106
return
92107
}
93108
}
94109

95-
if decimalRequired && timeRequired {
110+
if decimalRequired {
96111
break
97112
}
98113
}
99114

100-
if timeRequired {
101-
imports = append(imports, "time")
102-
}
103-
104115
if decimalRequired {
105116
imports = append(imports, "github.com/shopspring/decimal")
106117
}
@@ -180,7 +191,7 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
180191
fallthrough
181192
case "MONTHYEAR":
182193
fallthrough
183-
case "LOCALMKTDATE":
194+
case "LOCALMKTTIME", "LOCALMKTDATE":
184195
fallthrough
185196
case "TIME":
186197
fallthrough
@@ -204,6 +215,8 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
204215
fallthrough
205216
case "TZTIMESTAMP":
206217
fallthrough
218+
case "XID", "XIDREF":
219+
fallthrough
207220
case "STRING":
208221
quickfixType = "FIXString"
209222

@@ -218,6 +231,8 @@ func quickfixType(field *datadictionary.FieldType) (quickfixType string, err err
218231
fallthrough
219232
case "SEQNUM":
220233
fallthrough
234+
case "TAGNUM":
235+
fallthrough
221236
case "INT":
222237
quickfixType = "FIXInt"
223238

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
"checkIfEnumImportRequired": checkIfEnumImportRequired,
@@ -111,7 +112,14 @@ func ({{ template "receiver" }} {{ $.Name }}) Has{{ .Name}}() bool {
111112
112113
{{ define "group_template" }}
113114
quickfix.GroupTemplate{
114-
{{- range $index, $field := . }}{{if $index}},{{end}}{{if $field.IsGroup }}New{{ $field.Name }}RepeatingGroup(){{else}}quickfix.GroupElement(tag.{{$field.Name}}){{ end }}{{ end }} }
115+
{{- range $index, $field := . }}
116+
{{- if $field.IsGroup }}
117+
New{{ $field.Name }}RepeatingGroup(),
118+
{{- else}}
119+
quickfix.GroupElement(tag.{{$field.Name}}),
120+
{{- end }}
121+
{{- end }}
122+
}
115123
{{- end }}
116124
117125
{{ define "field_args" }}
@@ -139,7 +147,11 @@ type {{ .Name }}RepeatingGroup struct {
139147
// New{{ .Name }}RepeatingGroup returns an initialized, {{ .Name }}RepeatingGroup.
140148
func New{{ .Name }}RepeatingGroup() {{ .Name }}RepeatingGroup {
141149
return {{ .Name }}RepeatingGroup{
142-
quickfix.NewRepeatingGroup(tag.{{ .Name }}, {{ template "group_template" .Fields }})}
150+
quickfix.NewRepeatingGroup(
151+
tag.{{ .Name }},
152+
{{- template "group_template" .Fields }},
153+
),
154+
}
143155
}
144156
145157
// Add create and append a new {{ .Name }} to this group.
@@ -160,12 +172,17 @@ func ({{ template "receiver" }} {{ .Name}}RepeatingGroup) Get(i int) {{ .Name }}
160172
{{ define "receiver" }}h{{ end }}
161173
package {{ .Package }}
162174
163-
import(
175+
import (
176+
{{- if collectStandardImports .MessageDef }}
177+
{{- range collectStandardImports .MessageDef }}
178+
"{{ . }}"
179+
{{- end }}{{ "\n" }}
180+
{{- end }}
181+
{{- if collectExtraImports .MessageDef }}
164182
{{- range collectExtraImports .MessageDef }}
165183
"{{ . }}"
184+
{{- end }}{{ "\n" }}
166185
{{- end }}
167-
168-
169186
"github.com/quickfixgo/quickfix"
170187
{{- if checkIfEnumImportRequired .MessageDef}}
171188
"{{ importRootPath }}/enum"
@@ -196,11 +213,17 @@ func NewHeader(header *quickfix.Header) (h Header) {
196213
{{ define "receiver" }}t{{ end }}
197214
package {{ .Package }}
198215
199-
import(
216+
import (
217+
{{- if collectStandardImports .MessageDef }}
218+
{{- range collectStandardImports .MessageDef }}
219+
"{{ . }}"
220+
{{- end }}{{ "\n" }}
221+
{{- end }}
222+
{{- if collectExtraImports .MessageDef }}
200223
{{- range collectExtraImports .MessageDef }}
201224
"{{ . }}"
225+
{{- end }}{{ "\n" }}
202226
{{- end }}
203-
204227
"github.com/quickfixgo/quickfix"
205228
{{- if checkIfEnumImportRequired .MessageDef}}
206229
"{{ importRootPath }}/enum"
@@ -224,11 +247,17 @@ type Trailer struct {
224247
{{ define "receiver" }}m{{ end }}
225248
package {{ .Package }}
226249
227-
import(
250+
import (
251+
{{- if collectStandardImports .MessageDef }}
252+
{{- range collectStandardImports .MessageDef }}
253+
"{{ . }}"
254+
{{- end }}{{ "\n" }}
255+
{{- end }}
256+
{{- if collectExtraImports .MessageDef }}
228257
{{- range collectExtraImports .MessageDef }}
229258
"{{ . }}"
259+
{{- end }}{{ "\n" }}
230260
{{- end }}
231-
232261
"github.com/quickfixgo/quickfix"
233262
{{- if checkIfEnumImportRequired .MessageDef}}
234263
"{{ importRootPath }}/enum"
@@ -296,23 +325,25 @@ func Route(router RouteOut) (string, string, quickfix.MessageRoute) {
296325

297326
TagTemplate = template.Must(template.New("Tag").Parse(`
298327
package tag
299-
import("github.com/quickfixgo/quickfix")
328+
import "github.com/quickfixgo/quickfix"
300329
301330
const (
302331
{{- range .}}
303-
{{ .Name }} quickfix.Tag = {{ .Tag }}
332+
{{ .Name }} quickfix.Tag = {{ .Tag }}
304333
{{- end }}
305334
)
306335
`))
307336

308337
FieldTemplate = template.Must(template.New("Field").Funcs(tmplFuncs).Parse(`
309338
package field
310-
import(
339+
import (
340+
"time"
341+
342+
{{ if checkIfDecimalImportRequiredForFields . }}"github.com/shopspring/decimal"{{ end }}
343+
311344
"github.com/quickfixgo/quickfix"
312345
"{{ importRootPath }}/enum"
313346
"{{ importRootPath }}/tag"
314-
{{ if checkIfDecimalImportRequiredForFields . }} "github.com/shopspring/decimal" {{ end }}
315-
"time"
316347
)
317348
318349
{{ range . }}

0 commit comments

Comments
 (0)