Parcourir la Source

Better parsing of tabs

bmallred 9 ans auparavant
Parent
Commettre
32dd105c0c
3 fichiers modifiés avec 14 ajouts et 9 suppressions
  1. 1 8
      account.go
  2. 12 0
      helpers.go
  3. 1 1
      transaction.go

+ 1 - 8
account.go

@ -15,14 +15,7 @@ type Account struct {
15 15
16 16
// Convert from a string to an account
17 17
func (a *Account) FromString(text string) error {
18
	parts := strings.Split(text, "\t")
19
	fields := []string{}
20
	for _, p := range parts {
21
		if p != "" {
22
			fields = append(fields, p)
23
		}
24
	}
25
18
	fields := parseTabs(text)
26 19
	if len(fields) != 2 {
27 20
		return errors.New("Invalid account format")
28 21
	}

+ 12 - 0
helpers.go

@ -24,6 +24,18 @@ func ensureFileExists(fileName string) {
24 24
	}
25 25
}
26 26
27
// Parse some text separating the fields by tab(s).
28
func parseTabs(text string) []string {
29
	parts := strings.Split(text, "\t")
30
	fields := []string{}
31
	for _, p := range parts {
32
		if p != "" {
33
			fields = append(fields, p)
34
		}
35
	}
36
	return fields
37
}
38
27 39
// Format the ledger so it is human readable
28 40
func formatLedger() {
29 41
}

+ 1 - 1
transaction.go

@ -26,7 +26,7 @@ func (t *Transaction) FromString(text string) {
26 26
27 27
		switch i {
28 28
		case 0:
29
			fields := strings.Split(line, "\t")
29
			fields := parseTabs(text)
30 30
31 31
			date, err := parseDate(fields[0])
32 32
			check(err)