Skip to content

Commit

Permalink
Marshal logic to preserve object accross a request
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Dec 4, 2015
1 parent b1648e6 commit 6c3e1c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
22 changes: 16 additions & 6 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewObject(id string, objType string, attributes interface{}) (*Object, Send
Relationships: map[string]*Object{},
}

rawJSON, err := json.MarshalIndent(attributes, "", " ")
rawJSON, err := json.MarshalIndent(attributes, "", " ")
if err != nil {
return nil, ISE(fmt.Sprintf("Error marshaling attrs while creating a new JSON Object: %s", err))
}
Expand Down Expand Up @@ -58,31 +58,41 @@ func NewObject(id string, objType string, attributes interface{}) (*Object, Send
// // log errors via error.ISE
// jsh.Send(w, r, errors)
// }
func (o *Object) Unmarshal(objType string, target interface{}) (err SendableError) {
func (o *Object) Unmarshal(objType string, target interface{}) SendableError {

if objType != o.Type {
err = ISE(fmt.Sprintf(
return ISE(fmt.Sprintf(
"Expected type %s, when converting actual type: %s",
objType,
o.Type,
))
return
}

jsonErr := json.Unmarshal(o.Attributes, target)
if jsonErr != nil {
err = ISE(fmt.Sprintf(
return ISE(fmt.Sprintf(
"For type '%s' unable to marshal: %s\nError:%s",
objType,
string(o.Attributes),
jsonErr.Error(),
))
return
}

return validateInput(target)
}

// Marshal allows you to load a modified payload back into an object to preserve
// all of the data it has
func (o *Object) Marshal(attributes interface{}) SendableError {
raw, err := json.MarshalIndent(attributes, "", " ")
if err != nil {
return ISE(fmt.Sprintf("Error marshaling attrs while creating a new JSON Object: %s", err))
}

o.Attributes = raw
return nil
}

// Prepare creates a new JSON single object response with an appropriate HTTP status
// to match the request method type.
func (o *Object) Prepare(r *http.Request, response bool) (*Response, SendableError) {
Expand Down
13 changes: 13 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ func TestObject(t *testing.T) {
})
})

Convey("->Marshal()", func() {

Convey("should properly update attributes", func() {
attrs := map[string]string{"foo": "baz"}
err := testObject.Marshal(attrs)
So(err, ShouldBeNil)

raw, jsonErr := json.MarshalIndent(attrs, "", " ")
So(jsonErr, ShouldBeNil)
So(string(testObject.Attributes), ShouldEqual, string(raw))
})
})

Convey("->Prepare()", func() {

Convey("should handle a POST response correctly", func() {
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ const (
// yourType.ID = obj.ID
// // do business logic
//
// response, err := jsh.NewObject(yourType.ID, "yourtype", &yourType)
// err := object.Marshal(yourType)
// if err != nil {
// // log error
// err := jsh.Send(w, r, err)
// return
// }
//
// err := jsh.Send(w, r, response)
// err := jsh.Send(w, r, object)
// }
func ParseObject(r *http.Request) (*Object, SendableError) {

Expand Down

0 comments on commit 6c3e1c0

Please sign in to comment.