-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Object creation/unmarshaling helpers
- Loading branch information
Derek Dowling
committed
Nov 12, 2015
1 parent
6c44504
commit 4d142c2
Showing
4 changed files
with
126 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,52 @@ | ||
package jsh | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Object represents the default JSON spec for objects | ||
type Object struct { | ||
Type string `json:"type"` | ||
ID string `json:"id"` | ||
Attributes interface{} `json:"attributes"` | ||
Type string `json:"type"` | ||
ID string `json:"id"` | ||
Attributes json.RawMessage `json:"attributes"` | ||
} | ||
|
||
// NewObject prepares a new JSON Object for an API response. Whatever is provided | ||
// as attributes will be marshalled to JSON. | ||
func NewObject(id string, objType string, attributes interface{}) (*Object, error) { | ||
object := &Object{ | ||
ID: id, | ||
Type: objType, | ||
} | ||
|
||
rawJSON, err := json.MarshalIndent(attributes, "", " ") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error marshaling attrs while creating a new JSON Object: %s", err) | ||
} | ||
|
||
object.Attributes = rawJSON | ||
return object, nil | ||
} | ||
|
||
// Unmarshal puts an Object's Attributes into a more useful target type defined | ||
// by the user. A correct object type specified must also be provided otherwise | ||
// an error is returned to prevent hard to track down situations. | ||
func (o *Object) Unmarshal(objType string, target interface{}) error { | ||
|
||
if objType != o.Type { | ||
return fmt.Errorf("Attempting to convert object to incompatible type") | ||
} | ||
|
||
err := json.Unmarshal(o.Attributes, target) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"Error converting %s to type '%s': %s", | ||
o.Attributes, | ||
objType, | ||
err.Error(), | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jsh | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestObject(t *testing.T) { | ||
|
||
Convey("Object Tests", t, func() { | ||
|
||
testObject := &Object{ | ||
ID: "ID123", | ||
Type: "testConversion", | ||
Attributes: json.RawMessage(`{"foo":"bar"}`), | ||
} | ||
|
||
Convey("->NewObject()", func() { | ||
|
||
Convey("should create a new object with populated attrs", func() { | ||
attrs := struct { | ||
Foo string `json:"foo"` | ||
}{"bar"} | ||
|
||
newObj, err := NewObject(testObject.ID, testObject.Type, attrs) | ||
So(err, ShouldBeNil) | ||
So(newObj.Attributes, ShouldNotBeEmpty) | ||
}) | ||
}) | ||
|
||
Convey("->Unmarshal()", func() { | ||
testConversion := struct { | ||
ID string | ||
Foo string `json:"foo"` | ||
}{} | ||
|
||
Convey("Should successfully populate a valid struct", func() { | ||
err := testObject.Unmarshal("testConversion", &testConversion) | ||
So(err, ShouldBeNil) | ||
So(testConversion.Foo, ShouldEqual, "bar") | ||
}) | ||
|
||
Convey("Should reject a non-matching type", func() { | ||
err := testObject.Unmarshal("badType", &testConversion) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
|
||
Convey("->ParseList()", func() { | ||
listJSON := | ||
`{"data": [ | ||
{"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}}, | ||
{"type": "user", "id": "sweetID456", "attributes": {"ID":"456"}} | ||
]}` | ||
|
||
closer := createIOCloser([]byte(listJSON)) | ||
|
||
list, err := ParseList(closer) | ||
So(err, ShouldBeNil) | ||
So(len(list), ShouldEqual, 2) | ||
|
||
object := list[1] | ||
So(object.Type, ShouldEqual, "user") | ||
So(object.ID, ShouldEqual, "sweetID456") | ||
So(object.Attributes, ShouldResemble, json.RawMessage(`{"ID":"456"}`)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters