diff --git a/examples/msgpack/msgpack_entity.go b/examples/msgpack/msgpack_entity.go new file mode 100644 index 00000000..a751c3eb --- /dev/null +++ b/examples/msgpack/msgpack_entity.go @@ -0,0 +1,40 @@ +package restPack + +import ( + restful "github.com/emicklei/go-restful" + "gopkg.in/vmihailenco/msgpack.v2" +) + +// NewEntityAccessorMPack returns a new EntityReaderWriter for accessing MessagePack content. +// This package is not initialized with such an accessor using the MIME_MSGPACK contentType. +func NewEntityAccessorMsgPack(contentType string) restful.EntityReaderWriter { + return entityMsgPackAccess{ContentType: contentType} +} + +// entityOctetAccess is a EntityReaderWriter for Octet encoding +type entityMsgPackAccess struct { + // This is used for setting the Content-Type header when writing + ContentType string +} + +// Read unmarshalls the value from byte slice and using msgpack to unmarshal +func (e entityMsgPackAccess) Read(req *restful.Request, v interface{}) error { + return msgpack.NewDecoder(req.Request.Body).Decode(v) +} + +// Write marshals the value to byte slice and set the Content-Type Header. +func (e entityMsgPackAccess) Write(resp *restful.Response, status int, v interface{}) error { + return writeMsgPack(resp, status, e.ContentType, v) +} + +// writeMsgPack marshals the value to byte slice and set the Content-Type Header. +func writeMsgPack(resp *restful.Response, status int, contentType string, v interface{}) error { + if v == nil { + resp.WriteHeader(status) + // do not write a nil representation + return nil + } + resp.Header().Set(restful.HEADER_ContentType, contentType) + resp.WriteHeader(status) + return msgpack.NewEncoder(resp).Encode(v) +} diff --git a/examples/msgpack/msgpack_entity_test.go b/examples/msgpack/msgpack_entity_test.go new file mode 100644 index 00000000..5fcaeee2 --- /dev/null +++ b/examples/msgpack/msgpack_entity_test.go @@ -0,0 +1,48 @@ +package restPack + +import ( + "bytes" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + restful "github.com/emicklei/go-restful" +) + +const MIME_MSGPACK = "application/x-msgpack" // Accept or Content-Type used in Consumes() and/or Produces() + +func TestMsgPack(t *testing.T) { + + // register msg pack entity + restful.RegisterEntityAccessor(MIME_MSGPACK, NewEntityAccessorMsgPack(MIME_MSGPACK)) + type Tool struct { + Name string + Vendor string + } + + // Write + httpWriter := httptest.NewRecorder() + mpack := &Tool{Name: "json", Vendor: "apple"} + resp := restful.NewResponse(httpWriter) + resp.SetRequestAccepts("application/x-msgpack,*/*;q=0.8") + + err := resp.WriteEntity(mpack) + if err != nil { + t.Errorf("err %v", err) + } + + // Read + bodyReader := bytes.NewReader(httpWriter.Body.Bytes()) + httpRequest, _ := http.NewRequest("GET", "/test", bodyReader) + httpRequest.Header.Set("Content-Type", "application/x-msgpack; charset=UTF-8") + request := restful.NewRequest(httpRequest) + readMsgPack := new(Tool) + err = request.ReadEntity(&readMsgPack) + if err != nil { + t.Errorf("err %v", err) + } + if equal := reflect.DeepEqual(mpack, readMsgPack); !equal { + t.Fatalf("should not be error") + } +}