add codec option
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
eb1482d789
commit
16d616fbe2
@ -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.
|
||||
|
@ -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
|
||||
@ -69,8 +62,8 @@ func NewOptions(opts ...Option) Options {
|
||||
Logger: logger.DefaultLogger,
|
||||
Meter: meter.DefaultMeter,
|
||||
Tracer: tracer.DefaultTracer,
|
||||
MaxMsgSize: DefaultMaxMsgSize,
|
||||
TagName: DefaultTagName,
|
||||
Flatten: false,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
|
Loading…
Reference in New Issue
Block a user