diff --git a/codec/json/json.go b/codec/json/json.go index 42de4a4d..5af47a07 100644 --- a/codec/json/json.go +++ b/codec/json/json.go @@ -5,6 +5,8 @@ import ( "encoding/json" "io" + "github.com/golang/protobuf/jsonpb" + "github.com/golang/protobuf/proto" "github.com/micro/go-micro/codec" ) @@ -22,6 +24,9 @@ func (c *Codec) ReadBody(b interface{}) error { if b == nil { return nil } + if pb, ok := b.(proto.Message); ok { + return jsonpb.UnmarshalNext(c.Decoder, pb) + } return c.Decoder.Decode(b) } diff --git a/codec/json/marshaler.go b/codec/json/marshaler.go index b9d0be28..a13e5f39 100644 --- a/codec/json/marshaler.go +++ b/codec/json/marshaler.go @@ -2,6 +2,9 @@ package json import ( "encoding/json" + + "github.com/golang/protobuf/jsonpb" + "github.com/golang/protobuf/proto" ) type Marshaler struct{} @@ -11,6 +14,9 @@ func (j Marshaler) Marshal(v interface{}) ([]byte, error) { } func (j Marshaler) Unmarshal(d []byte, v interface{}) error { + if pb, ok := v.(proto.Message); ok { + return jsonpb.UnmarshalString(string(d), pb) + } return json.Unmarshal(d, v) } diff --git a/codec/text/text.go b/codec/text/text.go index f6f28859..da43da50 100644 --- a/codec/text/text.go +++ b/codec/text/text.go @@ -30,6 +30,9 @@ func (c *Codec) ReadBody(b interface{}) error { } switch b.(type) { + case *string: + v := b.(*string) + *v = string(buf) case *[]byte: v := b.(*[]byte) *v = buf @@ -51,6 +54,12 @@ func (c *Codec) Write(m *codec.Message, b interface{}) error { case *[]byte: ve := b.(*[]byte) v = *ve + case *string: + ve := b.(*string) + v = []byte(*ve) + case string: + ve := b.(string) + v = []byte(ve) case []byte: v = b.([]byte) default: @@ -65,7 +74,7 @@ func (c *Codec) Close() error { } func (c *Codec) String() string { - return "bytes" + return "text" } func NewCodec(c io.ReadWriteCloser) codec.Codec { diff --git a/registry/consul/watcher.go b/registry/consul/watcher.go index 2fc81886..1bb9d5b7 100644 --- a/registry/consul/watcher.go +++ b/registry/consul/watcher.go @@ -1,7 +1,6 @@ package consul import ( - "errors" "log" "os" "sync" @@ -246,14 +245,16 @@ func (cw *consulWatcher) handle(idx uint64, data interface{}) { func (cw *consulWatcher) Next() (*registry.Result, error) { select { case <-cw.exit: - return nil, errors.New("result chan closed") + return nil, registry.ErrWatcherStopped case r, ok := <-cw.next: if !ok { - return nil, errors.New("result chan closed") + return nil, registry.ErrWatcherStopped } return r, nil } - return nil, errors.New("result chan closed") + // NOTE: This is a dead code path: e.g. it will never be reached + // as we return in all previous code paths never leading to this return + return nil, registry.ErrWatcherStopped } func (cw *consulWatcher) Stop() {