major codec upgrade

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-11-23 16:18:47 +03:00
parent daffa9e548
commit c9049c3845
30 changed files with 196 additions and 1004 deletions

View File

@@ -48,7 +48,7 @@ type Request interface {
// The unencoded request body
Body() interface{}
// Write to the encoded request writer. This is nil before a call is made
Codec() codec.Writer
Codec() codec.Codec
// indicates whether the request will be a streaming one rather than unary
Stream() bool
}
@@ -56,7 +56,7 @@ type Request interface {
// Response is the response received from a service
type Response interface {
// Read the response
Codec() codec.Reader
Codec() codec.Codec
// read the header
Header() metadata.Metadata
// Read the undecoded response

View File

@@ -3,14 +3,26 @@ package client
import (
"context"
raw "github.com/unistack-org/micro-codec-bytes"
json "github.com/unistack-org/micro-codec-json"
"github.com/unistack-org/micro/v3/broker"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/errors"
"github.com/unistack-org/micro/v3/metadata"
)
var (
DefaultCodecs = map[string]codec.Codec{
//"application/json": cjson.NewCodec,
//"application/json-rpc": cjsonrpc.NewCodec,
//"application/protobuf": cproto.NewCodec,
//"application/proto-rpc": cprotorpc.NewCodec,
"application/octet-stream": codec.NewCodec(),
}
)
const (
defaultContentType = "application/json"
)
type noopClient struct {
opts Options
}
@@ -27,7 +39,7 @@ type noopRequest struct {
endpoint string
contentType string
body interface{}
codec codec.Writer
codec codec.Codec
stream bool
}
@@ -56,7 +68,7 @@ func (n *noopRequest) Body() interface{} {
return n.body
}
func (n *noopRequest) Codec() codec.Writer {
func (n *noopRequest) Codec() codec.Codec {
return n.codec
}
@@ -65,11 +77,11 @@ func (n *noopRequest) Stream() bool {
}
type noopResponse struct {
codec codec.Reader
codec codec.Codec
header metadata.Metadata
}
func (n *noopResponse) Codec() codec.Reader {
func (n *noopResponse) Codec() codec.Codec {
return n.codec
}
@@ -123,6 +135,16 @@ func (n *noopMessage) ContentType() string {
return n.opts.ContentType
}
func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
if cf, ok := n.opts.Codecs[contentType]; ok {
return cf, nil
}
if cf, ok := DefaultCodecs[contentType]; ok {
return cf, nil
}
return nil, codec.ErrUnknownContentType
}
func (n *noopClient) Init(opts ...Option) error {
for _, o := range opts {
o(&n.opts)
@@ -168,21 +190,15 @@ func (n *noopClient) Publish(ctx context.Context, p Message, opts ...PublishOpti
md["Micro-Topic"] = p.Topic()
// passed in raw data
if d, ok := p.Payload().(*raw.Frame); ok {
if d, ok := p.Payload().(*codec.Frame); ok {
body = d.Data
} else {
cf := n.opts.Broker.Options().Codec
if cf == nil {
cf = json.Marshaler{}
// use codec for payload
cf, err := n.newCodec(p.ContentType())
if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}
/*
// use codec for payload
cf, err := n.opts.Codecs[p.ContentType()]
if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}
*/
// set the body
b, err := cf.Marshal(p.Payload())
if err != nil {

View File

@@ -22,7 +22,7 @@ type Options struct {
// Plugged interfaces
Broker broker.Broker
Codecs map[string]codec.NewCodec
Codecs map[string]codec.Codec
Router router.Router
Selector selector.Selector
Transport transport.Transport
@@ -141,8 +141,8 @@ type RequestOptions struct {
func NewOptions(opts ...Option) Options {
options := Options{
Context: context.Background(),
ContentType: "application/protobuf",
Codecs: make(map[string]codec.NewCodec),
ContentType: "application/json",
Codecs: make(map[string]codec.Codec),
CallOptions: CallOptions{
Backoff: DefaultBackoff,
Retry: DefaultRetry,
@@ -179,7 +179,7 @@ func Logger(l logger.Logger) Option {
}
// Codec to be used to encode/decode requests for a given content type
func Codec(contentType string, c codec.NewCodec) Option {
func Codec(contentType string, c codec.Codec) Option {
return func(o *Options) {
o.Codecs[contentType] = c
}

View File

@@ -34,7 +34,7 @@ func (r *testRequest) Body() interface{} {
return r.body
}
func (r *testRequest) Codec() codec.Writer {
func (r *testRequest) Codec() codec.Codec {
return r.codec
}