2021-01-22 23:32:33 +03:00
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
2021-09-22 00:57:10 +03:00
|
|
|
"context"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-01-22 23:32:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option func
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options contains codec options
|
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Meter used for metrics
|
|
|
|
Meter meter.Meter
|
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
2021-09-22 00:57:10 +03:00
|
|
|
// Context stores additional codec options
|
|
|
|
Context context.Context
|
2021-09-22 17:09:26 +03:00
|
|
|
// TagName specifies tag name in struct to control codec
|
|
|
|
TagName string
|
|
|
|
// MaxMsgSize specifies max messages size that reads by codec
|
|
|
|
MaxMsgSize int
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxMsgSize sets the max message size
|
|
|
|
func MaxMsgSize(n int) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.MaxMsgSize = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 00:57:10 +03:00
|
|
|
// TagName sets the codec tag name in struct
|
|
|
|
func TagName(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TagName = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
// NewOptions returns new options
|
2021-01-22 23:32:33 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2021-09-22 00:57:10 +03:00
|
|
|
Context: context.Background(),
|
2021-01-22 23:32:33 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
|
|
|
MaxMsgSize: DefaultMaxMsgSize,
|
2021-09-22 00:57:10 +03:00
|
|
|
TagName: DefaultTagName,
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|