debugs
parent
cb9d895161
commit
bece66b5b3
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/ana-ledger/ledger"
|
"gogs.inhome.blapointe.com/ana-ledger/ledger"
|
||||||
|
|
@ -30,6 +31,15 @@ func main() {
|
||||||
|
|
||||||
switch *foo {
|
switch *foo {
|
||||||
case "reg":
|
case "reg":
|
||||||
|
sort.Slice(deltas, func(i, j int) bool {
|
||||||
|
return deltas[i].Debug() < deltas[j].Debug()
|
||||||
|
})
|
||||||
|
deltasSoFar := make(ledger.Deltas, 0)
|
||||||
|
for i := range deltas {
|
||||||
|
deltasSoFar = append(deltasSoFar, deltas[i])
|
||||||
|
bal, _ := deltasSoFar.Like(ledger.LikeName("^" + deltas[i].Name + "$")).Balances()
|
||||||
|
fmt.Printf("%s (%+v)\n", deltas[i].Debug(), bal[deltas[i].Name].Debug())
|
||||||
|
}
|
||||||
case "bal":
|
case "bal":
|
||||||
balances, err := deltas.Balances()
|
balances, err := deltas.Balances()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package ledger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Balances map[string]Balance
|
||||||
|
|
||||||
|
type Balance map[Currency]float64
|
||||||
|
|
||||||
|
func (balances Balances) Debug() string {
|
||||||
|
result := []string{}
|
||||||
|
for k, v := range balances {
|
||||||
|
result = append(result, fmt.Sprintf("%s:[%s]", k, v.Debug()))
|
||||||
|
}
|
||||||
|
return strings.Join(result, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (balance Balance) Debug() string {
|
||||||
|
result := []string{}
|
||||||
|
for k, v := range balance {
|
||||||
|
if k == USD {
|
||||||
|
result = append(result, fmt.Sprintf("%s%.2f", k, v))
|
||||||
|
} else {
|
||||||
|
result = append(result, fmt.Sprintf("%.3f %s", v, k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Join(result, " + ")
|
||||||
|
}
|
||||||
|
|
@ -12,11 +12,11 @@ func (deltas Deltas) Like(like ...Like) Deltas {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (deltas Deltas) Balances() (map[string]map[Currency]float64, error) {
|
func (deltas Deltas) Balances() (Balances, error) {
|
||||||
result := make(map[string]map[Currency]float64)
|
result := make(Balances)
|
||||||
for _, delta := range deltas {
|
for _, delta := range deltas {
|
||||||
if _, ok := result[delta.Name]; !ok {
|
if _, ok := result[delta.Name]; !ok {
|
||||||
result[delta.Name] = make(map[Currency]float64)
|
result[delta.Name] = make(Balance)
|
||||||
}
|
}
|
||||||
if _, ok := result[delta.Name][delta.Currency]; !ok {
|
if _, ok := result[delta.Name][delta.Currency]; !ok {
|
||||||
result[delta.Name][delta.Currency] = 0
|
result[delta.Name][delta.Currency] = 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue