debugs
parent
cb9d895161
commit
bece66b5b3
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gogs.inhome.blapointe.com/ana-ledger/ledger"
|
||||
|
|
@ -30,6 +31,15 @@ func main() {
|
|||
|
||||
switch *foo {
|
||||
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":
|
||||
balances, err := deltas.Balances()
|
||||
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
|
||||
}
|
||||
|
||||
func (deltas Deltas) Balances() (map[string]map[Currency]float64, error) {
|
||||
result := make(map[string]map[Currency]float64)
|
||||
func (deltas Deltas) Balances() (Balances, error) {
|
||||
result := make(Balances)
|
||||
for _, delta := range deltas {
|
||||
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 {
|
||||
result[delta.Name][delta.Currency] = 0
|
||||
|
|
|
|||
Loading…
Reference in New Issue