From 83bd10f71c267ea7d47e9d61f96d83b616c9e924 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Mon, 30 May 2022 03:25:10 -0400 Subject: [PATCH] Move components with custom parsing to utils.go --- types.go | 102 ------------------------------------------------------ utils.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 102 deletions(-) create mode 100644 utils.go diff --git a/types.go b/types.go index 77d59de..e9081ef 100644 --- a/types.go +++ b/types.go @@ -1,51 +1,7 @@ package amazon -import ( - "fmt" - "strconv" - "strings" - "time" - "unicode" -) - // partially generated with git.sr.ht/~mendelmaleh/csvgen -type DateUS struct { - time.Time -} - -func (d *DateUS) UnmarshalText(data []byte) error { - if len(data) == 0 { - return nil - } - - t, err := time.Parse("01/02/06", string(data)) - if err != nil { - return err - } - - d.Time = t - return nil -} - -type DateISO struct { - time.Time -} - -func (d *DateISO) UnmarshalText(data []byte) error { - if len(data) == 0 { - return nil - } - - t, err := time.Parse("2006-01-02T15:04:05", string(data)) - if err != nil { - return err - } - - d.Time = t - return nil -} - type Common struct { Shipping Shipping `csv:",inline"` @@ -69,64 +25,6 @@ type Shipping struct { State string `csv:"Shipping Address State"` } -type Tracking struct { - Carrier string - Number string -} - -func (t *Tracking) UnmarshalText(data []byte) error { - parts := strings.FieldsFunc(string(data), func(r rune) bool { - return r == '(' || r == ')' - }) - - if len(parts) != 2 { - return fmt.Errorf("expected 2 parts from tracking, got %d", len(parts)) - } - - t.Carrier = parts[0] - t.Number = parts[1] - - return nil -} - -type Currency struct { - Symbol string - Cents int -} - -func (c *Currency) UnmarshalText(data []byte) error { - if len(data) == 0 { - return nil - } - - trimmed := strings.TrimLeftFunc(string(data), func(r rune) bool { - return !unicode.IsDigit(r) - }) - - if s := len(data) - len(trimmed); s > 0 { - c.Symbol = string(data[:s]) - } - - whole, cents, ok := strings.Cut(trimmed, ".") - if !ok { - return fmt.Errorf("error splitting wholes and cents: %q", data) - } - - iw, err := strconv.Atoi(whole) - if err != nil { - return fmt.Errorf("error parsing wholes: %q", data) - } - - ic, err := strconv.Atoi(cents) - if err != nil { - return fmt.Errorf("error parsing cents: %q", data) - } - - c.Cents = iw*100 + ic - - return nil -} - type OrderInfo struct { Status string `csv:"Order Status"` PaymentInstrumentType string `csv:"Payment Instrument Type"` diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..25cf508 --- /dev/null +++ b/utils.go @@ -0,0 +1,103 @@ +package amazon + +import ( + "fmt" + "strconv" + "strings" + "time" + "unicode" +) + +type DateUS struct { + time.Time +} + +func (d *DateUS) UnmarshalText(data []byte) error { + if len(data) == 0 { + return nil + } + + t, err := time.Parse("01/02/06", string(data)) + if err != nil { + return err + } + + d.Time = t + return nil +} + +type DateISO struct { + time.Time +} + +func (d *DateISO) UnmarshalText(data []byte) error { + if len(data) == 0 { + return nil + } + + t, err := time.Parse("2006-01-02T15:04:05", string(data)) + if err != nil { + return err + } + + d.Time = t + return nil +} + +type Tracking struct { + Carrier string + Number string +} + +func (t *Tracking) UnmarshalText(data []byte) error { + parts := strings.FieldsFunc(string(data), func(r rune) bool { + return r == '(' || r == ')' + }) + + if len(parts) != 2 { + return fmt.Errorf("expected 2 parts from tracking, got %d", len(parts)) + } + + t.Carrier = parts[0] + t.Number = parts[1] + + return nil +} + +type Currency struct { + Symbol string + Cents int +} + +func (c *Currency) UnmarshalText(data []byte) error { + if len(data) == 0 { + return nil + } + + trimmed := strings.TrimLeftFunc(string(data), func(r rune) bool { + return !unicode.IsDigit(r) + }) + + if s := len(data) - len(trimmed); s > 0 { + c.Symbol = string(data[:s]) + } + + whole, cents, ok := strings.Cut(trimmed, ".") + if !ok { + return fmt.Errorf("error splitting wholes and cents: %q", data) + } + + iw, err := strconv.Atoi(whole) + if err != nil { + return fmt.Errorf("error parsing wholes: %q", data) + } + + ic, err := strconv.Atoi(cents) + if err != nil { + return fmt.Errorf("error parsing cents: %q", data) + } + + c.Cents = iw*100 + ic + + return nil +} -- 2.45.2