-
Notifications
You must be signed in to change notification settings - Fork 1
/
accounting_data.go
240 lines (208 loc) · 5.43 KB
/
accounting_data.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
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"regexp"
"strconv"
"time"
)
// Validated data from accounting files.
// Aliases referenced in transactions has been expanded, i.e. transactions only
// reference account names.
type AccountingData struct {
aliases map[string]*Alias
transactions []*Transaction
currentDate time.Time
currentDateLineMeta LineMeta
}
type Alias struct {
name string
account AccountName
lineMeta LineMeta
}
const (
Dr = 0
Cr = 1
DrCr = 2
)
type Transaction struct {
date time.Time
amount float64
accounts [DrCr]AccountName
}
func NewAccountingData() *AccountingData {
return &AccountingData{
aliases: make(map[string]*Alias),
}
}
func (data *AccountingData) SetDate(date time.Time, lineMeta *LineMeta) error {
if date.Before(data.currentDate) {
return lineMeta.ErrorAt(fmt.Sprintf("date set to an earlier date '%s' than previous date '%s' set at '%s'",
date.Format(transactionDateFormat),
data.currentDate.Format(transactionDateFormat), &data.currentDateLineMeta))
}
data.currentDate = date
data.currentDateLineMeta = *lineMeta
return nil
}
func (data *AccountingData) GetDate() time.Time {
return data.currentDate
}
func (data *AccountingData) AddAlias(alias string, account AccountName, lineMeta *LineMeta) error {
if t := data.aliases[alias]; t != nil {
return lineMeta.ErrorAt(fmt.Sprintf("alias '%s' redefined, first seen at '%s'",
alias, &t.lineMeta))
}
data.aliases[alias] = &Alias{name: alias, account: account, lineMeta: *lineMeta}
return nil
}
func (data *AccountingData) GetAlias(aliasName string) *Alias {
return data.aliases[aliasName]
}
func (data *AccountingData) AddTransaction(transaction *Transaction) {
data.transactions = append(data.transactions, transaction)
}
// Get all transactions parsed from accounting files.
// This is the sole data used by the report generator.
func (data *AccountingData) Transactions() []*Transaction {
return data.transactions
}
// Read accounting file and store the information in data collection.
func (data *AccountingData) ReadFile(fileName string) error {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNumber := 1
for scanner.Scan() {
line := Line{
meta: LineMeta{
file: fileName,
line: lineNumber,
},
row: bytes.Fields(scanner.Bytes()),
}
lineNumber++
if line.IsComment() {
} else if line.IsAlias() {
err = line.ParseAlias(data)
} else if line.IsDate() {
err = line.ParseDate(data)
} else if line.IsTransaction() {
err = line.ParseTransaction(data)
} else {
return line.meta.ErrorAt("invalid syntax")
}
if err != nil {
// Parsing errors
return err
}
}
if err = scanner.Err(); err != nil {
// Scanning errors
return err
}
return nil
}
const (
transactionDateFormat = "2006-01-02"
)
var aliasNameRegexp = regexp.MustCompile(`^[\p{L}\p{N}-]+$`)
type Line struct {
meta LineMeta
row [][]byte
}
func (line *Line) IsComment() bool {
if len(line.row) == 0 || line.row[0][0] == '#' {
return true
}
return false
}
func (line *Line) IsAlias() bool {
if len(line.row) == 3 && string(line.row[0]) == "alias" {
return true
}
return false
}
func (line *Line) IsDate() bool {
if len(line.row) == 1 && len(line.row[0]) == len(transactionDateFormat) {
return true
}
return false
}
func (line *Line) IsTransaction() bool {
if len(line.row) == (1 + DrCr) {
return true
}
return false
}
func (line *Line) ParseAlias(data *AccountingData) error {
alias := line.row[1]
account := AccountName(line.row[2])
if !aliasNameRegexp.Match(alias) {
return line.meta.ErrorAt("invalid alias name")
}
if !account.Valid() {
return line.meta.ErrorAt("invalid account name reference by alias")
}
return data.AddAlias(string(alias), AccountName(account), &line.meta)
}
func (line *Line) ParseDate(data *AccountingData) error {
date, err := time.Parse(transactionDateFormat, string(line.row[0]))
if err != nil {
return line.meta.ErrorAt("invalid date")
}
return data.SetDate(date, &line.meta)
}
func (line *Line) ParseTransaction(data *AccountingData) error {
amountText := line.row[0]
for _, v := range line.row[0] {
// Accept ',' as well as '.' as decimal point.
if v == ',' {
amountText = bytes.Replace(amountText, []byte(","), []byte("."), -1)
}
}
amount, err := strconv.ParseFloat(string(amountText), 64)
if err != nil {
return line.meta.ErrorAt("invalid value")
}
transaction := &Transaction{
date: data.GetDate(),
amount: amount,
}
if transaction.date.IsZero() {
return line.meta.ErrorAt("transaction without previous date")
}
for i, account := range line.row[1:] {
transaction.accounts[i] = AccountName(account)
if aliasNameRegexp.Match(account) {
if alias := data.GetAlias(string(account)); alias != nil {
transaction.accounts[i] = alias.account
} else {
return line.meta.ErrorAt(fmt.Sprintf("referenced alias '%s' is undefined", account))
}
} else if !transaction.accounts[i].Valid() {
return line.meta.ErrorAt(fmt.Sprintf("invalid account name '%s'", account))
}
}
data.AddTransaction(transaction)
return nil
}
type LineMeta struct {
file string
line int
}
func (meta *LineMeta) String() string {
return fmt.Sprintf("%s:%d", meta.file, meta.line)
}
func (meta *LineMeta) ErrorAt(desc string) error {
if desc == "" {
return fmt.Errorf("error: at '%s'", meta)
} else {
return fmt.Errorf("error: at '%s', %s", meta, desc)
}
}