/
iso8583_json_spec_defs.go
309 lines (248 loc) · 7.34 KB
/
iso8583_json_spec_defs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package iso8583
//fixed field attributes = key field{true|false};length;encoding;padding_type
//variable field attributes = key field{true|false};len indicator size in bytes;len ind encoding;data encoding_;nibbles={true|false} (no padding type)
//encoding can be ascii, binary, bcd or ebcdic
//nibbles=true implies that the size is indicated in number of half bytes (or nibbles) -
// i.e. is field value is "00000000" then if nibbles is true length indicator is 8 else 4
//a key field uniquely identifies a message/transaction
//more than a single field can be a key - a key is formed by concatenation
//padding_type has not be implemented, leave as default
// _ is used when the attribute is not applicable
//only Bitmapped field will have children, and the BitPosition indicates their position
//in the bitmap
import (
_ "bufio"
"bytes"
"container/list"
"encoding/json"
"github.com/rkbalgi/go/paysim/demo"
pylog "github.com/rkbalgi/go/paysim/log"
_ "io"
"log"
"os"
_ "strconv"
_ "strings"
)
type FieldAttributes struct {
DataEncoding string
FieldLength int
Key bool
FieldIndicatorLength int
FieldIndicatorEncoding string
Padding string
}
//JsonFieldDef represents the definition of a field in the specs.json file
type JsonFieldDef struct {
BitPosition int
Name string
Type string
Attrs FieldAttributes
Children []JsonFieldDef
}
//JsonSpecDef represents the definition of a field in the specs.json file
type JsonSpecDef struct {
SpecName string
Fields []JsonFieldDef
}
type JsonSpecDefs struct {
Specs []JsonSpecDef
}
func displaySpecs() {
for k, v := range specMap {
spec := v
buf := bytes.NewBufferString("")
for l := spec.fieldsDefList.Front(); l != nil; l = l.Next() {
switch obj := l.Value.(type) {
case IsoField:
{
buf.WriteString(obj.Def() + "\n")
break
}
case BitmappedField:
{
bmp := obj.(*BitMap)
buf.WriteString(obj.Def() + "\n")
for _, fDef := range bmp.subFieldDef {
if fDef != nil {
buf.WriteString(fDef.Def() + "\n")
}
}
}
} //end swicth
}
pylog.Printf("Spec: %s : Defs: \n%s\n", k, buf.String())
} //end for-range
}
var specInit bool = false
func ReadDemoSpecDefs() {
if !specInit {
specMap = make(map[string]*MessageDef)
} else {
for k, _ := range specMap {
delete(specMap, k)
}
}
str := demo.DemoSpecs
ReadSpecDefsFromBuf(bytes.NewBufferString(str))
specInit = true
displaySpecs()
}
func ReadSpecDefs(fileName string) {
if !specInit {
specMap = make(map[string]*MessageDef)
} else {
for k, _ := range specMap {
delete(specMap, k)
}
}
//initialize map of all spec's to their definitions
//read all specs from the json file
//lets just display details of all defined specs
file, err := os.Open(fileName)
if err != nil {
pylog.Log("failed to open specs.json file", err.Error())
return
}
tmpData := make([]byte, 100)
buf := bytes.NewBuffer([]byte{})
for {
count, err := file.Read(tmpData)
if err != nil && err.Error() == "EOF" {
break
} else if err != nil {
pylog.Log("failed to read from specs.json file", err.Error())
break
}
buf.Write(tmpData[:count])
}
ReadSpecDefsFromBuf(buf)
specInit = true
displaySpecs()
}
func ReadSpecDefsFromBuf(buf *bytes.Buffer) {
specDefs := new(JsonSpecDefs)
err := json.Unmarshal(buf.Bytes(), specDefs)
if err != nil {
pylog.Log("failed to parse specs.json", err.Error())
return
}
for _, spec := range specDefs.Specs {
iso8583MsgDef := new(MessageDef)
iso8583MsgDef.specName = spec.SpecName
iso8583MsgDef.fieldSeq = 0
iso8583MsgDef.fieldsDefList = list.New()
for _, isoFieldDef := range spec.Fields {
if isoFieldDef.Type == "Fixed" {
fixedFieldDef := constructFixedFieldDef(&isoFieldDef)
fixedFieldDef.SetId(iso8583MsgDef.nextFieldSeq())
if fixedFieldDef != nil {
iso8583MsgDef.addField(fixedFieldDef)
}
} else if isoFieldDef.Type == "Variable" {
varFieldDef := constructVariableFieldDef(&isoFieldDef)
varFieldDef.SetId(iso8583MsgDef.nextFieldSeq())
if varFieldDef != nil {
iso8583MsgDef.addField(varFieldDef)
}
} else if isoFieldDef.Type == "Bitmapped" {
bmpFieldDef := constructBmpFieldDef(iso8583MsgDef, &isoFieldDef)
bmpFieldDef.SetId(iso8583MsgDef.nextFieldSeq())
if bmpFieldDef != nil {
iso8583MsgDef.addField(bmpFieldDef)
}
} else {
log.Panic("unsupported field type - ", isoFieldDef.Type)
}
}
specMap[iso8583MsgDef.specName] = iso8583MsgDef
}
}
func constructFixedFieldDef(jsonFieldDef *JsonFieldDef) *FixedFieldDef {
/*attrs := strings.Split(json_field_def.Attrs, ";")
if len(attrs) != 4 {
log.Panic("invalid attribute spec for fixed field -" + json_field_def.Name)
return nil
}
//TODO:: for now only look at 1 and 2 attrs i.e. lengt and encoding
field_len, err := strconv.ParseUint(attrs[1], 10, 32)
if err != nil {
log.Panic("invalid field length -", json_field_def.Name)
return nil
}*/
encodingType := getEncoding(jsonFieldDef, jsonFieldDef.Attrs.DataEncoding)
fixedFieldDef := NewFixedFieldDef(jsonFieldDef.Name, encodingType, jsonFieldDef.Attrs.FieldLength)
fixedFieldDef.SetBitPosition(jsonFieldDef.BitPosition)
pylog.Log("processed field def- " + jsonFieldDef.Name)
return fixedFieldDef
}
func constructVariableFieldDef(jsonFieldDef *JsonFieldDef) *VariableFieldDef {
/*attrs := strings.Split(json_field_def.Attrs, ";")
if len(attrs) != 5 {
log.Panic("invalid attribute spec for variable field -" + json_field_def.Name)
return nil
}
//TODO:: for now only look at 1,2 and 3 attrs i.e. lengt and encoding
field_len, err := strconv.ParseUint(attrs[1], 10, 32)
if err != nil {
log.Panic("invalid length indicator length -", json_field_def.Name)
return nil
}*/
lenEncodingType := getEncoding(jsonFieldDef, jsonFieldDef.Attrs.FieldIndicatorEncoding)
dataEncodingType := getEncoding(jsonFieldDef, jsonFieldDef.Attrs.DataEncoding)
varFieldDef := NewVariableFieldDef(jsonFieldDef.Name, lenEncodingType, dataEncodingType, jsonFieldDef.Attrs.FieldIndicatorLength)
varFieldDef.SetBitPosition(jsonFieldDef.BitPosition)
pylog.Log("processed variable field def- " + jsonFieldDef.Name)
return varFieldDef
}
func constructBmpFieldDef(iso8583MsgDef *MessageDef, jsonFieldDef *JsonFieldDef) *BitMap {
bmp := NewBitMap()
//var bmp_field BitmappedField = bmp
for _, childField := range jsonFieldDef.Children {
switch childField.Type {
case "Fixed":
{
fld := constructFixedFieldDef(&childField)
fld.SetId(iso8583MsgDef.nextFieldSeq())
bmp.subFieldDef[childField.BitPosition] = fld
}
case "Variable":
{
fld := constructVariableFieldDef(&childField)
fld.SetId(iso8583MsgDef.nextFieldSeq())
bmp.subFieldDef[childField.BitPosition] = fld
}
default:
{
log.Panic("unsupported child field for bitmapped parent field - ", childField.Type)
}
}
}
pylog.Log("processed bitmapped field - " + jsonFieldDef.Name)
return bmp
}
func getEncoding(isoFieldDef *JsonFieldDef, data string) int {
encodingType := 0
switch data {
case "ascii":
{
encodingType = asciiEncoding
}
case "ebcdic":
{
encodingType = ebcdicEncoding
}
case "binary":
{
encodingType = binaryEncoding
}
case "bcd":
{
encodingType = bcdEncoding
}
default:
{
log.Panicf("invalid encoding [%s] on %s", data, isoFieldDef.Name)
}
}
return encodingType
}