diff --git a/codec/codec.go b/codec/codec.go index 051fa54c..d01263a7 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -41,11 +41,11 @@ type MessageType int // connection. ReadBody may be called with a nil argument to force the // body to be read and discarded. type Codec interface { - ReadHeader(io.Reader, *Message, MessageType) error - ReadBody(io.Reader, interface{}) error - Write(io.Writer, *Message, interface{}) error - Marshal(interface{}) ([]byte, error) - Unmarshal([]byte, interface{}) error + 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 } diff --git a/codec/noop.go b/codec/noop.go index 6447aed9..9cd0b21a 100644 --- a/codec/noop.go +++ b/codec/noop.go @@ -5,7 +5,9 @@ import ( "io" ) -type noopCodec struct{} +type noopCodec struct { + opts Options +} func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error { return nil @@ -69,11 +71,11 @@ func (c *noopCodec) String() string { } // NewCodec returns new noop codec -func NewCodec() Codec { - return &noopCodec{} +func NewCodec(opts ...Option) Codec { + return &noopCodec{opts: NewOptions(opts...)} } -func (c *noopCodec) Marshal(v interface{}) ([]byte, error) { +func (c *noopCodec) Marshal(v interface{}, opts ...Option) ([]byte, error) { if v == nil { return nil, nil } @@ -96,7 +98,7 @@ func (c *noopCodec) Marshal(v interface{}) ([]byte, error) { return json.Marshal(v) } -func (c *noopCodec) Unmarshal(d []byte, v interface{}) error { +func (c *noopCodec) Unmarshal(d []byte, v interface{}, opts ...Option) error { if v == nil { return nil } diff --git a/codec/options.go b/codec/options.go index dbcfa499..13ac33d3 100644 --- a/codec/options.go +++ b/codec/options.go @@ -1,6 +1,8 @@ package codec import ( + "context" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/meter" "github.com/unistack-org/micro/v3/tracer" @@ -19,6 +21,10 @@ type Options struct { Tracer tracer.Tracer // MaxMsgSize specifies max messages size that reads by codec MaxMsgSize int + // TagName specifies tag name in struct to control codec + TagName string + // Context stores additional codec options + Context context.Context } // MaxMsgSize sets the max message size @@ -28,6 +34,13 @@ func MaxMsgSize(n int) Option { } } +// TagName sets the codec tag name in struct +func TagName(n string) Option { + return func(o *Options) { + o.TagName = n + } +} + // Logger sets the logger func Logger(l logger.Logger) Option { return func(o *Options) { @@ -52,10 +65,12 @@ func Meter(m meter.Meter) Option { // NewOptions returns new options func NewOptions(opts ...Option) Options { options := Options{ + Context: context.Background(), Logger: logger.DefaultLogger, Meter: meter.DefaultMeter, Tracer: tracer.DefaultTracer, MaxMsgSize: DefaultMaxMsgSize, + TagName: DefaultTagName, } for _, o := range opts {