From 3093b008c1a3a22b01d82456030e991d11e9168b Mon Sep 17 00:00:00 2001 From: Mendel E Date: Mon, 30 May 2022 03:22:01 -0400 Subject: [PATCH] Parse currency --- types.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/types.go b/types.go index d57e6aa..06f2b9c 100644 --- a/types.go +++ b/types.go @@ -3,7 +3,10 @@ package amazon import ( "bytes" "fmt" + "strconv" + "strings" "time" + "unicode" ) // partially generated with git.sr.ht/~mendelmaleh/csvgen @@ -87,6 +90,44 @@ func (t *Tracking) UnmarshalText(data []byte) error { 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"` @@ -111,16 +152,16 @@ type Item struct { UnspscCode string `csv:"UNSPSC Code"` ReleaseDate DateISO `csv:"Release Date"` - Currency string `csv:"Currency"` - ListPricePerUnit string `csv:"List Price Per Unit"` - PurchasePricePerUnit string `csv:"Purchase Price Per Unit"` - TaxExemptionApplied string `csv:"Tax Exemption Applied"` - TaxExemptionType string `csv:"Tax Exemption Type"` - ExemptionOptOut string `csv:"Exemption Opt-Out"` - ItemSubtotal string `csv:"Item Subtotal"` - ItemSubtotalTax string `csv:"Item Subtotal Tax"` - ItemTotal string `csv:"Item Total"` - PoLineNumber string `csv:"PO Line Number"` + Currency string `csv:"Currency"` + ListPricePerUnit Currency `csv:"List Price Per Unit"` + PurchasePricePerUnit Currency `csv:"Purchase Price Per Unit"` + TaxExemptionApplied string `csv:"Tax Exemption Applied"` + TaxExemptionType string `csv:"Tax Exemption Type"` + ExemptionOptOut string `csv:"Exemption Opt-Out"` + ItemSubtotal Currency `csv:"Item Subtotal"` + ItemSubtotalTax Currency `csv:"Item Subtotal Tax"` + ItemTotal Currency `csv:"Item Total"` + PoLineNumber string `csv:"PO Line Number"` } type Order struct { -- 2.45.2