From eb1482d78929fabc5587aa3edaf1352606659853 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 16 Sep 2024 22:41:36 +0300 Subject: [PATCH] codec: simplify codec interface Signed-off-by: Vasiliy Tolstov --- codec/codec.go | 42 +------------------------------ codec/noop.go | 68 +++++--------------------------------------------- server/noop.go | 3 +-- 3 files changed, 8 insertions(+), 105 deletions(-) diff --git a/codec/codec.go b/codec/codec.go index daaf6ee1..3de1899a 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -3,17 +3,6 @@ package codec // import "go.unistack.org/micro/v3/codec" import ( "errors" - "io" - - "go.unistack.org/micro/v3/metadata" -) - -// Message types -const ( - Error MessageType = iota - Request - Response - Event ) var ( @@ -32,42 +21,13 @@ var ( DefaultTagName = "codec" ) -// MessageType specifies message type for codec -type MessageType int - -// Codec encodes/decodes various types of messages used within micro. -// ReadHeader and ReadBody are called in pairs to read requests/responses -// from the connection. Close is called when finished with the -// connection. ReadBody may be called with a nil argument to force the -// body to be read and discarded. +// Codec encodes/decodes various types of messages. type Codec interface { - ReadHeader(r io.Reader, m *Message, mt MessageType) error - ReadBody(r io.Reader, v interface{}) error - Write(w io.Writer, m *Message, v interface{}) error Marshal(v interface{}, opts ...Option) ([]byte, error) Unmarshal(b []byte, v interface{}, opts ...Option) error String() string } -// Message represents detailed information about -// the communication, likely followed by the body. -// In the case of an error, body may be nil. -type Message struct { - Header metadata.Metadata - Target string - Method string - Endpoint string - Error string - ID string - Body []byte - Type MessageType -} - -// NewMessage creates new codec message -func NewMessage(t MessageType) *Message { - return &Message{Type: t, Header: metadata.New(0)} -} - // MarshalAppend calls codec.Marshal(v) and returns the data appended to buf. // If codec implements MarshalAppend, that is called instead. func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, error) { diff --git a/codec/noop.go b/codec/noop.go index c9a66e9b..76d21d93 100644 --- a/codec/noop.go +++ b/codec/noop.go @@ -2,70 +2,14 @@ package codec import ( "encoding/json" - "io" + + codecpb "go.unistack.org/micro-proto/v3/codec" ) type noopCodec struct { opts Options } -func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error { - return nil -} - -func (c *noopCodec) ReadBody(conn io.Reader, b interface{}) error { - // read bytes - buf, err := io.ReadAll(conn) - if err != nil { - return err - } - - if b == nil { - return nil - } - - switch v := b.(type) { - case *string: - *v = string(buf) - case *[]byte: - *v = buf - case *Frame: - v.Data = buf - default: - return json.Unmarshal(buf, v) - } - - return nil -} - -func (c *noopCodec) Write(conn io.Writer, m *Message, b interface{}) error { - if b == nil { - return nil - } - - var v []byte - switch vb := b.(type) { - case *Frame: - v = vb.Data - case string: - v = []byte(vb) - case *string: - v = []byte(*vb) - case *[]byte: - v = *vb - case []byte: - v = vb - default: - var err error - v, err = json.Marshal(vb) - if err != nil { - return err - } - } - _, err := conn.Write(v) - return err -} - func (c *noopCodec) String() string { return "noop" } @@ -91,8 +35,8 @@ func (c *noopCodec) Marshal(v interface{}, opts ...Option) ([]byte, error) { return ve, nil case *Frame: return ve.Data, nil - case *Message: - return ve.Body, nil + case *codecpb.Frame: + return ve.Data, nil } return json.Marshal(v) @@ -115,8 +59,8 @@ func (c *noopCodec) Unmarshal(d []byte, v interface{}, opts ...Option) error { case *Frame: ve.Data = d return nil - case *Message: - ve.Body = d + case *codecpb.Frame: + ve.Data = d return nil } diff --git a/server/noop.go b/server/noop.go index 956ac9c2..6d2ecc05 100644 --- a/server/noop.go +++ b/server/noop.go @@ -1,7 +1,6 @@ package server import ( - "bytes" "context" "fmt" "reflect" @@ -691,7 +690,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl req = req.Elem() } - if err = cf.ReadBody(bytes.NewBuffer(msg.Body), req.Interface()); err != nil { + if err = cf.Unmarshal(msg.Body, req.Interface()); err != nil { return err }