dates can be strings i dont care
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
package ledger
|
package ledger
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Currency string
|
type Currency string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -11,14 +7,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Delta struct {
|
type Delta struct {
|
||||||
Date time.Time
|
Date string
|
||||||
Account string
|
Account string
|
||||||
Value float64
|
Value float64
|
||||||
Currency Currency
|
Currency Currency
|
||||||
Description string
|
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{
|
return Delta{
|
||||||
Date: d,
|
Date: d,
|
||||||
Account: acc,
|
Account: acc,
|
||||||
|
|||||||
@@ -2,14 +2,10 @@ package ledger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDelta(t *testing.T) {
|
func TestDelta(t *testing.T) {
|
||||||
d, err := time.Parse("2006-01-02", "2099-08-07")
|
d := "2099-08-07"
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
delta := newDelta(d, "", "name", 34.56, "$")
|
delta := newDelta(d, "", "name", 34.56, "$")
|
||||||
|
|
||||||
if delta.Date != d {
|
if delta.Date != d {
|
||||||
@@ -26,7 +22,7 @@ func TestDelta(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log(delta)
|
t.Log(delta)
|
||||||
|
|
||||||
d2, _ := time.Parse("2006-01-02", "2099-09-08")
|
d2 := "2099-09-08"
|
||||||
delta2 := newDelta(d2, "", "name", 11.11, "$")
|
delta2 := newDelta(d2, "", "name", 11.11, "$")
|
||||||
|
|
||||||
combined := delta.Plus(delta2)
|
combined := delta.Plus(delta2)
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type File string
|
type File string
|
||||||
@@ -60,7 +59,7 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type transaction struct {
|
type transaction struct {
|
||||||
date time.Time
|
date string
|
||||||
description string
|
description string
|
||||||
payee string
|
payee string
|
||||||
recipients []transactionRecipient
|
recipients []transactionRecipient
|
||||||
@@ -89,7 +88,7 @@ func (file File) transactions() ([]transaction, error) {
|
|||||||
if !one.empty() {
|
if !one.empty() {
|
||||||
result = append(result, one)
|
result = append(result, one)
|
||||||
}
|
}
|
||||||
if errors.Is(err, io.EOF) {
|
if err == io.EOF {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -99,5 +98,36 @@ func (file File) transactions() ([]transaction, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readTransaction(r io.Reader) (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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,35 +5,30 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFileDeltas(t *testing.T) {
|
func TestFileDeltas(t *testing.T) {
|
||||||
d := func(s string) time.Time {
|
|
||||||
v, _ := time.Parse("2006-01-02", s)
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
happy := []Delta{
|
happy := []Delta{
|
||||||
{
|
{
|
||||||
Date: d("2022-12-12"),
|
Date: "2022-12-12",
|
||||||
Account: "AssetAccount:Cash:Fidelity76",
|
Account: "AssetAccount:Cash:Fidelity76",
|
||||||
Value: -97.92,
|
Value: -97.92,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: d("2022-12-12"),
|
Date: "2022-12-12",
|
||||||
Account: "Withdrawal:0:SharedHome:DominionEnergy",
|
Account: "Withdrawal:0:SharedHome:DominionEnergy",
|
||||||
Value: 97.92,
|
Value: 97.92,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: d("2022-12-12"),
|
Date: "2022-12-12",
|
||||||
Account: "AssetAccount:Cash:Fidelity76",
|
Account: "AssetAccount:Cash:Fidelity76",
|
||||||
Value: -1.00,
|
Value: -1.00,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Date: d("2022-12-12"),
|
Date: "2022-12-12",
|
||||||
Account: "Debts:Credit:ChaseFreedomUltdVisa",
|
Account: "Debts:Credit:ChaseFreedomUltdVisa",
|
||||||
Value: 1.00,
|
Value: 1.00,
|
||||||
Currency: USD,
|
Currency: USD,
|
||||||
@@ -75,10 +70,6 @@ func TestFileDeltas(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReadTransaction(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 {
|
cases := map[string]struct {
|
||||||
input string
|
input string
|
||||||
want transaction
|
want transaction
|
||||||
@@ -101,7 +92,7 @@ func TestReadTransaction(t *testing.T) {
|
|||||||
C:D $-1.00
|
C:D $-1.00
|
||||||
`,
|
`,
|
||||||
want: transaction{
|
want: transaction{
|
||||||
date: d("2003-04-05"),
|
date: "2003-04-05",
|
||||||
description: "Reasoning here",
|
description: "Reasoning here",
|
||||||
payee: "",
|
payee: "",
|
||||||
recipients: []transactionRecipient{
|
recipients: []transactionRecipient{
|
||||||
|
|||||||
Reference in New Issue
Block a user