Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions _gen/generate-components/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"flag"
"fmt"
"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
"os"
"path"
"strconv"
"strings"

"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
)

var (
Expand Down Expand Up @@ -57,46 +58,51 @@ type group struct {
field *datadictionary.FieldDef
}

func collectGroups(parent string, field *datadictionary.FieldDef, groups []group) []group {
if !field.IsGroup() {
return groups
}
func collectGroups(parent string, part datadictionary.MessagePart, groups []group) []group {
switch field := part.(type) {
case *datadictionary.FieldDef:
if !field.IsGroup() {
return groups
}

groups = append(groups, group{parent, field})
for _, childField := range field.ChildFields {
groups = collectGroups(field.Name, childField, groups)
groups = append(groups, group{parent, field})
for _, childField := range field.Parts {
groups = collectGroups(field.Name(), childField, groups)
}
}

return groups
}

func writeFieldDeclaration(field *datadictionary.FieldDef, componentName string) string {
switch {
case field.IsComponent():
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Component.Name))] = true
case !field.IsGroup():
goType := gen.FixFieldTypeToGoType(field.Type)
if goType == "time.Time" {
imports["time"] = true
func writeFieldDeclaration(part datadictionary.MessagePart, componentName string) string {
switch field := part.(type) {
case datadictionary.Component:
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Name()))] = true
case *datadictionary.FieldDef:
if !field.IsGroup() {
goType := gen.FixFieldTypeToGoType(field.Type)
if goType == "time.Time" {
imports["time"] = true
}
}
}

return gen.WriteFieldDeclaration(fixSpec.Major, fixSpec.Minor, field, componentName)
return gen.WriteFieldDeclaration(fixSpec.Major, fixSpec.Minor, part, componentName)
}

func genGroupDeclaration(field *datadictionary.FieldDef, parent string) (fileOut string) {
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name, parent)
fileOut += fmt.Sprintf("type %v struct {\n", field.Name)
for _, groupField := range field.ChildFields {
fileOut += writeFieldDeclaration(groupField, field.Name)
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name(), parent)
fileOut += fmt.Sprintf("type %v struct {\n", field.Name())
for _, groupField := range field.Parts {
fileOut += writeFieldDeclaration(groupField, field.Name())
}

fileOut += "}\n"

return
}

