From a128ab02ef588b6a52c3dfe41de3bf094afd8f23 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Wed, 27 Jun 2018 13:25:32 -0700 Subject: [PATCH] checkoutservice: money proto migration Signed-off-by: Ahmet Alp Balkan --- src/checkoutservice/genproto/demo.pb.go | 359 +++++++++++------------- src/checkoutservice/main.go | 27 +- src/checkoutservice/money.go | 44 --- src/checkoutservice/money/money.go | 118 ++++++++ src/checkoutservice/money/money_test.go | 231 +++++++++++++++ src/checkoutservice/money_test.go | 53 ---- 6 files changed, 523 insertions(+), 309 deletions(-) delete mode 100644 src/checkoutservice/money.go create mode 100644 src/checkoutservice/money/money.go create mode 100644 src/checkoutservice/money/money_test.go delete mode 100644 src/checkoutservice/money_test.go diff --git a/src/checkoutservice/genproto/demo.pb.go b/src/checkoutservice/genproto/demo.pb.go index d547682..7e64573 100644 --- a/src/checkoutservice/genproto/demo.pb.go +++ b/src/checkoutservice/genproto/demo.pb.go @@ -35,7 +35,7 @@ func (m *CartItem) Reset() { *m = CartItem{} } func (m *CartItem) String() string { return proto.CompactTextString(m) } func (*CartItem) ProtoMessage() {} func (*CartItem) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{0} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{0} } func (m *CartItem) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CartItem.Unmarshal(m, b) @@ -81,7 +81,7 @@ func (m *AddItemRequest) Reset() { *m = AddItemRequest{} } func (m *AddItemRequest) String() string { return proto.CompactTextString(m) } func (*AddItemRequest) ProtoMessage() {} func (*AddItemRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{1} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{1} } func (m *AddItemRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AddItemRequest.Unmarshal(m, b) @@ -126,7 +126,7 @@ func (m *EmptyCartRequest) Reset() { *m = EmptyCartRequest{} } func (m *EmptyCartRequest) String() string { return proto.CompactTextString(m) } func (*EmptyCartRequest) ProtoMessage() {} func (*EmptyCartRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{2} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{2} } func (m *EmptyCartRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_EmptyCartRequest.Unmarshal(m, b) @@ -164,7 +164,7 @@ func (m *GetCartRequest) Reset() { *m = GetCartRequest{} } func (m *GetCartRequest) String() string { return proto.CompactTextString(m) } func (*GetCartRequest) ProtoMessage() {} func (*GetCartRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{3} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{3} } func (m *GetCartRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GetCartRequest.Unmarshal(m, b) @@ -203,7 +203,7 @@ func (m *Cart) Reset() { *m = Cart{} } func (m *Cart) String() string { return proto.CompactTextString(m) } func (*Cart) ProtoMessage() {} func (*Cart) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{4} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{4} } func (m *Cart) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Cart.Unmarshal(m, b) @@ -247,7 +247,7 @@ func (m *Empty) Reset() { *m = Empty{} } func (m *Empty) String() string { return proto.CompactTextString(m) } func (*Empty) ProtoMessage() {} func (*Empty) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{5} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{5} } func (m *Empty) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Empty.Unmarshal(m, b) @@ -279,7 +279,7 @@ func (m *ListRecommendationsRequest) Reset() { *m = ListRecommendationsR func (m *ListRecommendationsRequest) String() string { return proto.CompactTextString(m) } func (*ListRecommendationsRequest) ProtoMessage() {} func (*ListRecommendationsRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{6} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{6} } func (m *ListRecommendationsRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ListRecommendationsRequest.Unmarshal(m, b) @@ -324,7 +324,7 @@ func (m *ListRecommendationsResponse) Reset() { *m = ListRecommendations func (m *ListRecommendationsResponse) String() string { return proto.CompactTextString(m) } func (*ListRecommendationsResponse) ProtoMessage() {} func (*ListRecommendationsResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{7} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{7} } func (m *ListRecommendationsResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ListRecommendationsResponse.Unmarshal(m, b) @@ -352,21 +352,21 @@ func (m *ListRecommendationsResponse) GetProductIds() []string { } type Product struct { - Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` - Description string `protobuf:"bytes,3,opt,name=description" json:"description,omitempty"` - Picture string `protobuf:"bytes,4,opt,name=picture" json:"picture,omitempty"` - PriceUsd *MoneyAmount `protobuf:"bytes,5,opt,name=price_usd,json=priceUsd" json:"price_usd,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` + Description string `protobuf:"bytes,3,opt,name=description" json:"description,omitempty"` + Picture string `protobuf:"bytes,4,opt,name=picture" json:"picture,omitempty"` + PriceUsd *Money `protobuf:"bytes,5,opt,name=price_usd,json=priceUsd" json:"price_usd,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Product) Reset() { *m = Product{} } func (m *Product) String() string { return proto.CompactTextString(m) } func (*Product) ProtoMessage() {} func (*Product) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{8} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{8} } func (m *Product) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Product.Unmarshal(m, b) @@ -414,7 +414,7 @@ func (m *Product) GetPicture() string { return "" } -func (m *Product) GetPriceUsd() *MoneyAmount { +func (m *Product) GetPriceUsd() *Money { if m != nil { return m.PriceUsd } @@ -432,7 +432,7 @@ func (m *ListProductsResponse) Reset() { *m = ListProductsResponse{} } func (m *ListProductsResponse) String() string { return proto.CompactTextString(m) } func (*ListProductsResponse) ProtoMessage() {} func (*ListProductsResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{9} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{9} } func (m *ListProductsResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ListProductsResponse.Unmarshal(m, b) @@ -470,7 +470,7 @@ func (m *GetProductRequest) Reset() { *m = GetProductRequest{} } func (m *GetProductRequest) String() string { return proto.CompactTextString(m) } func (*GetProductRequest) ProtoMessage() {} func (*GetProductRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{10} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{10} } func (m *GetProductRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GetProductRequest.Unmarshal(m, b) @@ -508,7 +508,7 @@ func (m *SearchProductsRequest) Reset() { *m = SearchProductsRequest{} } func (m *SearchProductsRequest) String() string { return proto.CompactTextString(m) } func (*SearchProductsRequest) ProtoMessage() {} func (*SearchProductsRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{11} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{11} } func (m *SearchProductsRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_SearchProductsRequest.Unmarshal(m, b) @@ -546,7 +546,7 @@ func (m *SearchProductsResponse) Reset() { *m = SearchProductsResponse{} func (m *SearchProductsResponse) String() string { return proto.CompactTextString(m) } func (*SearchProductsResponse) ProtoMessage() {} func (*SearchProductsResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{12} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{12} } func (m *SearchProductsResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_SearchProductsResponse.Unmarshal(m, b) @@ -585,7 +585,7 @@ func (m *GetQuoteRequest) Reset() { *m = GetQuoteRequest{} } func (m *GetQuoteRequest) String() string { return proto.CompactTextString(m) } func (*GetQuoteRequest) ProtoMessage() {} func (*GetQuoteRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{13} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{13} } func (m *GetQuoteRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GetQuoteRequest.Unmarshal(m, b) @@ -620,17 +620,17 @@ func (m *GetQuoteRequest) GetItems() []*CartItem { } type GetQuoteResponse struct { - CostUsd *MoneyAmount `protobuf:"bytes,1,opt,name=cost_usd,json=costUsd" json:"cost_usd,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + CostUsd *Money `protobuf:"bytes,1,opt,name=cost_usd,json=costUsd" json:"cost_usd,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *GetQuoteResponse) Reset() { *m = GetQuoteResponse{} } func (m *GetQuoteResponse) String() string { return proto.CompactTextString(m) } func (*GetQuoteResponse) ProtoMessage() {} func (*GetQuoteResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{14} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{14} } func (m *GetQuoteResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GetQuoteResponse.Unmarshal(m, b) @@ -650,7 +650,7 @@ func (m *GetQuoteResponse) XXX_DiscardUnknown() { var xxx_messageInfo_GetQuoteResponse proto.InternalMessageInfo -func (m *GetQuoteResponse) GetCostUsd() *MoneyAmount { +func (m *GetQuoteResponse) GetCostUsd() *Money { if m != nil { return m.CostUsd } @@ -669,7 +669,7 @@ func (m *ShipOrderRequest) Reset() { *m = ShipOrderRequest{} } func (m *ShipOrderRequest) String() string { return proto.CompactTextString(m) } func (*ShipOrderRequest) ProtoMessage() {} func (*ShipOrderRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{15} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{15} } func (m *ShipOrderRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ShipOrderRequest.Unmarshal(m, b) @@ -714,7 +714,7 @@ func (m *ShipOrderResponse) Reset() { *m = ShipOrderResponse{} } func (m *ShipOrderResponse) String() string { return proto.CompactTextString(m) } func (*ShipOrderResponse) ProtoMessage() {} func (*ShipOrderResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{16} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{16} } func (m *ShipOrderResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ShipOrderResponse.Unmarshal(m, b) @@ -756,7 +756,7 @@ func (m *Address) Reset() { *m = Address{} } func (m *Address) String() string { return proto.CompactTextString(m) } func (*Address) ProtoMessage() {} func (*Address) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{17} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{17} } func (m *Address) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Address.Unmarshal(m, b) @@ -811,68 +811,30 @@ func (m *Address) GetZipCode() int32 { return 0 } -// Describes a money amount without currency. For example, decimal=2 and -// fractional=500 (or fractional=5) makes up 2.5 units. -type MoneyAmount struct { - Decimal uint32 `protobuf:"varint,1,opt,name=decimal" json:"decimal,omitempty"` - Fractional uint32 `protobuf:"varint,2,opt,name=fractional" json:"fractional,omitempty"` +// Represents an amount of money with its currency type. +type Money struct { + // The 3-letter currency code defined in ISO 4217. + CurrencyCode string `protobuf:"bytes,1,opt,name=currency_code,json=currencyCode" json:"currency_code,omitempty"` + // The whole units of the amount. + // For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar. + Units int64 `protobuf:"varint,2,opt,name=units" json:"units,omitempty"` + // Number of nano (10^-9) units of the amount. + // The value must be between -999,999,999 and +999,999,999 inclusive. + // If `units` is positive, `nanos` must be positive or zero. + // If `units` is zero, `nanos` can be positive, zero, or negative. + // If `units` is negative, `nanos` must be negative or zero. + // For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000. + Nanos int32 `protobuf:"varint,3,opt,name=nanos" json:"nanos,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` } -func (m *MoneyAmount) Reset() { *m = MoneyAmount{} } -func (m *MoneyAmount) String() string { return proto.CompactTextString(m) } -func (*MoneyAmount) ProtoMessage() {} -func (*MoneyAmount) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{18} -} -func (m *MoneyAmount) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_MoneyAmount.Unmarshal(m, b) -} -func (m *MoneyAmount) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_MoneyAmount.Marshal(b, m, deterministic) -} -func (dst *MoneyAmount) XXX_Merge(src proto.Message) { - xxx_messageInfo_MoneyAmount.Merge(dst, src) -} -func (m *MoneyAmount) XXX_Size() int { - return xxx_messageInfo_MoneyAmount.Size(m) -} -func (m *MoneyAmount) XXX_DiscardUnknown() { - xxx_messageInfo_MoneyAmount.DiscardUnknown(m) -} - -var xxx_messageInfo_MoneyAmount proto.InternalMessageInfo - -func (m *MoneyAmount) GetDecimal() uint32 { - if m != nil { - return m.Decimal - } - return 0 -} - -func (m *MoneyAmount) GetFractional() uint32 { - if m != nil { - return m.Fractional - } - return 0 -} - -type Money struct { - // The 3-letter currency code defined in ISO 4217. - CurrencyCode string `protobuf:"bytes,1,opt,name=currency_code,json=currencyCode" json:"currency_code,omitempty"` - Amount *MoneyAmount `protobuf:"bytes,2,opt,name=amount" json:"amount,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - func (m *Money) Reset() { *m = Money{} } func (m *Money) String() string { return proto.CompactTextString(m) } func (*Money) ProtoMessage() {} func (*Money) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{19} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{18} } func (m *Money) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Money.Unmarshal(m, b) @@ -899,11 +861,18 @@ func (m *Money) GetCurrencyCode() string { return "" } -func (m *Money) GetAmount() *MoneyAmount { +func (m *Money) GetUnits() int64 { if m != nil { - return m.Amount + return m.Units } - return nil + return 0 +} + +func (m *Money) GetNanos() int32 { + if m != nil { + return m.Nanos + } + return 0 } type GetSupportedCurrenciesResponse struct { @@ -918,7 +887,7 @@ func (m *GetSupportedCurrenciesResponse) Reset() { *m = GetSupportedCurr func (m *GetSupportedCurrenciesResponse) String() string { return proto.CompactTextString(m) } func (*GetSupportedCurrenciesResponse) ProtoMessage() {} func (*GetSupportedCurrenciesResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{20} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{19} } func (m *GetSupportedCurrenciesResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GetSupportedCurrenciesResponse.Unmarshal(m, b) @@ -958,7 +927,7 @@ func (m *CurrencyConversionRequest) Reset() { *m = CurrencyConversionReq func (m *CurrencyConversionRequest) String() string { return proto.CompactTextString(m) } func (*CurrencyConversionRequest) ProtoMessage() {} func (*CurrencyConversionRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{21} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{20} } func (m *CurrencyConversionRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CurrencyConversionRequest.Unmarshal(m, b) @@ -1006,7 +975,7 @@ func (m *CreditCardInfo) Reset() { *m = CreditCardInfo{} } func (m *CreditCardInfo) String() string { return proto.CompactTextString(m) } func (*CreditCardInfo) ProtoMessage() {} func (*CreditCardInfo) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{22} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{21} } func (m *CreditCardInfo) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CreditCardInfo.Unmarshal(m, b) @@ -1066,7 +1035,7 @@ func (m *ChargeRequest) Reset() { *m = ChargeRequest{} } func (m *ChargeRequest) String() string { return proto.CompactTextString(m) } func (*ChargeRequest) ProtoMessage() {} func (*ChargeRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{23} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{22} } func (m *ChargeRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ChargeRequest.Unmarshal(m, b) @@ -1111,7 +1080,7 @@ func (m *ChargeResponse) Reset() { *m = ChargeResponse{} } func (m *ChargeResponse) String() string { return proto.CompactTextString(m) } func (*ChargeResponse) ProtoMessage() {} func (*ChargeResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{24} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{23} } func (m *ChargeResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ChargeResponse.Unmarshal(m, b) @@ -1150,7 +1119,7 @@ func (m *OrderItem) Reset() { *m = OrderItem{} } func (m *OrderItem) String() string { return proto.CompactTextString(m) } func (*OrderItem) ProtoMessage() {} func (*OrderItem) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{25} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{24} } func (m *OrderItem) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_OrderItem.Unmarshal(m, b) @@ -1199,7 +1168,7 @@ func (m *OrderResult) Reset() { *m = OrderResult{} } func (m *OrderResult) String() string { return proto.CompactTextString(m) } func (*OrderResult) ProtoMessage() {} func (*OrderResult) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{26} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{25} } func (m *OrderResult) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_OrderResult.Unmarshal(m, b) @@ -1266,7 +1235,7 @@ func (m *SendOrderConfirmationRequest) Reset() { *m = SendOrderConfirmat func (m *SendOrderConfirmationRequest) String() string { return proto.CompactTextString(m) } func (*SendOrderConfirmationRequest) ProtoMessage() {} func (*SendOrderConfirmationRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{27} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{26} } func (m *SendOrderConfirmationRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_SendOrderConfirmationRequest.Unmarshal(m, b) @@ -1313,7 +1282,7 @@ func (m *CreateOrderRequest) Reset() { *m = CreateOrderRequest{} } func (m *CreateOrderRequest) String() string { return proto.CompactTextString(m) } func (*CreateOrderRequest) ProtoMessage() {} func (*CreateOrderRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{28} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{27} } func (m *CreateOrderRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CreateOrderRequest.Unmarshal(m, b) @@ -1366,7 +1335,7 @@ func (m *CreateOrderResponse) Reset() { *m = CreateOrderResponse{} } func (m *CreateOrderResponse) String() string { return proto.CompactTextString(m) } func (*CreateOrderResponse) ProtoMessage() {} func (*CreateOrderResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{29} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{28} } func (m *CreateOrderResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CreateOrderResponse.Unmarshal(m, b) @@ -1415,7 +1384,7 @@ func (m *PlaceOrderRequest) Reset() { *m = PlaceOrderRequest{} } func (m *PlaceOrderRequest) String() string { return proto.CompactTextString(m) } func (*PlaceOrderRequest) ProtoMessage() {} func (*PlaceOrderRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{30} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{29} } func (m *PlaceOrderRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_PlaceOrderRequest.Unmarshal(m, b) @@ -1481,7 +1450,7 @@ func (m *PlaceOrderResponse) Reset() { *m = PlaceOrderResponse{} } func (m *PlaceOrderResponse) String() string { return proto.CompactTextString(m) } func (*PlaceOrderResponse) ProtoMessage() {} func (*PlaceOrderResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_demo_40506d781b7ed975, []int{31} + return fileDescriptor_demo_be349a9cb1cf0a0c, []int{30} } func (m *PlaceOrderResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_PlaceOrderResponse.Unmarshal(m, b) @@ -1527,7 +1496,6 @@ func init() { proto.RegisterType((*ShipOrderRequest)(nil), "hipstershop.ShipOrderRequest") proto.RegisterType((*ShipOrderResponse)(nil), "hipstershop.ShipOrderResponse") proto.RegisterType((*Address)(nil), "hipstershop.Address") - proto.RegisterType((*MoneyAmount)(nil), "hipstershop.MoneyAmount") proto.RegisterType((*Money)(nil), "hipstershop.Money") proto.RegisterType((*GetSupportedCurrenciesResponse)(nil), "hipstershop.GetSupportedCurrenciesResponse") proto.RegisterType((*CurrencyConversionRequest)(nil), "hipstershop.CurrencyConversionRequest") @@ -2294,100 +2262,99 @@ var _CheckoutService_serviceDesc = grpc.ServiceDesc{ Metadata: "demo.proto", } -func init() { proto.RegisterFile("demo.proto", fileDescriptor_demo_40506d781b7ed975) } +func init() { proto.RegisterFile("demo.proto", fileDescriptor_demo_be349a9cb1cf0a0c) } -var fileDescriptor_demo_40506d781b7ed975 = []byte{ - // 1466 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x58, 0xdd, 0x72, 0xd3, 0xc6, - 0x17, 0x8f, 0x9c, 0x38, 0xb6, 0x8f, 0x63, 0x27, 0x59, 0x12, 0xfe, 0x46, 0x81, 0x90, 0xff, 0x66, - 0x4a, 0x43, 0xa1, 0x19, 0x30, 0xed, 0x70, 0x51, 0x5a, 0xca, 0x98, 0x8c, 0xf1, 0x0c, 0x14, 0xaa, - 0x40, 0xa7, 0x1d, 0x3a, 0x78, 0x84, 0xb4, 0xc1, 0x2a, 0xd6, 0x07, 0xab, 0x55, 0xa6, 0x66, 0x7a, - 0x45, 0x5f, 0xa4, 0x57, 0xbd, 0xe8, 0x03, 0xb4, 0xef, 0xd0, 0xfb, 0xbe, 0x42, 0x9f, 0xa3, 0xb3, - 0xab, 0x5d, 0x7d, 0x59, 0x4a, 0xc2, 0x4d, 0x7b, 0xe7, 0xdd, 0xfd, 0xed, 0x39, 0xbf, 0x3d, 0xdf, - 0x32, 0x80, 0x4d, 0x5c, 0x7f, 0x3f, 0xa0, 0x3e, 0xf3, 0x51, 0x7b, 0xe2, 0x04, 0x21, 0x23, 0x34, - 0x9c, 0xf8, 0x01, 0x3e, 0x80, 0xe6, 0xc0, 0xa4, 0x6c, 0xc4, 0x88, 0x8b, 0x2e, 0x01, 0x04, 0xd4, - 0xb7, 0x23, 0x8b, 0x8d, 0x1d, 0xbb, 0xa7, 0xed, 0x68, 0x7b, 0x2d, 0xa3, 0x25, 0x77, 0x46, 0x36, - 0xd2, 0xa1, 0xf9, 0x26, 0x32, 0x3d, 0xe6, 0xb0, 0x59, 0xaf, 0xb6, 0xa3, 0xed, 0xd5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0x0b, 0x29, 0x33, 0xd7, 0x1e, - 0xf4, 0xcb, 0x95, 0xe7, 0x4a, 0xe0, 0xcb, 0x65, 0xf1, 0xf7, 0xcc, 0xad, 0x7f, 0x02, 0x00, 0x00, - 0xff, 0xff, 0xdd, 0xe8, 0xfb, 0x77, 0xac, 0x11, 0x00, 0x00, +var fileDescriptor_demo_be349a9cb1cf0a0c = []byte{ + // 1442 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x58, 0xdf, 0x72, 0xd3, 0xc6, + 0x17, 0x8e, 0x92, 0x38, 0x8e, 0x8f, 0x63, 0x27, 0x59, 0x12, 0x7e, 0x46, 0xe1, 0x4f, 0x7e, 0x9b, + 0x29, 0x0d, 0x05, 0x52, 0x70, 0x3b, 0xc3, 0x45, 0x69, 0x29, 0x63, 0x32, 0xc6, 0x33, 0x50, 0xa8, + 0x02, 0x1d, 0x3a, 0x74, 0xea, 0x11, 0xd2, 0x82, 0x55, 0x22, 0xad, 0x58, 0xad, 0x32, 0x35, 0xd3, + 0x2b, 0xfa, 0x16, 0x7d, 0x80, 0x5e, 0xf4, 0x01, 0xda, 0x77, 0xe8, 0x7d, 0x5f, 0xa1, 0xcf, 0xd1, + 0xd9, 0xd5, 0xae, 0xfe, 0x59, 0x4a, 0xc2, 0x4d, 0x7b, 0xa7, 0xdd, 0xfd, 0xf6, 0x9c, 0x6f, 0xcf, + 0x9e, 0x73, 0xf6, 0xb3, 0x01, 0x5c, 0xe2, 0xd3, 0xbd, 0x90, 0x51, 0x4e, 0x51, 0x7b, 0xe2, 0x85, + 0x11, 0x27, 0x2c, 0x9a, 0xd0, 0x10, 0xef, 0xc3, 0xf2, 0xc0, 0x66, 0x7c, 0xc4, 0x89, 0x8f, 0x2e, + 0x00, 0x84, 0x8c, 0xba, 0xb1, 0xc3, 0xc7, 0x9e, 0xdb, 0x33, 0xb6, 0x8d, 0xdd, 0x96,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 0xcc, 0x25, 0xec, 0x5f, 0xe1, 0xfc, 0x29, + 0xac, 0xe7, 0x1c, 0x66, 0xe9, 0xcd, 0x99, 0xed, 0xbc, 0xf6, 0x82, 0x57, 0x59, 0xed, 0x80, 0x9e, + 0x1a, 0xb9, 0xf8, 0x57, 0x03, 0x9a, 0xca, 0x2f, 0xda, 0x85, 0xb5, 0x88, 0x33, 0x42, 0xf8, 0x58, + 0x11, 0x18, 0xdf, 0x54, 0x3b, 0xba, 0xc9, 0xbc, 0x02, 0xde, 0xac, 0x40, 0xf6, 0x55, 0x11, 0x14, + 0x91, 0x7d, 0x51, 0x22, 0x8e, 0xe8, 0x79, 0x49, 0x1d, 0xc8, 0x6f, 0x51, 0x00, 0x0e, 0x8d, 0x03, + 0xce, 0xa6, 0xba, 0x00, 0xd4, 0x10, 0x9d, 0x83, 0xe5, 0xb7, 0x5e, 0x38, 0x76, 0xa8, 0x4b, 0x64, + 0xfe, 0x37, 0xac, 0xe6, 0x5b, 0x2f, 0x1c, 0x50, 0x97, 0xe0, 0x67, 0xd0, 0x90, 0x11, 0x46, 0x3b, + 0xd0, 0x71, 0x62, 0xc6, 0x48, 0xe0, 0x4c, 0x13, 0x60, 0x42, 0x71, 0x45, 0x4f, 0x0a, 0xb4, 0x48, + 0xc8, 0x38, 0xf0, 0x78, 0x24, 0x59, 0x2d, 0x58, 0xc9, 0x40, 0xcc, 0x06, 0x76, 0x40, 0x23, 0xc9, + 0xa6, 0x61, 0x25, 0x03, 0x3c, 0x84, 0x8b, 0x43, 0xc2, 0x0f, 0xe2, 0x30, 0xa4, 0x8c, 0x13, 0x77, + 0x90, 0xd8, 0xf1, 0x48, 0x96, 0xae, 0x1f, 0x40, 0xb7, 0xe0, 0x52, 0xf7, 0x89, 0x4e, 0xde, 0x67, + 0x84, 0xbf, 0x83, 0x73, 0x83, 0x74, 0x22, 0x38, 0x22, 0x2c, 0xf2, 0x68, 0xa0, 0xef, 0xfe, 0x32, + 0x2c, 0xbe, 0x64,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5e, 0xaa, 0x5d, 0xd7, 0x06, 0x5f, 0x2c, + 0xc9, 0xff, 0x50, 0x3e, 0xf9, 0x27, 0x00, 0x00, 0xff, 0xff, 0x67, 0x70, 0xff, 0x79, 0x51, 0x11, + 0x00, 0x00, } diff --git a/src/checkoutservice/main.go b/src/checkoutservice/main.go index 65d976c..268fc99 100644 --- a/src/checkoutservice/main.go +++ b/src/checkoutservice/main.go @@ -13,6 +13,7 @@ import ( "google.golang.org/grpc/status" pb "./genproto" + money "./money" ) const ( @@ -71,10 +72,7 @@ func (cs *checkoutService) CreateOrder(ctx context.Context, req *pb.CreateOrderR if err != nil { return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err) } - resp.ShippingCost = &pb.Money{ - Amount: shippingQuoteUSD, - CurrencyCode: "USD", - } + resp.ShippingCost = shippingQuoteUSD // TODO(ahmetb) convert to req.UserCurrency // TODO(ahmetb) calculate resp.OrderItem with req.UserCurrency return resp, nil @@ -102,20 +100,20 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq if err != nil { return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err) } - shippingPrice, err := cs.convertCurrency(ctx, &pb.Money{ - Amount: shippingUsd, - CurrencyCode: usdCurrency}, req.UserCurrency) + shippingPrice, err := cs.convertCurrency(ctx, shippingUsd, req.UserCurrency) if err != nil { return nil, status.Errorf(codes.Internal, "failed to convert shipping cost to currency: %+v", err) } - var totalPrice pb.Money - totalPrice = sumMoney(totalPrice, *shippingPrice) + total := pb.Money{CurrencyCode: req.UserCurrency, + Units: 0, + Nanos: 0} + total = money.Must(money.Sum(total, *shippingPrice)) for _, it := range orderItems { - totalPrice = sumMoney(totalPrice, *it.Cost) + total = money.Must(money.Sum(total, *it.Cost)) } - txID, err := cs.chargeCard(ctx, &totalPrice, req.CreditCard) + txID, err := cs.chargeCard(ctx, &total, req.CreditCard) if err != nil { return nil, status.Errorf(codes.Internal, "failed to charge card: %+v", err) } @@ -143,7 +141,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq return resp, nil } -func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.MoneyAmount, error) { +func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) { conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, grpc.WithInsecure()) if err != nil { return nil, fmt.Errorf("could not connect shipping service: %+v", err) @@ -189,10 +187,7 @@ func (cs *checkoutService) prepOrderItems(ctx context.Context, items []*pb.CartI if err != nil { return nil, fmt.Errorf("failed to get product #%q", item.GetProductId()) } - usdPrice := &pb.Money{ - Amount: product.GetPriceUsd(), - CurrencyCode: usdCurrency} - price, err := cs.convertCurrency(ctx, usdPrice, userCurrency) + price, err := cs.convertCurrency(ctx, product.GetPriceUsd(), userCurrency) if err != nil { return nil, fmt.Errorf("failed to convert price of %q to %s", item.GetProductId(), userCurrency) } diff --git a/src/checkoutservice/money.go b/src/checkoutservice/money.go deleted file mode 100644 index 584b72e..0000000 --- a/src/checkoutservice/money.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "math" - - pb "./genproto" -) - -func sum(m1, m2 pb.MoneyAmount) pb.MoneyAmount { - f1, f2 := float64(m1.Fractional), float64(m2.Fractional) - lg1 := math.Max(1, math.Ceil(math.Log10(f1))) - if f1 == math.Pow(10, lg1) { - lg1++ - } - lg2 := math.Max(1, math.Ceil(math.Log10(f2))) - if f2 == math.Pow(10, lg2) { - lg2++ - } - lgMax := math.Max(lg1, lg2) - - dSum := m1.Decimal + m2.Decimal - o1 := f1 * math.Pow(10, lgMax-lg1) - o2 := f2 * math.Pow(10, lgMax-lg2) - fSum := o1 + o2 - if fSum >= math.Pow(10, lgMax) { - fSum -= math.Pow(10, lgMax) - dSum++ - } - - for int(fSum)%10 == 0 && fSum != 0 { - fSum = float64(int(fSum) / 10) - } - - return pb.MoneyAmount{ - Decimal: dSum, - Fractional: uint32(fSum)} -} - -func sumMoney(m1, m2 pb.Money) pb.Money { - s := sum(*m1.Amount, *m2.Amount) - return pb.Money{ - Amount: &s, - CurrencyCode: m1.CurrencyCode} -} diff --git a/src/checkoutservice/money/money.go b/src/checkoutservice/money/money.go new file mode 100644 index 0000000..410daaf --- /dev/null +++ b/src/checkoutservice/money/money.go @@ -0,0 +1,118 @@ +package money + +import ( + "errors" + + pb "../genproto" +) + +const ( + nanosMin = -999999999 + nanosMax = +999999999 + nanosMod = 1000000000 +) + +var ( + ErrInvalidValue = errors.New("one of the specified money values is invalid") + ErrMismatchingCurrency = errors.New("mismatching currency codes") +) + +// IsValid checks if specified value has a valid units/nanos signs and ranges. +func IsValid(m pb.Money) bool { + return signMatches(m) && validNanos(m.GetNanos()) +} + +func signMatches(m pb.Money) bool { + return m.GetNanos() == 0 || m.GetUnits() == 0 || (m.GetNanos() < 0) == (m.GetUnits() < 0) +} + +func validNanos(nanos int32) bool { return nanosMin <= nanos && nanos <= nanosMax } + +// IsZero returns true if the specified money value is equal to zero. +func IsZero(m pb.Money) bool { return m.GetUnits() == 0 && m.GetNanos() == 0 } + +// IsPositive returns true if the specified money value is valid and is +// positive. +func IsPositive(m pb.Money) bool { + return IsValid(m) && m.GetUnits() > 0 || (m.GetUnits() == 0 && m.GetNanos() > 0) +} + +// IsNegative returns true if the specified money value is valid and is +// negative. +func IsNegative(m pb.Money) bool { + return IsValid(m) && m.GetUnits() < 0 || (m.GetUnits() == 0 && m.GetNanos() < 0) +} + +// AreSameCurrency returns true if values l and r have a currency code and +// they are the same values. +func AreSameCurrency(l, r pb.Money) bool { + return l.GetCurrencyCode() == r.GetCurrencyCode() && l.GetCurrencyCode() != "" +} + +// AreEquals returns true if values l and r are the equal, including the +// currency. This does not check validity of the provided values. +func AreEquals(l, r pb.Money) bool { + return l.GetCurrencyCode() == r.GetCurrencyCode() && + l.GetUnits() == r.GetUnits() && l.GetNanos() == r.GetNanos() +} + +// Negate returns the same amount with the sign negated. +func Negate(m pb.Money) pb.Money { + return pb.Money{ + Units: -m.GetUnits(), + Nanos: -m.GetNanos(), + CurrencyCode: m.GetCurrencyCode()} +} + +// Must panics if the given error is not nil. This can be used with other +// functions like: "m := Must(Sum(a,b))". +func Must(v pb.Money, err error) pb.Money { + if err != nil { + panic(err) + } + return v +} + +// Sum adds two values. Returns an error if one of the values are invalid or +// currency codes are not matching (unless currency code is unspecified for +// both). +func Sum(l, r pb.Money) (pb.Money, error) { + if !IsValid(l) || !IsValid(r) { + return pb.Money{}, ErrInvalidValue + } else if l.GetCurrencyCode() != r.GetCurrencyCode() { + return pb.Money{}, ErrMismatchingCurrency + } + units := l.GetUnits() + r.GetUnits() + nanos := l.GetNanos() + r.GetNanos() + + if (units == 0 && nanos == 0) || (units > 0 && nanos >= 0) || (units < 0 && nanos <= 0) { + // same sign + units += int64(nanos / nanosMod) + nanos = nanos % nanosMod + } else { + // different sign. nanos guaranteed to not to go over the limit + if units > 0 { + units-- + nanos += nanosMod + } else { + units++ + nanos -= nanosMod + } + } + + return pb.Money{ + Units: units, + Nanos: nanos, + CurrencyCode: l.GetCurrencyCode()}, nil +} + +// MultiplySlow is a slow multiplication operation done through adding the value +// to itself n-1 times. +func MultiplySlow(m pb.Money, n uint32) pb.Money { + out := m + for n > 1 { + out = Must(Sum(out, m)) + n-- + } + return out +} diff --git a/src/checkoutservice/money/money_test.go b/src/checkoutservice/money/money_test.go new file mode 100644 index 0000000..d672129 --- /dev/null +++ b/src/checkoutservice/money/money_test.go @@ -0,0 +1,231 @@ +package money + +import ( + "fmt" + "reflect" + "testing" + + pb "../genproto" +) + +func mmc(u int64, n int32, c string) pb.Money { return pb.Money{Units: u, Nanos: n, CurrencyCode: c} } +func mm(u int64, n int32) pb.Money { return mmc(u, n, "") } + +func TestIsValid(t *testing.T) { + tests := []struct { + name string + in pb.Money + want bool + }{ + {"valid -/-", mm(-981273891273, -999999999), true}, + {"invalid -/+", mm(-981273891273, +999999999), false}, + {"valid +/+", mm(981273891273, 999999999), true}, + {"invalid +/-", mm(981273891273, -999999999), false}, + {"invalid +/+overflow", mm(3, 1000000000), false}, + {"invalid +/-overflow", mm(3, -1000000000), false}, + {"invalid -/+overflow", mm(-3, 1000000000), false}, + {"invalid -/-overflow", mm(-3, -1000000000), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsValid(tt.in); got != tt.want { + t.Errorf("IsValid(%v) = %v, want %v", tt.in, got, tt.want) + } + }) + } +} + +func TestIsZero(t *testing.T) { + tests := []struct { + name string + in pb.Money + want bool + }{ + {"zero", mm(0, 0), true}, + {"not-zero (-/+)", mm(-1, +1), false}, + {"not-zero (-/-)", mm(-1, -1), false}, + {"not-zero (+/+)", mm(+1, +1), false}, + {"not-zero (+/-)", mm(+1, -1), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsZero(tt.in); got != tt.want { + t.Errorf("IsZero(%v) = %v, want %v", tt.in, got, tt.want) + } + }) + } +} + +func TestIsPositive(t *testing.T) { + tests := []struct { + name string + in pb.Money + want bool + }{ + {"zero", mm(0, 0), false}, + {"positive (+/+)", mm(+1, +1), true}, + {"invalid (-/+)", mm(-1, +1), false}, + {"negative (-/-)", mm(-1, -1), false}, + {"invalid (+/-)", mm(+1, -1), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsPositive(tt.in); got != tt.want { + t.Errorf("IsPositive(%v) = %v, want %v", tt.in, got, tt.want) + } + }) + } +} + +func TestIsNegative(t *testing.T) { + tests := []struct { + name string + in pb.Money + want bool + }{ + {"zero", mm(0, 0), false}, + {"positive (+/+)", mm(+1, +1), false}, + {"invalid (-/+)", mm(-1, +1), false}, + {"negative (-/-)", mm(-1, -1), true}, + {"invalid (+/-)", mm(+1, -1), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsNegative(tt.in); got != tt.want { + t.Errorf("IsNegative(%v) = %v, want %v", tt.in, got, tt.want) + } + }) + } +} + +func TestAreSameCurrency(t *testing.T) { + type args struct { + l pb.Money + r pb.Money + } + tests := []struct { + name string + args args + want bool + }{ + {"both empty currency", args{mmc(1, 0, ""), mmc(2, 0, "")}, false}, + {"left empty currency", args{mmc(1, 0, ""), mmc(2, 0, "USD")}, false}, + {"right empty currency", args{mmc(1, 0, "USD"), mmc(2, 0, "")}, false}, + {"mismatching", args{mmc(1, 0, "USD"), mmc(2, 0, "CAD")}, false}, + {"matching", args{mmc(1, 0, "USD"), mmc(2, 0, "USD")}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := AreSameCurrency(tt.args.l, tt.args.r); got != tt.want { + t.Errorf("AreSameCurrency([%v],[%v]) = %v, want %v", tt.args.l, tt.args.r, got, tt.want) + } + }) + } +} + +func TestAreEquals(t *testing.T) { + type args struct { + l pb.Money + r pb.Money + } + tests := []struct { + name string + args args + want bool + }{ + {"equals", args{mmc(1, 2, "USD"), mmc(1, 2, "USD")}, true}, + {"mismatching currency", args{mmc(1, 2, "USD"), mmc(1, 2, "CAD")}, false}, + {"mismatching units", args{mmc(10, 20, "USD"), mmc(1, 20, "USD")}, false}, + {"mismatching nanos", args{mmc(1, 2, "USD"), mmc(1, 20, "USD")}, false}, + {"negated", args{mmc(1, 2, "USD"), mmc(-1, -2, "USD")}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := AreEquals(tt.args.l, tt.args.r); got != tt.want { + t.Errorf("AreEquals([%v],[%v]) = %v, want %v", tt.args.l, tt.args.r, got, tt.want) + } + }) + } +} + +func TestNegate(t *testing.T) { + tests := []struct { + name string + in pb.Money + want pb.Money + }{ + {"zero", mm(0, 0), mm(0, 0)}, + {"negative", mm(-1, -200), mm(1, 200)}, + {"positive", mm(1, 200), mm(-1, -200)}, + {"carries currency code", mmc(0, 0, "XXX"), mmc(0, 0, "XXX")}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Negate(tt.in); !AreEquals(got, tt.want) { + t.Errorf("Negate([%v]) = %v, want %v", tt.in, got, tt.want) + } + }) + } +} + +func TestMust_pass(t *testing.T) { + v := Must(mm(2, 3), nil) + if !AreEquals(v, mm(2, 3)) { + t.Errorf("returned the wrong value: %v", v) + } +} + +func TestMust_panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Logf("panic captured: %v", r) + } + }() + Must(mm(2, 3), fmt.Errorf("some error")) + t.Fatal("this should not have executed due to the panic above") +} + +func TestSum(t *testing.T) { + type args struct { + l pb.Money + r pb.Money + } + tests := []struct { + name string + args args + want pb.Money + wantErr error + }{ + {"0+0=0", args{mm(0, 0), mm(0, 0)}, mm(0, 0), nil}, + {"Error: currency code on left", args{mmc(0, 0, "XXX"), mm(0, 0)}, mm(0, 0), ErrMismatchingCurrency}, + {"Error: currency code on right", args{mm(0, 0), mmc(0, 0, "YYY")}, mm(0, 0), ErrMismatchingCurrency}, + {"Error: currency code mismatch", args{mmc(0, 0, "AAA"), mmc(0, 0, "BBB")}, mm(0, 0), ErrMismatchingCurrency}, + {"Error: invalid +/-", args{mm(+1, -1), mm(0, 0)}, mm(0, 0), ErrInvalidValue}, + {"Error: invalid -/+", args{mm(0, 0), mm(-1, +2)}, mm(0, 0), ErrInvalidValue}, + {"Error: invalid nanos", args{mm(0, 1000000000), mm(1, 0)}, mm(0, 0), ErrInvalidValue}, + {"both positive (no carry)", args{mm(2, 200000000), mm(2, 200000000)}, mm(4, 400000000), nil}, + {"both positive (nanos=max)", args{mm(2, 111111111), mm(2, 888888888)}, mm(4, 999999999), nil}, + {"both positive (carry)", args{mm(2, 200000000), mm(2, 900000000)}, mm(5, 100000000), nil}, + {"both negative (no carry)", args{mm(-2, -200000000), mm(-2, -200000000)}, mm(-4, -400000000), nil}, + {"both negative (carry)", args{mm(-2, -200000000), mm(-2, -900000000)}, mm(-5, -100000000), nil}, + {"mixed (larger positive, just decimals)", args{mm(11, 0), mm(-2, 0)}, mm(9, 0), nil}, + {"mixed (larger negative, just decimals)", args{mm(-11, 0), mm(2, 0)}, mm(-9, 0), nil}, + {"mixed (larger positive, no borrow)", args{mm(11, 100000000), mm(-2, -100000000)}, mm(9, 0), nil}, + {"mixed (larger positive, with borrow)", args{mm(11, 100000000), mm(-2, -9000000 /*.09*/)}, mm(9, 91000000 /*.091*/), nil}, + {"mixed (larger negative, no borrow)", args{mm(-11, -100000000), mm(2, 100000000)}, mm(-9, 0), nil}, + {"mixed (larger negative, with borrow)", args{mm(-11, -100000000), mm(2, 9000000 /*.09*/)}, mm(-9, -91000000 /*.091*/), nil}, + {"0+negative", args{mm(0, 0), mm(-2, -100000000)}, mm(-2, -100000000), nil}, + {"negative+0", args{mm(-2, -100000000), mm(0, 0)}, mm(-2, -100000000), nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Sum(tt.args.l, tt.args.r) + if err != tt.wantErr { + t.Errorf("Sum([%v],[%v]): expected err=\"%v\" got=\"%v\"", tt.args.l, tt.args.r, tt.wantErr, err) + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Sum([%v],[%v]) = %v, want %v", tt.args.l, tt.args.r, got, tt.want) + } + }) + } +} diff --git a/src/checkoutservice/money_test.go b/src/checkoutservice/money_test.go deleted file mode 100644 index 29f8a85..0000000 --- a/src/checkoutservice/money_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "reflect" - "testing" - - pb "./genproto" -) - -func Test_sum(t *testing.T) { - type args struct { - m1 pb.MoneyAmount - m2 pb.MoneyAmount - } - tests := []struct { - name string - args args - want pb.MoneyAmount - }{ - { - name: "no fractions", - args: args{pb.MoneyAmount{Decimal: 10}, pb.MoneyAmount{Decimal: 100}}, - want: pb.MoneyAmount{Decimal: 110}, - }, - { - name: "same fraction digits", - args: args{pb.MoneyAmount{Decimal: 1, Fractional: 23}, pb.MoneyAmount{Decimal: 1, Fractional: 44}}, - want: pb.MoneyAmount{Decimal: 2, Fractional: 67}, - }, - { - name: "different fraction digits", - args: args{pb.MoneyAmount{Decimal: 1, Fractional: 351}, pb.MoneyAmount{Decimal: 1, Fractional: 1}}, - want: pb.MoneyAmount{Decimal: 2, Fractional: 451}, - }, - { - name: "redundant trailing zeroes are removed from fraction", - args: args{pb.MoneyAmount{Decimal: 1, Fractional: 351}, pb.MoneyAmount{Decimal: 1, Fractional: 349}}, - want: pb.MoneyAmount{Decimal: 2, Fractional: 7}, - }, - { - name: "carry", - args: args{pb.MoneyAmount{Decimal: 1, Fractional: 5}, pb.MoneyAmount{Decimal: 1, Fractional: 5000000}}, - want: pb.MoneyAmount{Decimal: 3}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := sum(tt.args.m1, tt.args.m2); !reflect.DeepEqual(got, tt.want) { - t.Errorf("sum(%v+%v) = %v, want=%v", tt.args.m1, tt.args.m2, got, tt.want) - } - }) - } -}