Quellcode durchsuchen

Updated ability to include comments in ledger

bmallred vor 9 Jahren
Ursprung
Commit
1dbe0e7954
3 geänderte Dateien mit 20 neuen und 10 gelöschten Zeilen
  1. 11 5
      account.go
  2. 6 3
      commandList.go
  3. 3 2
      transaction.go

+ 11 - 5
account.go

@ -15,21 +15,27 @@ type Account struct {
15 15
16 16
// Convert from a string to an account
17 17
func (a *Account) FromString(text string) error {
18
	fields := strings.Split(text, "\t")
18
	parts := strings.Split(text, "\t")
19
	fields := []string{}
20
	for _, p := range parts {
21
		if p != "" {
22
			fields = append(fields, p)
23
		}
24
	}
19 25
20
	if len(fields) != 3 {
26
	if len(fields) != 2 {
21 27
		return errors.New("Invalid account format")
22 28
	}
23 29
24 30
	debit := true
25
	if strings.HasPrefix(fields[2], "-") {
31
	if strings.HasPrefix(fields[1], "-") {
26 32
		debit = false
27 33
	}
28 34
29 35
	a.Debit = debit
30
	a.Name = fields[1]
36
	a.Name = fields[0]
31 37
	a.Amount = new(big.Rat)
32
	a.Amount.SetString(fields[2][1:])
38
	a.Amount.SetString(fields[1][1:])
33 39
34 40
	return nil
35 41
}

+ 6 - 3
commandList.go

@ -5,6 +5,7 @@ import (
5 5
	"bytes"
6 6
	"fmt"
7 7
	"os"
8
	"strings"
8 9
9 10
	"github.com/codegangsta/cli"
10 11
)
@ -53,9 +54,11 @@ func actionList(c *cli.Context) {
53 54
	for scanner.Scan() {
54 55
		text := scanner.Text()
55 56
56
		t := Transaction{}
57
		t.FromString(text)
58
		l.Transactions = append(l.Transactions, t)
57
		if strings.Index(text, "#") != 0 {
58
			t := Transaction{}
59
			t.FromString(text)
60
			l.Transactions = append(l.Transactions, t)
61
		}
59 62
	}
60 63
61 64
	fmt.Print(l.ToString())

+ 3 - 2
transaction.go

@ -70,8 +70,9 @@ func (t *Transaction) CheckBalance() error {
70 70
		}
71 71
	}
72 72
73
	if balance.FloatString(2) != "0.00" {
74
		return errors.New("Transaction does not balance")
73
	b := balance.FloatString(2)
74
	if b != "0.00" {
75
		return errors.New(fmt.Sprintf("Transaction does not balance: %s", b))
75 76
	}
76 77
77 78
	return nil