From 77b179753c0632f376983f9b055b164486eaebfc Mon Sep 17 00:00:00 2001 From: Junpei Tsuji Date: Thu, 8 Feb 2018 20:06:41 +0900 Subject: [PATCH] Handle type mismatch --- appstore/model.go | 31 +++++++++++++++------- appstore/model_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 appstore/model_test.go diff --git a/appstore/model.go b/appstore/model.go index a4c3476..cdbac3b 100644 --- a/appstore/model.go +++ b/appstore/model.go @@ -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 diff --git a/appstore/model_test.go b/appstore/model_test.go new file mode 100644 index 0000000..8b0fb5e --- /dev/null +++ b/appstore/model_test.go @@ -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) + } + }) + } +}