From 16b0c58cf877b92479ba3836b5f97ef8da0c1a68 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Mon, 14 Nov 2022 19:57:43 +0100 Subject: [PATCH] fix(go): use decimal instead f32 for price --- go.mod | 1 + go.sum | 2 ++ go/client_test.go | 11 ++++++----- go/parking_lot.go | 7 ++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 3e8eb23..ac8916f 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/google/go-cmp v0.5.9 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/shopspring/decimal v1.3.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 3a92e1e..620ccda 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= +github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= diff --git a/go/client_test.go b/go/client_test.go index 39926b8..29f97e9 100644 --- a/go/client_test.go +++ b/go/client_test.go @@ -10,6 +10,7 @@ import ( wheretopark "wheretopark/go" geojson "github.com/paulmach/go.geojson" + "github.com/shopspring/decimal" "github.com/stretchr/testify/assert" ) @@ -95,23 +96,23 @@ func TestParkingLot(t *testing.T) { Pricing: []wheretopark.PricingRule{ { Duration: "PT1H", - Price: 0.0, + Price: decimal.Zero, }, { Duration: "PT2H", - Price: 2.0, + Price: decimal.NewFromInt(2), }, { Duration: "PT3H", - Price: 3.0, + Price: decimal.NewFromInt(3), }, { Duration: "PT24H", - Price: 25.0, + Price: decimal.NewFromInt(25), }, { Duration: "PT1H", - Price: 4.0, + Price: decimal.NewFromInt(4), Repeating: true, }, }, diff --git a/go/parking_lot.go b/go/parking_lot.go index cb6b301..81dc193 100644 --- a/go/parking_lot.go +++ b/go/parking_lot.go @@ -3,6 +3,7 @@ package wheretopark import ( "github.com/mmcloughlin/geohash" geojson "github.com/paulmach/go.geojson" + "github.com/shopspring/decimal" ) type ID = string @@ -12,9 +13,9 @@ type PaymentMethod = string type LanguageCode = string type PricingRule struct { - Duration string `json:"duration"` - Price float32 `json:"price"` - Repeating bool `json:"repeating,omitempty"` + Duration string `json:"duration"` + Price decimal.Decimal `json:"price"` + Repeating bool `json:"repeating,omitempty"` } type Rule struct {