Merge branch 'master' into crufting

This commit is contained in:
Asim Aslam 2019-06-10 12:44:27 +01:00
commit 070bd40b4c
4 changed files with 26 additions and 5 deletions

View File

@ -5,6 +5,8 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
) )
@ -22,6 +24,9 @@ func (c *Codec) ReadBody(b interface{}) error {
if b == nil { if b == nil {
return nil return nil
} }
if pb, ok := b.(proto.Message); ok {
return jsonpb.UnmarshalNext(c.Decoder, pb)
}
return c.Decoder.Decode(b) return c.Decoder.Decode(b)
} }

View File

@ -2,6 +2,9 @@ package json
import ( import (
"encoding/json" "encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
) )
type Marshaler struct{} type Marshaler struct{}
@ -11,6 +14,9 @@ func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
} }
func (j Marshaler) Unmarshal(d []byte, v interface{}) 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) return json.Unmarshal(d, v)
} }

View File

@ -30,6 +30,9 @@ func (c *Codec) ReadBody(b interface{}) error {
} }
switch b.(type) { switch b.(type) {
case *string:
v := b.(*string)
*v = string(buf)
case *[]byte: case *[]byte:
v := b.(*[]byte) v := b.(*[]byte)
*v = buf *v = buf
@ -51,6 +54,12 @@ func (c *Codec) Write(m *codec.Message, b interface{}) error {
case *[]byte: case *[]byte:
ve := b.(*[]byte) ve := b.(*[]byte)
v = *ve v = *ve
case *string:
ve := b.(*string)
v = []byte(*ve)
case string:
ve := b.(string)
v = []byte(ve)
case []byte: case []byte:
v = b.([]byte) v = b.([]byte)
default: default:
@ -65,7 +74,7 @@ func (c *Codec) Close() error {
} }
func (c *Codec) String() string { func (c *Codec) String() string {
return "bytes" return "text"
} }
func NewCodec(c io.ReadWriteCloser) codec.Codec { func NewCodec(c io.ReadWriteCloser) codec.Codec {

View File

@ -1,7 +1,6 @@
package consul package consul
import ( import (
"errors"
"log" "log"
"os" "os"
"sync" "sync"
@ -246,14 +245,16 @@ func (cw *consulWatcher) handle(idx uint64, data interface{}) {
func (cw *consulWatcher) Next() (*registry.Result, error) { func (cw *consulWatcher) Next() (*registry.Result, error) {
select { select {
case <-cw.exit: case <-cw.exit:
return nil, errors.New("result chan closed") return nil, registry.ErrWatcherStopped
case r, ok := <-cw.next: case r, ok := <-cw.next:
if !ok { if !ok {
return nil, errors.New("result chan closed") return nil, registry.ErrWatcherStopped
} }
return r, nil 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() { func (cw *consulWatcher) Stop() {