someday
This commit is contained in:
1
ana/ana.go
Normal file
1
ana/ana.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package ana
|
||||||
7
go.mod
7
go.mod
@@ -1,3 +1,10 @@
|
|||||||
module gogs.inhome.blapointe.com/ana-ledger
|
module gogs.inhome.blapointe.com/ana-ledger
|
||||||
|
|
||||||
go 1.21.1
|
go 1.21.1
|
||||||
|
|
||||||
|
require github.com/howeyc/ledger v1.15.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976 // indirect
|
||||||
|
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7 // indirect
|
||||||
|
)
|
||||||
|
|||||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976 h1:+jyVKPjl5Y39thM0ZlVrRqKjSO/Upr5tP9ZQGELv8gw=
|
||||||
|
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976/go.mod h1:/HQknSiD7YKT15DoHXuiXezQfNPBUm8PeqFaTxeA3HU=
|
||||||
|
github.com/howeyc/ledger v1.15.1 h1:KmCnRrhPJS/eRMqMZqAX3lMrIQumkHPCs3cAvo/dsTs=
|
||||||
|
github.com/howeyc/ledger v1.15.1/go.mod h1:FwnBqmXCItQpDvsDnLmw7JAhvZdoLGqQpfm/gT5YtHA=
|
||||||
|
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7 h1:2wH5antjhmU3EuWyidm0lJ4B9hGMpl5lNRo+M9uGJ5A=
|
||||||
|
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7/go.mod h1:R+UgFL3iylLhx9N4w35zZ2HdhDlgorRDx4SxbchWuN0=
|
||||||
38
ledger/delta.go
Normal file
38
ledger/delta.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package ledger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/howeyc/ledger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Currency string
|
||||||
|
|
||||||
|
const (
|
||||||
|
USD = Currency("$")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Delta struct {
|
||||||
|
Date time.Time
|
||||||
|
Account string
|
||||||
|
Value float64
|
||||||
|
Currency Currency
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDeltas(t *ledger.Transaction) []Delta {
|
||||||
|
result := make([]Delta, len(t.AccountChanges))
|
||||||
|
for i, a := range t.AccountChanges {
|
||||||
|
result[i] = newDelta(t.Date, a)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDelta(d time.Time, a ledger.Account) Delta {
|
||||||
|
value, _ := a.Balance.Float64()
|
||||||
|
return Delta{
|
||||||
|
Date: d,
|
||||||
|
Account: a.Name,
|
||||||
|
Value: value,
|
||||||
|
Currency: USD, // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
41
ledger/file.go
Normal file
41
ledger/file.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package ledger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/howeyc/ledger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type File string
|
||||||
|
|
||||||
|
func NewFile(p string) (File, error) {
|
||||||
|
f := File(p)
|
||||||
|
_, err := f.Deltas()
|
||||||
|
return f, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (file File) Deltas(likes ...Like) ([]Delta, error) {
|
||||||
|
transactions, err := file.transactions()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := make([]Delta, 0, len(transactions)*2)
|
||||||
|
for _, transaction := range transactions {
|
||||||
|
for _, delta := range newDeltas(transaction) {
|
||||||
|
if got := func() bool {
|
||||||
|
for _, like := range likes {
|
||||||
|
if !like(delta) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}(); !got {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, delta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (file File) transactions() ([]*ledger.Transaction, error) {
|
||||||
|
return ledger.ParseLedgerFile(string(file))
|
||||||
|
}
|
||||||
15
ledger/like.go
Normal file
15
ledger/like.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package ledger
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
type Like func(Delta) bool
|
||||||
|
|
||||||
|
func LikeAcc(pattern string) Like {
|
||||||
|
return func(d Delta) bool {
|
||||||
|
return like(pattern, d.Account)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func like(pattern string, other string) bool {
|
||||||
|
return regexp.MustCompile(pattern).MatchString(other)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user