From 60ff93d7c12242cbaee49fdeffab9a0fa6418b58 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Mon, 23 Apr 2018 22:41:31 -0500 Subject: [PATCH] Move casting into the decode --- datautils/jsonobject.go | 10 ++++++---- datautils/resource.go | 6 +++--- datautils/resource_test.go | 2 +- db/database.go | 4 ---- handlers/deposit_resource_test.go | 30 +++++++++++++++--------------- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/datautils/jsonobject.go b/datautils/jsonobject.go index dc903eb..80c32ac 100644 --- a/datautils/jsonobject.go +++ b/datautils/jsonobject.go @@ -1,6 +1,8 @@ package datautils -import "fmt" +import ( + "fmt" +) // JSONObject represents a JSON object. type JSONObject map[string]interface{} @@ -11,10 +13,10 @@ func (d *JSONObject) GetS(key string) string { return (*d)[key].(string) } -// GetI returns the int value at key -func (d *JSONObject) GetI(key string) int { +// GetI returns the float64 value at key +func (d *JSONObject) GetF(key string) float64 { d.ensureKeyExists(key) - return (*d)[key].(int) + return (*d)[key].(float64) } // GetB returns the boolean value at key diff --git a/datautils/resource.go b/datautils/resource.go index c7c01e7..da102bf 100644 --- a/datautils/resource.go +++ b/datautils/resource.go @@ -61,8 +61,8 @@ func (d *Resource) ExternalIdentifier() string { } // Version returns the document's version -func (d *Resource) Version() int { - return d.JSON.GetI("version") +func (d *Resource) Version() float64 { + return d.JSON.GetF("version") } // Type returns the document's type @@ -104,7 +104,7 @@ func (d *Resource) WithExternalIdentifier(id string) *Resource { // WithVersion sets the version func (d *Resource) WithVersion(version int) *Resource { - d.JSON["version"] = version + d.JSON["version"] = float64(version) return d } diff --git a/datautils/resource_test.go b/datautils/resource_test.go index 7ff7e06..2a699f2 100644 --- a/datautils/resource_test.go +++ b/datautils/resource_test.go @@ -21,7 +21,7 @@ func TestWithExternalIdentifier(t *testing.T) { func TestWithVersion(t *testing.T) { resource := NewResource(JSONObject{}) resource.WithVersion(5) - assert.Equal(t, 5, resource.Version()) + assert.Equal(t, float64(5), resource.Version()) } func TestType(t *testing.T) { diff --git a/db/database.go b/db/database.go index e5603c5..6b91caf 100644 --- a/db/database.go +++ b/db/database.go @@ -48,9 +48,5 @@ func respToResource(item map[string]*dynamodb.AttributeValue) (*datautils.Resour return nil, err } - // UnmarshalMap coerces all numbers in AWS to float64. - // Force version to be an integer - json["version"] = int(json["version"].(float64)) - return datautils.NewResource(json), nil } diff --git a/handlers/deposit_resource_test.go b/handlers/deposit_resource_test.go index b93c4b9..02468d6 100644 --- a/handlers/deposit_resource_test.go +++ b/handlers/deposit_resource_test.go @@ -37,7 +37,7 @@ func TestCreateResourceHappyPath(t *testing.T) { assert.Equal(t, http.StatusCreated, r.Code) assert.Equal(t, 1, len(repo.(*MockDatabase).CreatedResources)) resource := repo.(*MockDatabase).CreatedResources[0] - assert.Equal(t, 1, resource.Version()) + assert.Equal(t, float64(1), resource.Version()) // assert.Equal(t, "bib12345678", repo.(*MockDatabase).CreatedResources[0].(map[string]interface{})["sourceid"]) }) @@ -47,9 +47,9 @@ func TestCreateResourceNoApiKey(t *testing.T) { r := gofight.New() r.POST("/v1/resource"). SetJSON(gofight.D{ - "tacoIdentifier": "oo000oo0001", - "sourceId": "bib12345678", - "title": "My work", + "tacoIdentifier": "oo000oo0001", + "sourceId": "bib12345678", + "title": "My work", }). Run(handler(nil, nil), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { @@ -77,9 +77,9 @@ func TestCreateResourceMissingSourceId(t *testing.T) { "On-Behalf-Of": "lmcrae@stanford.edu", }). SetJSON(gofight.D{ - "tacoIdentifier": "oo000oo0001", - "@type": "http://sdr.sul.stanford.edu/models/sdr3-object.jsonld", - "title": "My work", + "tacoIdentifier": "oo000oo0001", + "@type": "http://sdr.sul.stanford.edu/models/sdr3-object.jsonld", + "title": "My work", }). Run(handler(nil, nil), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { @@ -94,14 +94,14 @@ func TestCreateInvalidResource(t *testing.T) { "On-Behalf-Of": "lmcrae@stanford.edu", }). SetJSON(gofight.D{ - "tacoIdentifier": "oo000oo0001", - "@context": "http://example.com", // This is not a valid context - "@type": "http://sdr.sul.stanford.edu/models/sdr3-object.jsonld", - "access": "world", - "label": "My work", - "preserve": true, - "publish": true, - "sourceId": "bib12345678"}). + "tacoIdentifier": "oo000oo0001", + "@context": "http://example.com", // This is not a valid context + "@type": "http://sdr.sul.stanford.edu/models/sdr3-object.jsonld", + "access": "world", + "label": "My work", + "preserve": true, + "publish": true, + "sourceId": "bib12345678"}). Run(handler(nil, nil), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { assert.Equal(t, http.StatusUnprocessableEntity, r.Code)