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 4c9ffb6..f061eb7 100644 --- a/datautils/resource.go +++ b/datautils/resource.go @@ -62,7 +62,7 @@ func (d *Resource) ExternalIdentifier() string { // Version returns the document's version func (d *Resource) Version() int { - return d.JSON.GetI("version") + return int(d.JSON.GetF("version")) } // Type returns the document's type @@ -132,7 +132,7 @@ func (d *Resource) WithCurrentVersion(flag bool) *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/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..a50aa4a 100644 --- a/handlers/deposit_resource_test.go +++ b/handlers/deposit_resource_test.go @@ -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)