func genGroupDeclarations(name string, fields []*datadictionary.FieldDef) (fileOut string) {
func genGroupDeclarations(name string, fields []datadictionary.MessagePart) (fileOut string) {
groups := []group{}
for _, field := range fields {
groups = collectGroups(name, field, groups)
Expand All @@ -113,10 +119,10 @@ func genHeader(header *datadictionary.MessageDef) {
imports = make(map[string]bool)

//delay field output to determine imports
delayOut := genGroupDeclarations("Header", header.FieldsInDeclarationOrder)
delayOut := genGroupDeclarations("Header", header.Parts)
delayOut += fmt.Sprintf("//Header is the %v Header type\n", pkg)
delayOut += "type Header struct {\n"
for _, field := range header.FieldsInDeclarationOrder {
for _, field := range header.Parts {
delayOut += writeFieldDeclaration(field, "Header")
}
delayOut += "}\n"
Expand All @@ -126,7 +132,7 @@ func genHeader(header *datadictionary.MessageDef) {
fileOut += delayOut

writer := new(bytes.Buffer)
if err := gen.WriteFieldSetters(writer, "Header", header.FieldsInDeclarationOrder); err != nil {
if err := gen.WriteFieldSetters(writer, "Header", header.Parts); err != nil {
panic(err)
}
fileOut += writer.String()
Expand All @@ -139,28 +145,28 @@ func genTrailer(trailer *datadictionary.MessageDef) {
fileOut := packageString()
fileOut += fmt.Sprintf("//Trailer is the %v Trailer type\n", pkg)
fileOut += "type Trailer struct {\n"
for _, field := range trailer.FieldsInDeclarationOrder {
for _, field := range trailer.Parts {
fileOut += writeFieldDeclaration(field, "Trailer")
}
fileOut += "}\n"

writer := new(bytes.Buffer)
if err := gen.WriteFieldSetters(writer, "Trailer", trailer.FieldsInDeclarationOrder); err != nil {
if err := gen.WriteFieldSetters(writer, "Trailer", trailer.Parts); err != nil {
panic(err)
}
fileOut += writer.String()

gen.WriteFile(path.Join(pkg, "trailer.go"), fileOut)
}

func genComponent(name string, component *datadictionary.Component) {
func genComponent(name string, component *datadictionary.ComponentType) {
imports = make(map[string]bool)

//delay output to determine imports
delayOut := genGroupDeclarations(name, component.Fields)
delayOut := genGroupDeclarations(name, component.Parts)
delayOut += fmt.Sprintf("//%v is a %v Component\n", name, pkg)
delayOut += fmt.Sprintf("type %v struct {\n", name)
for _, field := range component.Fields {
for _, field := range component.Parts {
delayOut += writeFieldDeclaration(field, name)
}
delayOut += "}\n"
Expand All @@ -174,9 +180,9 @@ func genComponent(name string, component *datadictionary.Component) {
gen.WriteFile(path.Join(pkg, strings.ToLower(name), name+".go"), fileOut)
}

func genComponentSetters(component *datadictionary.Component) string {
func genComponentSetters(component *datadictionary.ComponentType) string {
writer := new(bytes.Buffer)
if err := gen.WriteFieldSetters(writer, component.Name, component.Fields); err != nil {
if err := gen.WriteFieldSetters(writer, component.Name(), component.Parts); err != nil {
panic(err)
}

Expand Down Expand Up @@ -216,7 +222,7 @@ func main() {
genTrailer(fixSpec.Trailer)
}

for name, component := range fixSpec.Components {
for name, component := range fixSpec.ComponentTypes {
genComponent(name, component)
}
}
25 changes: 13 additions & 12 deletions _gen/generate-fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"flag"
"fmt"
"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
"os"
"sort"

"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
)

var (
Expand Down Expand Up @@ -138,16 +139,16 @@ func genFields() {
fmt.Printf("Unknown type '%v' for tag '%v'\n", field.Type, tag)
}

fileOut += fmt.Sprintf("//%vField is a %v field\n", field.Name, field.Type)
fileOut += fmt.Sprintf("type %vField struct { quickfix.%v }\n", field.Name, baseType)
fileOut += fmt.Sprintf("//Tag returns tag.%v (%v)\n", field.Name, field.Tag)
fileOut += fmt.Sprintf("func (f %vField) Tag() quickfix.Tag {return tag.%v}\n", field.Name, field.Name)
fileOut += fmt.Sprintf("//%vField is a %v field\n", field.Name(), field.Type)
fileOut += fmt.Sprintf("type %vField struct { quickfix.%v }\n", field.Name(), baseType)
fileOut += fmt.Sprintf("//Tag returns tag.%v (%v)\n", field.Name(), field.Tag)
fileOut += fmt.Sprintf("func (f %vField) Tag() quickfix.Tag {return tag.%v}\n", field.Name(), field.Name())

switch goType {
case "bool", "int", "float64", "string":
fileOut += fmt.Sprintf("//New%v returns a new %vField initialized with val\n", field.Name, field.Name)
fileOut += fmt.Sprintf("func New%v(val %v) *%vField {\n", field.Name, goType, field.Name)
fileOut += fmt.Sprintf("return &%vField{quickfix.%v(val)}\n", field.Name, baseType)
fileOut += fmt.Sprintf("//New%v returns a new %vField initialized with val\n", field.Name(), field.Name())
fileOut += fmt.Sprintf("func New%v(val %v) *%vField {\n", field.Name(), goType, field.Name())
fileOut += fmt.Sprintf("return &%vField{quickfix.%v(val)}\n", field.Name(), baseType)
fileOut += "}\n"
}
}
Expand Down Expand Up @@ -188,9 +189,9 @@ func main() {
}

for _, field := range spec.FieldTypeByTag {
fieldMap[field.Name] = int(field.Tag)
fieldMap[field.Name()] = int(field.Tag)

if oldField, ok := fieldTypeMap[field.Name]; ok {
if oldField, ok := fieldTypeMap[field.Name()]; ok {
//merge old enums with new
if len(oldField.Enums) > 0 && field.Enums == nil {
field.Enums = make(map[string]datadictionary.Enum)
Expand All @@ -214,7 +215,7 @@ func main() {
}
}

fieldTypeMap[field.Name] = field
fieldTypeMap[field.Name()] = field
}
}

Expand Down
69 changes: 34 additions & 35 deletions _gen/generate-messages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"flag"
"fmt"
"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
"os"
"path"
"sort"
"strconv"
"strings"

"github.com/quickfixgo/quickfix/_gen"
"github.com/quickfixgo/quickfix/datadictionary"
)

var (
Expand Down Expand Up @@ -55,31 +56,33 @@ import(
return fileOut
}

func writeFieldDeclaration(field *datadictionary.FieldDef, componentName string) string {
switch {
case field.IsComponent():
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Component.Name))] = true
case !field.IsGroup():
goType := gen.FixFieldTypeToGoType(field.Type)
if goType == "time.Time" {
imports["time"] = true
func writeFieldDeclaration(part datadictionary.MessagePart, componentName string) string {
switch field := part.(type) {
case datadictionary.Component:
imports[fmt.Sprintf("github.com/quickfixgo/quickfix/%v/%v", pkg, strings.ToLower(field.Name()))] = true
case *datadictionary.FieldDef:
if !field.IsGroup() {
goType := gen.FixFieldTypeToGoType(field.Type)
if goType == "time.Time" {
imports["time"] = true
}
}
}

return gen.WriteFieldDeclaration(fixSpec.Major, fixSpec.Minor, field, componentName)
return gen.WriteFieldDeclaration(fixSpec.Major, fixSpec.Minor, part, componentName)
}

func genGroupDeclaration(field *datadictionary.FieldDef, parent string) (fileOut string) {
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name, parent)
fileOut += fmt.Sprintf("type %v struct {\n", field.Name)
for _, groupField := range field.ChildFields {
fileOut += writeFieldDeclaration(groupField, field.Name)
fileOut += fmt.Sprintf("//%v is a repeating group in %v\n", field.Name(), parent)
fileOut += fmt.Sprintf("type %v struct {\n", field.Name())
for _, groupField := range field.Parts {
fileOut += writeFieldDeclaration(groupField, field.Name())
}

fileOut += "}\n"

writer := new(bytes.Buffer)
if err := gen.WriteFieldSetters(writer, field.Name, field.ChildFields); err != nil {
if err := gen.WriteFieldSetters(writer, field.Name(), field.Parts); err != nil {
panic(err)
}
fileOut += writer.String()
Expand All @@ -93,22 +96,25 @@ type group struct {
field *datadictionary.FieldDef
}

func collectGroups(parent string, field *datadictionary.FieldDef, groups []group) []group {
if !field.IsGroup() {
return groups
}
func collectGroups(parent string, part datadictionary.MessagePart, groups []group) []group {
switch field := part.(type) {
case *datadictionary.FieldDef:
if !field.IsGroup() {
return groups
}

groups = append(groups, group{parent, field})
for _, childField := range field.ChildFields {
groups = collectGroups(field.Name, childField, groups)
groups = append(groups, group{parent, field})
for _, childField := range field.Parts {
groups = collectGroups(field.Name(), childField, groups)
}
}

return groups
}

func genGroupDeclarations(msg *datadictionary.MessageDef) (fileOut string) {
groups := []group{}
for _, field := range msg.FieldsInDeclarationOrder {
for _, field := range msg.Parts {
groups = collectGroups(msg.Name, field, groups)
}

Expand All @@ -128,12 +134,12 @@ func headerTrailerPkg() string {
return pkg
}

func genMessage(msg *datadictionary.MessageDef, requiredFields []*datadictionary.FieldDef) string {
func genMessage(msg *datadictionary.MessageDef) string {
fileOut := fmt.Sprintf("//Message is a %v FIX Message\n", msg.Name)
fileOut += "type Message struct {\n"
fileOut += fmt.Sprintf("FIXMsgType string `fix:\"%v\"`\n", msg.MsgType)
fileOut += fmt.Sprintf("%v.Header\n", headerTrailerPkg())
for _, field := range msg.FieldsInDeclarationOrder {
for _, field := range msg.Parts {
fileOut += writeFieldDeclaration(field, msg.Name)
}
fileOut += fmt.Sprintf("%v.Trailer\n", headerTrailerPkg())
Expand All @@ -146,7 +152,7 @@ func genMessage(msg *datadictionary.MessageDef, requiredFields []*datadictionary

func genMessageSetters(msg *datadictionary.MessageDef) string {
writer := new(bytes.Buffer)
if err := gen.WriteFieldSetters(writer, "Message", msg.FieldsInDeclarationOrder); err != nil {
if err := gen.WriteFieldSetters(writer, "Message", msg.Parts); err != nil {
panic(err)
}
return writer.String()
Expand Down Expand Up @@ -185,13 +191,6 @@ func Route(router RouteOut) (string,string,quickfix.MessageRoute) {
}

func genMessagePkg(msg *datadictionary.MessageDef) {
requiredFields := make([]*datadictionary.FieldDef, 0, len(msg.FieldsInDeclarationOrder))
for _, field := range msg.FieldsInDeclarationOrder {
if field.Required {
requiredFields = append(requiredFields, field)
}
}

pkgName := strings.ToLower(msg.Name)
imports = make(map[string]bool)

Expand All @@ -201,7 +200,7 @@ func genMessagePkg(msg *datadictionary.MessageDef) {
//run through group and message declarations to collect required imports first
delayOut := ""
delayOut += genGroupDeclarations(msg)
delayOut += genMessage(msg, requiredFields)
delayOut += genMessage(msg)

fileOut += genMessageImports()
fileOut += delayOut
Expand Down
Loading