Handle type mismatch
This commit is contained in:
@@ -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
|
||||
@@ -76,11 +89,11 @@ type (
|
||||
Receipt struct {
|
||||
ReceiptType string `json:"receipt_type"`
|
||||
AdamID int64 `json:"adam_id"`
|
||||
AppItemID int64 `json:"app_item_id"`
|
||||
AppItemID numericString `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"`
|
||||
VersionExternalIdentifier numericString `json:"version_external_identifier"`
|
||||
OriginalApplicationVersion string `json:"original_application_version"`
|
||||
InApp []InApp `json:"in_app"`
|
||||
ReceiptCreationDate
|
||||
|
||||
58
appstore/model_test.go
Normal file
58
appstore/model_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user