Merge pull request #51 from jun06t/numeric

Handle type mismatch
This commit is contained in:
Junpei Tsuji
2018-02-09 10:24:14 +09:00
committed by GitHub
3 changed files with 81 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2014 Dogenzaka Developers
Copyright (c) 2014 AWA Developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,5 +1,18 @@
package appstore
import "encoding/json"
type numericString string
func (n *numericString) UnmarshalJSON(b []byte) error {
var number json.Number
if err := json.Unmarshal(b, &number); err != nil {
return err
}
*n = numericString(number.String())
return nil
}
type (
// https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
// The IAPRequest type has the request parameter
@@ -74,15 +87,15 @@ type (
// The Receipt type has whole data of receipt
Receipt struct {
ReceiptType string `json:"receipt_type"`
AdamID int64 `json:"adam_id"`
AppItemID int64 `json:"app_item_id"`
BundleID string `json:"bundle_id"`
ApplicationVersion string `json:"application_version"`
DownloadID int64 `json:"download_id"`
VersionExternalIdentifier int64 `json:"version_external_identifier"`
OriginalApplicationVersion string `json:"original_application_version"`
InApp []InApp `json:"in_app"`
ReceiptType string `json:"receipt_type"`
AdamID int64 `json:"adam_id"`
AppItemID numericString `json:"app_item_id"`
BundleID string `json:"bundle_id"`
ApplicationVersion string `json:"application_version"`
DownloadID int64 `json:"download_id"`
VersionExternalIdentifier numericString `json:"version_external_identifier"`
OriginalApplicationVersion string `json:"original_application_version"`
InApp []InApp `json:"in_app"`
ReceiptCreationDate
RequestDate
OriginalPurchaseDate

58
appstore/model_test.go Normal file
View File

@@ -0,0 +1,58 @@
package appstore
import (
"encoding/json"
"errors"
"reflect"
"testing"
)
func TestNumericString_UnmarshalJSON(t *testing.T) {
type foo struct {
ID numericString
}
tests := []struct {
name string
in []byte
err error
out foo
}{
{
name: "string case",
in: []byte("{\"ID\":\"8080\"}"),
err: nil,
out: foo{ID: "8080"},
},
{
name: "number case",
in: []byte("{\"ID\":8080}"),
err: nil,
out: foo{ID: "8080"},
},
{
name: "object case",
in: []byte("{\"ID\":{\"Num\": 8080}}"),
err: errors.New("json: cannot unmarshal object into Go struct field foo.ID of type json.Number"),
out: foo{},
},
}
for _, v := range tests {
t.Run(v.name, func(t *testing.T) {
out := foo{}
err := json.Unmarshal(v.in, &out)
if err != nil {
if err.Error() != v.err.Error() {
t.Errorf("input: %s, get: %s, want: %s\n", v.in, err, v.err)
}
return
}
if !reflect.DeepEqual(out, v.out) {
t.Errorf("input: %s, get: %v, want: %v\n", v.in, out, v.out)
}
})
}
}