reduce howeyc
parent
65d6a493d3
commit
7db1eff2b3
|
|
@ -22,17 +22,17 @@ type Delta struct {
|
||||||
func newDeltas(t *ledger.Transaction) []Delta {
|
func newDeltas(t *ledger.Transaction) []Delta {
|
||||||
result := make([]Delta, len(t.AccountChanges))
|
result := make([]Delta, len(t.AccountChanges))
|
||||||
for i, a := range t.AccountChanges {
|
for i, a := range t.AccountChanges {
|
||||||
result[i] = newDelta(t.Date, a)
|
value, _ := a.Balance.Float64()
|
||||||
|
result[i] = newDelta(t.Date, a.Name, value)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDelta(d time.Time, a ledger.Account) Delta {
|
func newDelta(d time.Time, a string, v float64) Delta {
|
||||||
value, _ := a.Balance.Float64()
|
|
||||||
return Delta{
|
return Delta{
|
||||||
Date: d,
|
Date: d,
|
||||||
Account: a.Name,
|
Account: a,
|
||||||
Value: value,
|
Value: v,
|
||||||
Currency: USD, // TODO
|
Currency: USD, // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,6 @@ package ledger
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/howeyc/ledger"
|
|
||||||
"github.com/howeyc/ledger/decimal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDelta(t *testing.T) {
|
func TestDelta(t *testing.T) {
|
||||||
|
|
@ -13,11 +10,7 @@ func TestDelta(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
delta := newDelta(d, ledger.Account{
|
delta := newDelta(d, "name", 34.56)
|
||||||
Name: "name",
|
|
||||||
Balance: decimal.NewFromFloat(34.56),
|
|
||||||
Comment: "comment",
|
|
||||||
})
|
|
||||||
|
|
||||||
if delta.Date != d {
|
if delta.Date != d {
|
||||||
t.Error(delta.Date)
|
t.Error(delta.Date)
|
||||||
|
|
@ -34,11 +27,7 @@ func TestDelta(t *testing.T) {
|
||||||
t.Log(delta)
|
t.Log(delta)
|
||||||
|
|
||||||
d2, _ := time.Parse("2006-01-02", "2099-09-08")
|
d2, _ := time.Parse("2006-01-02", "2099-09-08")
|
||||||
delta2 := newDelta(d2, ledger.Account{
|
delta2 := newDelta(d2, "name", 11.11)
|
||||||
Name: "name",
|
|
||||||
Balance: decimal.NewFromFloat(11.11),
|
|
||||||
Comment: "comment",
|
|
||||||
})
|
|
||||||
|
|
||||||
combined := delta.Plus(delta2)
|
combined := delta.Plus(delta2)
|
||||||
if combined.Date != d2 {
|
if combined.Date != d2 {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package ledger
|
package ledger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/howeyc/ledger"
|
"github.com/howeyc/ledger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -30,5 +32,10 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (file File) transactions() ([]*ledger.Transaction, error) {
|
func (file File) transactions() ([]*ledger.Transaction, error) {
|
||||||
return ledger.ParseLedgerFile(string(file))
|
f, err := os.Open(string(file))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
return ledger.ParseLedger(f)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue