Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Move casting into the decode
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne authored and eefahy committed Apr 24, 2018
1 parent 24376dc commit 8aec7b8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
10 changes: 6 additions & 4 deletions datautils/jsonobject.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datautils

import "fmt"
import (
"fmt"
)

// JSONObject represents a JSON object.
type JSONObject map[string]interface{}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions datautils/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 0 additions & 4 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 14 additions & 14 deletions handlers/deposit_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 8aec7b8

Please sign in to comment.