-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
279 lines (245 loc) · 6.93 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"golang.org/x/text/encoding/charmap"
)
type _900 struct {
IZ900DVBDIPOS string `decoder:"7"`
IZ900NAZBAN string `decoder:"50"`
IZ9000OIBBNK string `decoder:"11"`
IZ900VRIZ string `decoder:"4"`
IZ900DATUM string `decoder:"8"`
IZ900REZ2 string `decoder:"917" json:"-" xml:"-"`
IZ900TIPSL string `decoder:"3" json:"-" xml:"-"`
}
type _903 struct {
IZ903VBDI string `decoder:"7"`
IZ903BIC string `decoder:"11"`
IZ903RACUN string `decoder:"21"`
IZ903VLRN string `decoder:"3"`
IZ903NAZKLI string `decoder:"70"`
IZ903SJEDKLI string `decoder:"35"`
IZ903MB string `decoder:"8"`
IZ903OIBKLI string `decoder:"11"`
IZ903RBIZV string `decoder:"3"`
IZ903PODBR string `decoder:"3"`
IZ903DATUM string `decoder:"8"`
IZ903BRGRU string `decoder:"4"`
IZ903VRIZ string `decoder:"4"`
IZ903REZ string `decoder:"809" json:"-" xml:"-"`
IZ903TIPSL string `decoder:"3" json:"-" xml:"-"`
}
type _905 struct {
IZ905OZTRA string `decoder:"2"`
IZ905RNPRPL string `decoder:"34"`
IZ905NAZPRPL string `decoder:"70"`
IZ905ADRPRPL string `decoder:"35"`
IZ905SJPRPL string `decoder:"35"`
IZ905DATVAL string `decoder:"8"`
IZ905DATIZVR string `decoder:"8"`
IZ905VLPL string `decoder:"3"`
IZ905TECAJ string `decoder:"15"`
IZ905PREDZNVL string `decoder:"1"`
IZ905IZNOSPPVALUTE string `decoder:"15"`
IZ905PREDZN string `decoder:"1"`
IZ905IZNOS string `decoder:"15"`
IZ905PNBPL string `decoder:"26"`
IZ905PNBPR string `decoder:"26"`
IZ905SIFNAM string `decoder:"4"`
IZ905OPISPL string `decoder:"140"`
IZ905IDTRFINA string `decoder:"42"`
IZ905IDTRBAN string `decoder:"35"`
IZ905REZ2 string `decoder:"482" json:"-" xml:"-"`
IZ905TIPSL string `decoder:"3" json:"-" xml:"-"`
}
type _907 struct {
IZ907RAČUN string `decoder:"21"`
IZ907VLRN string `decoder:"3"`
IZ907NAZKLI string `decoder:"70"`
IZ907RBIZV string `decoder:"3"`
IZ907PRRBIZV string `decoder:"3"`
IZ907DATUM string `decoder:"8"`
IZ907DATPRSAL string `decoder:"8"`
IZ907PPPOS string `decoder:"1"`
IZ907PRSAL string `decoder:"15"`
IZ907PREREZ string `decoder:"1"`
IZ907IZNREZ string `decoder:"15"`
IZ907DATOKV string `decoder:"8"`
IZ907IZNOKV string `decoder:"15"`
IZ907IZNZAPSR string `decoder:"15"`
IZ907PRASPSTA string `decoder:"1"`
IZ907IZNRASP string `decoder:"15"`
IZ907PDUGU string `decoder:"1"`
IZ907KDUGU string `decoder:"15"`
IZ907PPOTR string `decoder:"1"`
IZ907KPOTR string `decoder:"15"`
IZ07PRNOS string `decoder:"1"`
IZ907KOSAL string `decoder:"15"`
IZ907BRGRU string `decoder:"4"`
IZ907BRSTA string `decoder:"6"`
IZ907TEKST string `decoder:"420"`
IZ907REZ2 string `decoder:"317" json:"-" xml:"-"`
IZ907TIPSL string `decoder:"3" json:"-" xml:"-"`
}
type _909 struct {
IZ909DATUM string `decoder:"8"`
IZ909UKGRU string `decoder:"5"`
IZ909UKSLG string `decoder:"6"`
IZ909REZ3 string `decoder:"978" json:"-" xml:"-"`
IZ909TIPSL string `decoder:"3" json:"-" xml:"-"`
}
type _999 struct {
IZ999REZ1 string `decoder:"997" json:"-" xml:"-"`
IZ999TIPSL string `decoder:"3" json:"-" xml:"-"`
}
// Group repesents a group of transactions in a single Statement
type Group struct {
*_903
*_907
Transactions []_905
}
// Statement represents a single bank statement
type Statement struct {
*_900
*_909
Groups []Group
}
func decode(str interface{}, runes []rune) {
reflected := reflect.Indirect(reflect.ValueOf(str))
for i := 0; i < reflected.NumField(); i++ {
tag := reflected.Type().Field(i).Tag.Get("decoder")
fieldLen, _ := strconv.ParseInt(tag, 10, 64)
reflected.Field(i).SetString(strings.TrimSpace(string(runes[:fieldLen])))
runes = runes[fieldLen:]
}
}
func parse(handle io.Reader, format string) (bytes []byte, err error) {
decoder := charmap.Windows1250.NewDecoder()
decoderReader := decoder.Reader(handle)
linesScanned := 0
statement := Statement{_900: &_900{}, _909: &_909{}, Groups: []Group{}}
reader := bufio.NewReader(decoderReader)
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
runes := []rune(line)
runes = runes[:1000]
strType := string(runes[997:])
if linesScanned == 0 && strType != "900" {
return nil, fmt.Errorf("Invalid line %d with type %s", linesScanned, strType)
}
switch strType {
case "900":
decode(statement._900, runes)
break
case "903":
str := _903{}
decode(&str, runes)
statement.Groups = append(statement.Groups, Group{_903: &str, Transactions: []_905{}})
break
case "905":
str := _905{}
decode(&str, runes)
iz905 := &statement.Groups[len(statement.Groups)-1].Transactions
*iz905 = append(*iz905, str)
break
case "907":
str := _907{}
decode(&str, runes)
statement.Groups[len(statement.Groups)-1]._907 = &str
break
case "909":
decode(statement._909, runes)
break
case "999":
break
default:
return nil, fmt.Errorf("Unsupported type %s on line %d", strType, linesScanned)
}
linesScanned++
}
if format == "xml" {
bytes, err := xml.Marshal(statement)
if err != nil {
return nil, err
}
out := append([]byte(xml.Header), bytes...)
return out, nil
}
return json.Marshal(statement)
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusNotAcceptable), http.StatusNotAcceptable)
return
}
file, _, err := r.FormFile("statement")
defer file.Close()
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
format := r.URL.Query().Get("format")
if len(format) == 0 {
format = "json"
}
if format != "xml" && format != "json" {
http.Error(w, "Invalid format", http.StatusBadRequest)
return
}
bytes, err := parse(file, format)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", fmt.Sprintf("application/%s", format))
w.Write(bytes)
}
func cliMain(format string) {
stat, _ := os.Stdin.Stat()
hasStdin := (stat.Mode() & os.ModeCharDevice) == 0
var handle io.Reader = os.Stdin
if !hasStdin {
file := os.Args[len(os.Args)-1]
_, err := os.Stat(file)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
f, _ := os.Open(file)
defer f.Close()
handle = f
}
bytes, err := parse(handle, format)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(string(bytes))
}
func main() {
isServer := flag.Bool("s", false, "Start as HTTP server")
format := flag.String("f", "json", "Output format ('json' or 'xml')")
flag.Parse()
if *isServer {
addr := ":3001"
fmt.Println(fmt.Sprintf("Listening on %s", addr))
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(addr, nil))
} else {
cliMain(*format)
}
}