From 392036f2b05df72be05322a7d330f383299fefce Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 24 Oct 2023 06:19:26 -0600 Subject: [PATCH] dates can be strings i dont care --- ledger/delta.go | 8 ++------ ledger/delta_test.go | 8 ++------ ledger/file.go | 38 ++++++++++++++++++++++++++++++++++---- ledger/file_test.go | 19 +++++-------------- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/ledger/delta.go b/ledger/delta.go index be4c358..019b206 100644 --- a/ledger/delta.go +++ b/ledger/delta.go @@ -1,9 +1,5 @@ package ledger -import ( - "time" -) - type Currency string const ( @@ -11,14 +7,14 @@ const ( ) type Delta struct { - Date time.Time + Date string Account string Value float64 Currency Currency Description string } -func newDelta(d time.Time, desc, acc string, v float64, c string) Delta { +func newDelta(d, desc, acc string, v float64, c string) Delta { return Delta{ Date: d, Account: acc, diff --git a/ledger/delta_test.go b/ledger/delta_test.go index c96bf4d..03f0a44 100644 --- a/ledger/delta_test.go +++ b/ledger/delta_test.go @@ -2,14 +2,10 @@ package ledger import ( "testing" - "time" ) func TestDelta(t *testing.T) { - d, err := time.Parse("2006-01-02", "2099-08-07") - if err != nil { - t.Fatal(err) - } + d := "2099-08-07" delta := newDelta(d, "", "name", 34.56, "$") if delta.Date != d { @@ -26,7 +22,7 @@ func TestDelta(t *testing.T) { } t.Log(delta) - d2, _ := time.Parse("2006-01-02", "2099-09-08") + d2 := "2099-09-08" delta2 := newDelta(d2, "", "name", 11.11, "$") combined := delta.Plus(delta2) diff --git a/ledger/file.go b/ledger/file.go index f788575..1a57187 100644 --- a/ledger/file.go +++ b/ledger/file.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "time" ) type File string @@ -60,7 +59,7 @@ func (file File) Deltas(like ...Like) ([]Delta, error) { } type transaction struct { - date time.Time + date string description string payee string recipients []transactionRecipient @@ -89,7 +88,7 @@ func (file File) transactions() ([]transaction, error) { if !one.empty() { result = append(result, one) } - if errors.Is(err, io.EOF) { + if err == io.EOF { return result, nil } if err != nil { @@ -99,5 +98,36 @@ func (file File) transactions() ([]transaction, error) { } func readTransaction(r io.Reader) (transaction, error) { - return transaction{}, io.EOF + result := transaction{} + var err error + + if result.date, err = readTransactionDate(r); err != nil { + return result, fmt.Errorf("did not find transaction date: %w", err) + } + + if result.description, err = readTransactionDescription(r); err != nil { + return result, fmt.Errorf("did not find transaction description: %w", err) + } + + return transaction{}, errors.New("not impl: reading payee, recipients") +} + +func readTransactionDate(r io.Reader) (string, error) { + var firstByte [1]byte + for firstByte[0] < '0' || firstByte[0] > '9' { + if _, err := r.Read(firstByte[:]); err != nil { + return "", err + } + } + + restOfDate := make([]byte, len("2006-01-02")-1) + if _, err := r.Read(restOfDate); err != nil { + return "", err + } + + return fmt.Sprintf("%s%s", firstByte, restOfDate), nil +} + +func readTransactionDescription(r io.Reader) (string, error) { + return "", io.EOF } diff --git a/ledger/file_test.go b/ledger/file_test.go index 8825d48..5e431c1 100644 --- a/ledger/file_test.go +++ b/ledger/file_test.go @@ -5,35 +5,30 @@ import ( "io" "strings" "testing" - "time" ) func TestFileDeltas(t *testing.T) { - d := func(s string) time.Time { - v, _ := time.Parse("2006-01-02", s) - return v - } happy := []Delta{ { - Date: d("2022-12-12"), + Date: "2022-12-12", Account: "AssetAccount:Cash:Fidelity76", Value: -97.92, Currency: USD, }, { - Date: d("2022-12-12"), + Date: "2022-12-12", Account: "Withdrawal:0:SharedHome:DominionEnergy", Value: 97.92, Currency: USD, }, { - Date: d("2022-12-12"), + Date: "2022-12-12", Account: "AssetAccount:Cash:Fidelity76", Value: -1.00, Currency: USD, }, { - Date: d("2022-12-12"), + Date: "2022-12-12", Account: "Debts:Credit:ChaseFreedomUltdVisa", Value: 1.00, Currency: USD, @@ -75,10 +70,6 @@ func TestFileDeltas(t *testing.T) { } func TestReadTransaction(t *testing.T) { - d := func(s string) time.Time { - v, _ := time.Parse("2006-01-02", s) - return v - } cases := map[string]struct { input string want transaction @@ -101,7 +92,7 @@ func TestReadTransaction(t *testing.T) { C:D $-1.00 `, want: transaction{ - date: d("2003-04-05"), + date: "2003-04-05", description: "Reasoning here", payee: "", recipients: []transactionRecipient{