Skip to content

Commit

Permalink
Change decimal.Decimal to *decimal.Decimal (pointer) #218
Browse files Browse the repository at this point in the history
Rational:

Make *decimal.Decimal omitempty'able (json(api).Marshal/Unmarshal)
  • Loading branch information
daemonfire300 committed Aug 24, 2020
1 parent 6506a6d commit b602a21
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
12 changes: 10 additions & 2 deletions http/jsonapi/generator/generate_helper.go
Expand Up @@ -68,7 +68,11 @@ func (g *typeGenerator) invoke() error { // nolint: gocyclo
g.stmt.String()
case "decimal":
addValidator(g.tags, "matches(^(\\d*\\.)?\\d+$)")
g.stmt.Qual(pkgDecimal, "Decimal")
if g.isParam {
g.stmt.Qual(pkgDecimal, "Decimal")
} else {
g.stmt.Op("*").Qual(pkgDecimal, "Decimal")
}
default:
g.stmt.String()
}
Expand All @@ -82,7 +86,11 @@ func (g *typeGenerator) invoke() error { // nolint: gocyclo
case "number":
switch g.schema.Format {
case "decimal":
g.stmt.Qual(pkgDecimal, "Decimal")
if g.isParam {
g.stmt.Qual(pkgDecimal, "Decimal")
} else {
g.stmt.Op("*").Qual(pkgDecimal, "Decimal")
}
case "float":
g.stmt.Float32()
case "double":
Expand Down
10 changes: 5 additions & 5 deletions http/jsonapi/generator/internal/pay/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions http/jsonapi/generator/internal/pay/pay_test.go
Expand Up @@ -9,13 +9,14 @@ import (
"strings"
"testing"

"github.com/shopspring/decimal"

"github.com/pace/bricks/http/jsonapi"
"github.com/pace/bricks/http/jsonapi/runtime"
"github.com/pace/bricks/http/oauth2"
oidc "github.com/pace/bricks/http/oidc"
"github.com/pace/bricks/http/security/apikey"
"github.com/pace/bricks/maintenance/log"
"github.com/shopspring/decimal"
)

type testService struct {
Expand Down Expand Up @@ -70,12 +71,15 @@ func (s *testService) ProcessPayment(ctx context.Context, w ProcessPaymentRespon
if r.Content.PriceIncludingVAT.String() != "69.34" {
s.t.Errorf(`expected priceIncludingVAT "69.34", got %q`, r.Content.PriceIncludingVAT)
}

amount := decimal.RequireFromString("11.07")
rate := decimal.RequireFromString("19.0")
priceWithVat := decimal.RequireFromString("69.34")
priceWithoutVat := decimal.RequireFromString("58.27")
w.Created(&ProcessPaymentCreated{
ID: "42",
VAT: ProcessPaymentCreatedVAT{
Amount: decimal.RequireFromString("11.07"),
Rate: decimal.RequireFromString("19.0"),
Amount: &amount,
Rate: &rate,
},
Currency: "EUR",
Fueling: ProcessPaymentCreatedFueling{
Expand All @@ -85,8 +89,8 @@ func (s *testService) ProcessPayment(ctx context.Context, w ProcessPaymentRespon
Mileage: 66435,
},
PaymentToken: "f106ac99-213c-4cf7-8c1b-1e841516026b",
PriceIncludingVAT: decimal.RequireFromString("69.34"),
PriceWithoutVAT: decimal.RequireFromString("58.27"),
PriceIncludingVAT: &priceWithVat,
PriceWithoutVAT: &priceWithoutVat,
})

return nil
Expand Down Expand Up @@ -219,10 +223,10 @@ func TestHandlerDecimal(t *testing.T) {
t.Fatal(err)
}

assertDecimal(t, pc.VAT.Amount, decimal.RequireFromString("11.07"))
assertDecimal(t, pc.VAT.Rate, decimal.RequireFromString("19.0"))
assertDecimal(t, pc.PriceIncludingVAT, decimal.RequireFromString("69.34"))
assertDecimal(t, pc.PriceWithoutVAT, decimal.RequireFromString("58.27"))
assertDecimal(t, *pc.VAT.Amount, decimal.RequireFromString("11.07"))
assertDecimal(t, *pc.VAT.Rate, decimal.RequireFromString("19.0"))
assertDecimal(t, *pc.PriceIncludingVAT, decimal.RequireFromString("69.34"))
assertDecimal(t, *pc.PriceWithoutVAT, decimal.RequireFromString("58.27"))
}

func assertDecimal(t *testing.T, got, want decimal.Decimal) {
Expand Down
26 changes: 13 additions & 13 deletions http/jsonapi/generator/internal/poi/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions http/jsonapi/request.go
Expand Up @@ -401,9 +401,11 @@ func unmarshalAttribute(
value = reflect.ValueOf(attribute)
fieldType := structField.Type

// decimal.Decimal
if fieldValue.Type() == reflect.TypeOf(decimal.Decimal{}) {
value, err = handleDecimal(rawAttribute)

// decimal.Decimal and *decimal.Decimal
if fieldValue.Type() == reflect.TypeOf(decimal.Decimal{}) ||
fieldValue.Type() == reflect.TypeOf(new(decimal.Decimal)){
value, err = handleDecimal(rawAttribute, fieldValue)
return
}

Expand Down Expand Up @@ -448,7 +450,7 @@ func unmarshalAttribute(
return
}

func handleDecimal(attribute json.RawMessage) (reflect.Value, error) {
func handleDecimal(attribute json.RawMessage, fieldValue reflect.Value) (reflect.Value, error) {
var dec decimal.Decimal
err := json.Unmarshal(attribute, &dec)
if err != nil {
Expand Down

0 comments on commit b602a21

Please sign in to comment.