From 3aaf1182cb58524ef5193c816f3bfafb31bdf99b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 16 Sep 2024 23:02:45 +0300 Subject: [PATCH] add codec option Signed-off-by: Vasiliy Tolstov --- codec/codec.go | 23 +++++------------------ codec/options.go | 23 ++++++++--------------- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/codec/codec.go b/codec/codec.go index 3de1899a..b850cf8f 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -1,5 +1,5 @@ // Package codec is an interface for encoding messages -package codec // import "go.unistack.org/micro/v3/codec" +package codec import ( "errors" @@ -13,8 +13,6 @@ var ( ) var ( - // DefaultMaxMsgSize specifies how much data codec can handle - DefaultMaxMsgSize = 1024 * 1024 * 4 // 4Mb // DefaultCodec is the global default codec DefaultCodec = NewCodec() // DefaultTagName specifies struct tag name to control codec Marshal/Unmarshal @@ -28,21 +26,10 @@ type Codec interface { String() string } -// 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) { - if nc, ok := c.(interface { - MarshalAppend([]byte, interface{}, ...Option) ([]byte, error) - }); ok { - return nc.MarshalAppend(buf, v, opts...) - } - - mbuf, err := c.Marshal(v, opts...) - if err != nil { - return nil, err - } - - return append(buf, mbuf...), nil +type CodecV2 interface { + Marshal(buf []byte, v interface{}, opts ...Option) ([]byte, error) + Unmarshal(buf []byte, v interface{}, opts ...Option) error + String() string } // RawMessage is a raw encoded JSON value. diff --git a/codec/options.go b/codec/options.go index afcfb1c5..a0ed6e54 100644 --- a/codec/options.go +++ b/codec/options.go @@ -23,15 +23,8 @@ type Options struct { Context context.Context // TagName specifies tag name in struct to control codec TagName string - // MaxMsgSize specifies max messages size that reads by codec - MaxMsgSize int -} - -// MaxMsgSize sets the max message size -func MaxMsgSize(n int) Option { - return func(o *Options) { - o.MaxMsgSize = n - } + // Flatten specifies that struct must be analyzed for flatten tag + Flatten bool } // TagName sets the codec tag name in struct @@ -65,12 +58,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, + Context: context.Background(), + Logger: logger.DefaultLogger, + Meter: meter.DefaultMeter, + Tracer: tracer.DefaultTracer, + TagName: DefaultTagName, + Flatten: false, } for _, o := range opts {