forked from manishrjain/into-ledger
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transactionlog.go
55 lines (48 loc) 路 1.21 KB
/
transactionlog.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
package main
import (
"strings"
"text/template"
"time"
humanize "github.com/dustin/go-humanize"
uuid "github.com/nu7hatch/gouuid"
)
// Functions to expand capabilities of transaction templates
var funcMap = map[string]interface{}{
"humanFloat": humanize.FormatFloat,
"commaFloat": func(f float64) string {
return humanize.FormatFloat("###,##", f)
},
"uuid": func() (string, error) {
u4, err := uuid.NewV4()
return u4.String(), err
},
}
// TxnTemplate holds values used in transaction templating
type TxnTemplate struct {
Date time.Time
Payee string
To string
From string
Amount float64
Currency string
}
func toTxnTemplate(t Txn) TxnTemplate {
var tt TxnTemplate
tt.Date = t.Date
tt.Payee = t.Desc
tt.To = t.To
tt.From = t.From
tt.Amount = t.Cur
tt.Currency = t.CurName
return tt
}
func newTransactionTemplate(txnTemplateString string) (*template.Template, error) {
return template.New("transaction").Funcs(funcMap).Parse(txnTemplateString)
}
// ledgerFormat formats a string for insertion into a ledger journal, using
// provided template.
func ledgerFormat(t Txn, tmpl *template.Template) string {
var b strings.Builder
tmpl.Execute(&b, toTxnTemplate(t))
return b.String()
}