-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqif.go
93 lines (74 loc) · 1.56 KB
/
qif.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
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
type QifTransaction struct {
Date time.Time
Amount int64
Payee string
Category string
Memo string
Address string
}
func ReadTransactions(contents string) ([]QifTransaction, error) {
transactions := []QifTransaction{}
transaction := QifTransaction{}
for index, line := range strings.Split(contents, "\n") {
if index == 0 {
continue
}
// Date
if (strings.HasPrefix(line, "D")) {
date, err := time.Parse("02/01/2006", line[1:])
check(err)
transaction.Date = date
continue
}
// Amount
if (strings.HasPrefix(line, "T")) {
amountFloat, err := strconv.ParseFloat(line[1:], 64)
if err != nil {
return nil, err
}
amountFloat *= 100
amountInt := int64(amountFloat)
transaction.Amount = amountInt
continue
}
// Payee
if (strings.HasPrefix(line, "P")) {
transaction.Payee = line[1:]
continue
}
// Category
if (strings.HasPrefix(line, "L")) {
transaction.Category = line[1:]
continue
}
// Memo
if (strings.HasPrefix(line, "M")) {
transaction.Memo = line[1:]
continue
}
// Address
if (strings.HasPrefix(line, "A")) {
transaction.Address = line[1:]
continue
}
// Line ending detected
if strings.Trim(line, " ") == "^" {
transactions = append(transactions, transaction)
transaction = QifTransaction{}
continue
}
// file end
if line == "" {
break
}
return nil, fmt.Errorf("unexpected line: '%s'", line)
}
return transactions, nil
}