|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "github.com/quickfixgo/quickfix/_gen" |
| 7 | + "github.com/quickfixgo/quickfix/datadictionary" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + pkg string |
| 16 | + fixSpec *datadictionary.DataDictionary |
| 17 | +) |
| 18 | + |
| 19 | +func usage() { |
| 20 | + fmt.Fprintf(os.Stderr, "usage: generate-components [flags] <path to data dictionary>\n") |
| 21 | + flag.PrintDefaults() |
| 22 | + os.Exit(2) |
| 23 | +} |
| 24 | + |
| 25 | +func initPackage() { |
| 26 | + pkg = strings.ToLower(fixSpec.FIXType) + strconv.Itoa(fixSpec.Major) + strconv.Itoa(fixSpec.Minor) |
| 27 | + |
| 28 | + if fixSpec.ServicePack != 0 { |
| 29 | + pkg += "sp" + strconv.Itoa(fixSpec.ServicePack) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func packageString() (s string) { |
| 34 | + s = fmt.Sprintf("package %v\n", pkg) |
| 35 | + return |
| 36 | +} |
| 37 | + |
| 38 | +func writeField(field *datadictionary.FieldDef, componentName string) (s string) { |
| 39 | + if field.Required { |
| 40 | + s += fmt.Sprintf("//%v is a required field for %v.\n", field.Name, componentName) |
| 41 | + } else { |
| 42 | + s += fmt.Sprintf("//%v is a non-required field for %v.\n", field.Name, componentName) |
| 43 | + } |
| 44 | + |
| 45 | + goType := "" |
| 46 | + switch field.Type { |
| 47 | + case "MULTIPLESTRINGVALUE", "MULTIPLEVALUESTRING": |
| 48 | + fallthrough |
| 49 | + case "MULTIPLECHARVALUE": |
| 50 | + fallthrough |
| 51 | + case "CHAR": |
| 52 | + fallthrough |
| 53 | + case "CURRENCY": |
| 54 | + fallthrough |
| 55 | + case "DATA": |
| 56 | + fallthrough |
| 57 | + case "MONTHYEAR": |
| 58 | + fallthrough |
| 59 | + case "LOCALMKTDATE": |
| 60 | + fallthrough |
| 61 | + case "EXCHANGE": |
| 62 | + fallthrough |
| 63 | + case "LANGUAGE": |
| 64 | + fallthrough |
| 65 | + case "XMLDATA": |
| 66 | + fallthrough |
| 67 | + case "COUNTRY": |
| 68 | + fallthrough |
| 69 | + case "UTCTIMEONLY": |
| 70 | + fallthrough |
| 71 | + case "UTCDATEONLY": |
| 72 | + fallthrough |
| 73 | + case "TZTIMEONLY": |
| 74 | + fallthrough |
| 75 | + case "TZTIMESTAMP": |
| 76 | + fallthrough |
| 77 | + case "STRING": |
| 78 | + goType = "string" |
| 79 | + |
| 80 | + case "BOOLEAN": |
| 81 | + goType = "bool" |
| 82 | + |
| 83 | + case "LENGTH": |
| 84 | + fallthrough |
| 85 | + case "DAYOFMONTH": |
| 86 | + fallthrough |
| 87 | + case "NUMINGROUP": |
| 88 | + fallthrough |
| 89 | + case "SEQNUM": |
| 90 | + fallthrough |
| 91 | + case "INT": |
| 92 | + goType = "int" |
| 93 | + |
| 94 | + case "TIME": |
| 95 | + fallthrough |
| 96 | + case "UTCTIMESTAMP": |
| 97 | + goType = "time.Time" |
| 98 | + |
| 99 | + case "QTY": |
| 100 | + fallthrough |
| 101 | + case "AMT": |
| 102 | + fallthrough |
| 103 | + case "PRICE": |
| 104 | + fallthrough |
| 105 | + case "PRICEOFFSET": |
| 106 | + fallthrough |
| 107 | + case "PERCENTAGE": |
| 108 | + fallthrough |
| 109 | + case "FLOAT": |
| 110 | + goType = "float64" |
| 111 | + |
| 112 | + default: |
| 113 | + fmt.Printf("Unknown type '%v' for tag '%v'\n", field.Type, field.Tag) |
| 114 | + } |
| 115 | + |
| 116 | + fixTags := strconv.Itoa(field.Tag) |
| 117 | + if field.Tag == 8 { |
| 118 | + if fixSpec.Major == 4 { |
| 119 | + fixTags = fmt.Sprintf("%v,default=FIX.%v.%v", fixTags, fixSpec.Major, fixSpec.Minor) |
| 120 | + } else { |
| 121 | + fixTags = fixTags + ",default=FIXT.1.1" |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if field.Required { |
| 126 | + s += fmt.Sprintf("%v %v `fix:\"%v\"`\n", field.Name, goType, fixTags) |
| 127 | + } else { |
| 128 | + s += fmt.Sprintf("%v *%v `fix:\"%v\"`\n", field.Name, goType, fixTags) |
| 129 | + } |
| 130 | + return |
| 131 | +} |
| 132 | + |
| 133 | +func genComponentImports() string { |
| 134 | + fileOut := "import \"time\"\n" |
| 135 | + return fileOut |
| 136 | +} |
| 137 | + |
| 138 | +func genHeader(header *datadictionary.MessageDef) { |
| 139 | + fileOut := packageString() |
| 140 | + fileOut += genComponentImports() |
| 141 | + |
| 142 | + fileOut += fmt.Sprintf("//Header is the %v Header type\n", pkg) |
| 143 | + fileOut += "type Header struct {\n" |
| 144 | + for _, field := range header.FieldsInDeclarationOrder { |
| 145 | + fileOut += writeField(field, "Header") |
| 146 | + } |
| 147 | + fileOut += "}\n" |
| 148 | + |
| 149 | + gen.WriteFile(path.Join(pkg, "header.go"), fileOut) |
| 150 | +} |
| 151 | + |
| 152 | +func genTrailer(trailer *datadictionary.MessageDef) { |
| 153 | + fileOut := packageString() |
| 154 | + fileOut += fmt.Sprintf("//Trailer is the %v Trailer type\n", pkg) |
| 155 | + fileOut += "type Trailer struct {\n" |
| 156 | + for _, field := range trailer.FieldsInDeclarationOrder { |
| 157 | + fileOut += writeField(field, "Trailer") |
| 158 | + } |
| 159 | + fileOut += "}\n" |
| 160 | + |
| 161 | + gen.WriteFile(path.Join(pkg, "trailer.go"), fileOut) |
| 162 | +} |
| 163 | + |
| 164 | +func main() { |
| 165 | + flag.Usage = usage |
| 166 | + flag.Parse() |
| 167 | + |
| 168 | + if flag.NArg() != 1 { |
| 169 | + usage() |
| 170 | + } |
| 171 | + |
| 172 | + dataDict := flag.Arg(0) |
| 173 | + |
| 174 | + if spec, err := datadictionary.Parse(dataDict); err != nil { |
| 175 | + panic(err) |
| 176 | + } else { |
| 177 | + fixSpec = spec |
| 178 | + } |
| 179 | + |
| 180 | + initPackage() |
| 181 | + if fi, err := os.Stat(pkg); os.IsNotExist(err) { |
| 182 | + if err := os.Mkdir(pkg, os.ModePerm); err != nil { |
| 183 | + panic(err) |
| 184 | + } |
| 185 | + } else if !fi.IsDir() { |
| 186 | + panic(pkg + "/ is not a directory") |
| 187 | + } |
| 188 | + |
| 189 | + switch pkg { |
| 190 | + //uses fixt11 header/trailer |
| 191 | + case "fix50", "fix50sp1", "fix50sp2": |
| 192 | + default: |
| 193 | + genHeader(fixSpec.Header) |
| 194 | + genTrailer(fixSpec.Trailer) |
| 195 | + } |
| 196 | +} |
0 commit comments