/
starlark.go
178 lines (146 loc) · 4.35 KB
/
starlark.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
package importer
import (
"errors"
"fmt"
"github.com/mescanne/goledger/book"
"github.com/mescanne/goledger/cmd/utils"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"math/big"
"os"
)
func getNumber(amount starlark.Value) (*big.Rat, error) {
if amount == starlark.None {
return nil, nil
}
var amt *big.Rat = &big.Rat{}
switch v := amount.(type) {
case starlark.Float:
amt.SetFloat64(float64(v))
case starlark.Int:
amt.SetInt(v.BigInt())
case starlark.String:
_, ok := amt.SetString(string(v))
if !ok {
return nil, fmt.Errorf("invalid amount '%s'", string(v))
}
default:
return nil, fmt.Errorf("not a valid amount type %T (string, int, float)", amount)
}
return amt, nil
}
func (imp *ImportDef) processData(idata starlark.Value, file string, sc string) (*book.Book, error) {
sc, err := utils.GetFileOrStr(sc)
if err != nil {
return nil, err
}
// Make more flexible (esp floats)
resolve.AllowFloat = true
resolve.AllowGlobalReassign = true
resolve.AllowRecursion = true
// Capture user errors
userError := errors.New("user code")
errorf := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var msg string
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "msg", &msg); err != nil {
return nil, err
}
return starlark.None, fmt.Errorf("%w: %s", userError, msg)
}
// Build the book
bbuilder := book.NewBookBuilder()
addf := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
// Mandatory values
var date string
var desc string
var amount starlark.Value
// Optional first account values
var denom int = 1
var ccy string = imp.CCY
var account string = imp.Account
// Optional second account
var amount2 starlark.Value = starlark.None
var ccy2 string = imp.CCY
var denom2 int = 1
var account2 string = ""
// Optional final settling account
var caccount string = imp.CounterAccount
var note string = ""
var lnote string = ""
err := starlark.UnpackArgs(b.Name(), args, kwargs,
"date", &date, "desc", &desc,
"amt", &amount, "ccy?", &ccy, "denom?", &denom, "account?", &account,
"amt2", &amount2, "ccy2?", &ccy2, "denom2?", &denom2, "account2?", &account2,
"caccount?", &caccount,
"note?", ¬e,
"lnote?", &lnote)
if err != nil {
return nil, err
}
// Convert the date
ndate := book.DateFromString(string(date))
if ndate == book.Date(0) {
return nil, fmt.Errorf("invalid date %s", date)
}
// Convert the amount
amt, err := getNumber(amount)
if err != nil {
return nil, err
}
// Divide by denominator if set
amt.Mul(amt, big.NewRat(1, int64(denom)))
// Set the negative of the amount
neg := (&big.Rat{}).Neg(amt)
// Make the transaction
bbuilder.NewTransaction(ndate, string(desc), note)
bbuilder.AddPosting(account, ccy, amt, lnote)
bbuilder.AddPosting(caccount, ccy, neg, "")
// Nothing else to do
if amount2 == starlark.None && account2 == "" {
return starlark.None, nil
}
if amount2 == starlark.None || account2 == "" || ccy2 == ccy {
// Error
}
// Convert the amount2
amt2, err := getNumber(amount2)
if err != nil {
return nil, err
}
// Divide by denominator if set
amt2.Mul(amt2, big.NewRat(1, int64(denom2)))
// Set the negative of the amount
neg2 := (&big.Rat{}).Neg(amt2)
bbuilder.AddPosting(account2, ccy2, amt2, "")
bbuilder.AddPosting(caccount, ccy2, neg2, "")
return starlark.None, nil
}
// The Thread defines the behavior of the built-in 'print' function.
thread := &starlark.Thread{
Name: "main",
Print: func(_ *starlark.Thread, msg string) {
fmt.Printf("; %s\n", msg)
},
}
// This dictionary defines the pre-declared environment.
predeclared := starlark.StringDict{
"data": idata,
"file": starlark.String(file),
"add": starlark.NewBuiltin("add", addf),
"error": starlark.NewBuiltin("error", errorf),
}
// Execute a program.
_, err = starlark.ExecFile(thread, "code", sc, predeclared)
if err != nil {
if errors.Is(err, userError) {
return nil, err
}
// Full backtrace if possible
if evalErr, ok := err.(*starlark.EvalError); ok {
fmt.Fprintf(os.Stderr, "Runtime error stack: %s\n", evalErr.Backtrace())
}
return nil, fmt.Errorf("importing: %w", err)
}
// Build the book -- done!
return bbuilder.Build(), nil
}