Skip to content

Commit 6c3e1c0

Browse files
author
Derek Dowling
committed
Marshal logic to preserve object accross a request
1 parent b1648e6 commit 6c3e1c0

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

object.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewObject(id string, objType string, attributes interface{}) (*Object, Send
2727
Relationships: map[string]*Object{},
2828
}
2929

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

6363
if objType != o.Type {
64-
err = ISE(fmt.Sprintf(
64+
return ISE(fmt.Sprintf(
6565
"Expected type %s, when converting actual type: %s",
6666
objType,
6767
o.Type,
6868
))
69-
return
7069
}
7170

7271
jsonErr := json.Unmarshal(o.Attributes, target)
7372
if jsonErr != nil {
74-
err = ISE(fmt.Sprintf(
73+
return ISE(fmt.Sprintf(
7574
"For type '%s' unable to marshal: %s\nError:%s",
7675
objType,
7776
string(o.Attributes),
7877
jsonErr.Error(),
7978
))
80-
return
8179
}
8280

8381
return validateInput(target)
8482
}
8583

84+
// Marshal allows you to load a modified payload back into an object to preserve
85+
// all of the data it has
86+
func (o *Object) Marshal(attributes interface{}) SendableError {
87+
raw, err := json.MarshalIndent(attributes, "", " ")
88+
if err != nil {
89+
return ISE(fmt.Sprintf("Error marshaling attrs while creating a new JSON Object: %s", err))
90+
}
91+
92+
o.Attributes = raw
93+
return nil
94+
}
95+
8696
// Prepare creates a new JSON single object response with an appropriate HTTP status
8797
// to match the request method type.
8898
func (o *Object) Prepare(r *http.Request, response bool) (*Response, SendableError) {

object_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ func TestObject(t *testing.T) {
106106
})
107107
})
108108

109+
Convey("->Marshal()", func() {
110+
111+
Convey("should properly update attributes", func() {
112+
attrs := map[string]string{"foo": "baz"}
113+
err := testObject.Marshal(attrs)
114+
So(err, ShouldBeNil)
115+
116+
raw, jsonErr := json.MarshalIndent(attrs, "", " ")
117+
So(jsonErr, ShouldBeNil)
118+
So(string(testObject.Attributes), ShouldEqual, string(raw))
119+
})
120+
})
121+
109122
Convey("->Prepare()", func() {
110123

111124
Convey("should handle a POST response correctly", func() {

parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ const (
3737
// yourType.ID = obj.ID
3838
// // do business logic
3939
//
40-
// response, err := jsh.NewObject(yourType.ID, "yourtype", &yourType)
40+
// err := object.Marshal(yourType)
4141
// if err != nil {
4242
// // log error
4343
// err := jsh.Send(w, r, err)
4444
// return
4545
// }
4646
//
47-
// err := jsh.Send(w, r, response)
47+
// err := jsh.Send(w, r, object)
4848
// }
4949
func ParseObject(r *http.Request) (*Object, SendableError) {
5050

0 commit comments

Comments
 (0)