~mendelmaleh/pfin

ref: 4b06f49cd6d4f9ffc730cc85847a1d7a2e22898c pfin/parser/amex/types.go -rw-r--r-- 1.7 KiB
4b06f49cMendel E Make pfin.Transaction.Amount() consistent 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package amex

import (
	"fmt"
	"strings"

	"git.sr.ht/~mendelmaleh/pfin/parser/util"
)

type Transaction struct {
	Fields // computed fields
	Raw    // csv fields
}

type Fields struct {
	User    string
	Account string
}

type Raw struct {
	Date                     util.DateUS `csv:"Date"`
	Description              string      `csv:"Description"`
	CardMember               string      `csv:"Card Member"`
	AccountNumber            string      `csv:"Account #"`
	Amount                   float64     `csv:"Amount"`
	ExtendedDetails          string      `csv:"Extended Details"`
	AppearsOnYourStatementAs string      `csv:"Appears On Your Statement As"`
	Address                  string      `csv:"Address"`

	CityState `csv:"City/State"`

	ZipCode   string `csv:"Zip Code"`
	Country   string `csv:"Country"`
	Reference string `csv:"Reference"`

	Category `csv:"Category"`
}

type CityState struct {
	City  string
	State string
}

func (c *CityState) UnmarshalText(data []byte) error {
	if len(data) == 0 {
		return nil
	}

	s := strings.Split(string(data), "\n")

	if len(s) >= 1 {
		c.City = s[0]
	}

	if len(s) == 2 {
		c.State = s[1]
	}

	if len(s) > 2 {
		return fmt.Errorf("expected two parts for City/State, got %d (%v, %s)", len(s), s, string(data))
	}

	return nil
}

type Category struct {
	Main string
	Sub  string
}

func (c *Category) UnmarshalText(data []byte) error {
	if len(data) == 0 {
		return nil
	}

	s := strings.Split(string(data), "-")

	if len(s) >= 1 {
		c.Main = s[0]
	}

	if len(s) == 2 {
		c.Sub = s[1]
	}

	if len(s) > 2 {
		return fmt.Errorf("expected two parts for Category, got %d (%v, %s)", len(s), s, string(data))
	}

	return nil
}

func (c *Category) String() string {
	return fmt.Sprintf("%s/%s", c.Main, c.Sub)
}