From 12aba53395c295880638f7f58f3a00271682bb58 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 29 Jul 2026 15:02:07 -0230 Subject: shorten cost datastructure names --- cmd/lulu/cost.go | 16 +++++++------- cost.go | 37 ++++++++++++-------------------- cost_test.go | 65 ++++++++++++++++++++++++++------------------------------ lulu.go | 17 ++++----------- print.go | 2 +- 5 files changed, 56 insertions(+), 81 deletions(-) diff --git a/cmd/lulu/cost.go b/cmd/lulu/cost.go index 26f641e..1ef655d 100644 --- a/cmd/lulu/cost.go +++ b/cmd/lulu/cost.go @@ -15,14 +15,14 @@ import ( type CostCmd struct { ShippingAddress - Ship lulu.ShippingLevel `required help:"${ship_level_help}"` - Items []PrintJobCostLineItem `required help:"${cost_item_help}"` + Ship lulu.ShippingLevel `required help:"${ship_level_help}"` + Items []CostLineItem `required help:"${cost_item_help}"` } func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { - items := make([]lulu.PrintJobCostLineItem, len(cmd.Items)) + items := make([]lulu.CostLineItem, len(cmd.Items)) for i := range cmd.Items { - items[i] = cmd.Items[i].PrintJobCostLineItem + items[i] = cmd.Items[i].CostLineItem } cost, addrVal, err := clnt.Cost(items, cmd.ShippingAddress.Addr(), cmd.Ship) if err != nil { @@ -42,11 +42,11 @@ func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { return w.Flush() } -type PrintJobCostLineItem struct { - lulu.PrintJobCostLineItem +type CostLineItem struct { + lulu.CostLineItem } -func (item *PrintJobCostLineItem) UnmarshalText(text []byte) error { +func (item *CostLineItem) UnmarshalText(text []byte) error { s := string(text) groups := printJobCostLineItemExpr.FindStringSubmatch(s) if len(groups) != 3+1 { @@ -72,7 +72,7 @@ func (item *PrintJobCostLineItem) UnmarshalText(text []byte) error { return nil } -func printCost(w io.Writer, cost lulu.PrintJobCost) { +func printCost(w io.Writer, cost lulu.Cost) { for _, fee := range cost.Fees { fmt.Fprintf(w, "fee %s %s:\t%s %s\t\n", fee.Type, fee.Sku, fee.TotalCostExclTax, fee.Currency) } diff --git a/cost.go b/cost.go index de2080e..2e10669 100644 --- a/cost.go +++ b/cost.go @@ -2,13 +2,8 @@ package lulu import "github.com/shopspring/decimal" -type PrintJobCostLineItem struct { - NPages uint `json:"page_count"` - Mfg PkgId `json:"pod_package_id"` - Quantity uint `json:"quantity"` -} - -type PrintJobCost struct { +// Cost is the cost of a print job. +type Cost struct { Fees []Fee `json:"fees"` LineItemCosts []LineItemCost `json:"line_item_costs"` ShipCost FulfillmentCost `json:"shipping_cost"` @@ -54,25 +49,19 @@ type Discount struct { Description string `json:"description"` } -type printJobCostReq struct { - LineItems []PrintJobCostLineItem `json:"line_items"` - ShipAddr printJobCostReqShipAddr `json:"shipping_address"` - ShipOpt ShippingLevel `json:"shipping_option"` +type CostLineItem struct { + NPages uint `json:"page_count"` + Mfg PkgId `json:"pod_package_id"` + Quantity uint `json:"quantity"` } -// printJobCostReqShipAddr is a subset of a shipping address. It is -// necessary because the API returns an error if the address in a -// /print-job-cost-calculations/ request has any extra fields. -type printJobCostReqShipAddr struct { - City string `json:"city"` - Country string `json:"country_code"` - PostCode string `json:"postcode"` - State string `json:"state_code"` - Street1 string `json:"street1"` - Phone PhoneNumber `json:"phone_number"` +type costReq struct { + Items []CostLineItem `json:"line_items"` + ShipAddr ShippingAddress `json:"shipping_address"` + ShipOpt ShippingLevel `json:"shipping_option"` } -type printJobCostResp struct { +type costResp struct { AddressValidation ShippingAddressValidation `json:"shipping_address"` Fees []Fee `json:"fees"` LineItemCosts []LineItemCost `json:"line_item_costs"` @@ -85,8 +74,8 @@ type printJobCostResp struct { Currency string `json:"currency"` } -func (resp printJobCostResp) cost() PrintJobCost { - return PrintJobCost{ +func (resp costResp) cost() Cost { + return Cost{ Fees: resp.Fees, LineItemCosts: resp.LineItemCosts, ShipCost: resp.ShipCost, diff --git a/cost_test.go b/cost_test.go index c2f18ec..a02c125 100644 --- a/cost_test.go +++ b/cost_test.go @@ -17,8 +17,8 @@ var ( costRespJson string ) -var costReqSample = printJobCostReq{ - []PrintJobCostLineItem{ +var costReqSample = costReq{ + []CostLineItem{ { 32, PkgId{UsTrade, Mono, Standard, Perfect, P60UncoatedWhite, Matte, NoLinen, NoFoil}, @@ -29,36 +29,32 @@ var costReqSample = printJobCostReq{ 200, }, }, - printJobCostReqShipAddr{ - City: "Lübeck", - Country: "DE", - PostCode: "23552", - State: "", - Street1: "Holstenstr. 40", - Phone: MustParsePhoneNumber("844-212-0689"), + ShippingAddress{ + City: "Lübeck", + CountryCode: "DE", + PostCode: "23552", + Street1: "Holstenstr. 40", + Phone: MustParsePhoneNumber("844-212-0689"), }, Express, } -var printJobCostRespSample = printJobCostResp{ +var costRespSample = costResp{ AddressValidation: ShippingAddressValidation{ Address: ShippingAddress{ - City: "Lübeck", - PostCode: "23552", - Street1: "Holstenstr. 40", - Phone: MustParsePhoneNumber("844-212-0689"), - State: "", - Country: "DE", - IsBusiness: false, - Name: ". .", + City: "Lübeck", + PostCode: "23552", + Street1: "Holstenstr. 40", + Phone: MustParsePhoneNumber("844-212-0689"), + CountryCode: "DE", + IsBusiness: false, + Name: ". .", }, Suggested: ShippingAddress{ - Country: "DE", - State: "", - PostCode: "23552", - City: "Lübeck", - Street1: "Holstenstraße 40", - Street2: "", + CountryCode: "DE", + PostCode: "23552", + City: "Lübeck", + Street1: "Holstenstraße 40", }, Warnings: []ShippingAddressWarning{{ "validation_warning", @@ -128,29 +124,28 @@ var printJobCostRespSample = printJobCostResp{ Currency: "CAD", } -func TestMarshalPrintJobCostReq(t *testing.T) { +func TestMarshalCostReq(t *testing.T) { requireMarshalJsonEq(t, costReqJson, costReqSample) } -func TestUnmarshalPrintJobCostResp(t *testing.T) { - requireUnmarshalJsonEq(t, printJobCostRespSample, costRespJson) +func TestUnmarshalCostResp(t *testing.T) { + requireUnmarshalJsonEq(t, costRespSample, costRespJson) } func TestCost(t *testing.T) { c := newClient(t) - items := costReqSample.LineItems + items := costReqSample.Items addr := ShippingAddress{ - City: "Lübeck", - Country: "DE", - PostCode: "23552", - State: "", - Street1: "Holstenstr. 40", - Phone: MustParsePhoneNumber("844-212-0689"), + City: "Lübeck", + CountryCode: "DE", + PostCode: "23552", + Street1: "Holstenstr. 40", + Phone: MustParsePhoneNumber("844-212-0689"), } shiplvl := costReqSample.ShipOpt cost, av, err := c.Cost(items, addr, shiplvl) require.NoError(t, err) - require.Equal(t, printJobCostRespSample.AddressValidation, av) + require.Equal(t, costRespSample.AddressValidation, av) requireCurrency(t, cost.Currency) for _, fee := range cost.Fees { requireCurrency(t, fee.Currency) diff --git a/lulu.go b/lulu.go index 8532052..ae7bf23 100644 --- a/lulu.go +++ b/lulu.go @@ -227,21 +227,12 @@ func (c *Client) GetCoverValidation(id uint) (CoverValidation, error) { // actually creating a print job. // // https://api.lulu.com/docs/#tag/Print-Job-Cost-Calculations/operation/Print-Job-cost-calculations_create -func (c *Client) Cost(items []PrintJobCostLineItem, addr ShippingAddress, shipOpt ShippingLevel) (PrintJobCost, ShippingAddressValidation, error) { - reqAddr := printJobCostReqShipAddr{ - City: addr.City, - Country: addr.Country, - PostCode: addr.PostCode, - State: addr.State, - Street1: addr.Street1, - Phone: addr.Phone, - } - payload := printJobCostReq{items, reqAddr, shipOpt} - - var resp printJobCostResp +func (c *Client) Cost(items []CostLineItem, addr ShippingAddress, shipOpt ShippingLevel) (Cost, ShippingAddressValidation, error) { + payload := costReq{items, addr, shipOpt} + var resp costResp err := c.postDecode(printJobCostPath, payload, http.StatusCreated, &resp) if err != nil { - return PrintJobCost{}, ShippingAddressValidation{}, pkgErr(err) + return Cost{}, ShippingAddressValidation{}, pkgErr(err) } return resp.cost(), resp.AddressValidation, nil } diff --git a/print.go b/print.go index 930950c..8157eb0 100644 --- a/print.go +++ b/print.go @@ -83,7 +83,7 @@ type PrintJob struct { // developer or shop owner, not the end customer. Contact EmailAddress `json:"contact_email"` - Cost PrintJobCost `json:"costs"` + Cost Cost `json:"costs"` Created time.Time `json:"date_created"` Modified time.Time `json:"date_modified"` -- cgit v1.2.3