From b021fcb1305f35698fc2ee4f122e1c87e6f98b82 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 27 Jul 2020 13:22:00 +0100 Subject: [PATCH] v3 refactor (#1868) * Move to v3 Co-authored-by: Ben Toogood --- codec.go | 4 ++-- context.go | 2 +- error.go | 2 +- extractor.go | 2 +- extractor_test.go | 2 +- grpc.go | 22 +++++++++++----------- grpc_test.go | 40 ++++++++++++++++------------------------ handler.go | 4 ++-- options.go | 16 +++++++--------- proto/test.pb.micro.go | 6 +++--- request.go | 4 ++-- response.go | 2 +- server.go | 4 ++-- stream.go | 2 +- subscriber.go | 12 ++++++------ 15 files changed, 57 insertions(+), 67 deletions(-) diff --git a/codec.go b/codec.go index 55287f6..597420b 100644 --- a/codec.go +++ b/codec.go @@ -8,8 +8,8 @@ import ( "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" - "github.com/micro/go-micro/v2/codec" - "github.com/micro/go-micro/v2/codec/bytes" + "github.com/micro/go-micro/v3/codec" + "github.com/micro/go-micro/v3/codec/bytes" "google.golang.org/grpc" "google.golang.org/grpc/encoding" "google.golang.org/grpc/metadata" diff --git a/context.go b/context.go index 0b246d7..fe8ac3d 100644 --- a/context.go +++ b/context.go @@ -3,7 +3,7 @@ package grpc import ( "context" - "github.com/micro/go-micro/v2/server" + "github.com/micro/go-micro/v3/server" ) func setServerOption(k, v interface{}) server.Option { diff --git a/error.go b/error.go index 4556415..83046a5 100644 --- a/error.go +++ b/error.go @@ -3,7 +3,7 @@ package grpc import ( "net/http" - "github.com/micro/go-micro/v2/errors" + "github.com/micro/go-micro/v3/errors" "google.golang.org/grpc/codes" ) diff --git a/extractor.go b/extractor.go index 5576182..d1d9d0f 100644 --- a/extractor.go +++ b/extractor.go @@ -5,7 +5,7 @@ import ( "reflect" "strings" - "github.com/micro/go-micro/v2/registry" + "github.com/micro/go-micro/v3/registry" ) func extractValue(v reflect.Type, d int) *registry.Value { diff --git a/extractor_test.go b/extractor_test.go index ccf9942..9eac760 100644 --- a/extractor_test.go +++ b/extractor_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "github.com/micro/go-micro/v2/registry" + "github.com/micro/go-micro/v3/registry" ) type testHandler struct{} diff --git a/grpc.go b/grpc.go index fdaec7e..32cb53e 100644 --- a/grpc.go +++ b/grpc.go @@ -15,17 +15,17 @@ import ( "time" "github.com/golang/protobuf/proto" - "github.com/micro/go-micro/v2/broker" - "github.com/micro/go-micro/v2/errors" - pberr "github.com/micro/go-micro/v2/errors/proto" - "github.com/micro/go-micro/v2/logger" - meta "github.com/micro/go-micro/v2/metadata" - "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/util/addr" - "github.com/micro/go-micro/v2/util/backoff" - mgrpc "github.com/micro/go-micro/v2/util/grpc" - mnet "github.com/micro/go-micro/v2/util/net" + "github.com/micro/go-micro/v3/broker" + "github.com/micro/go-micro/v3/errors" + pberr "github.com/micro/go-micro/v3/errors/proto" + "github.com/micro/go-micro/v3/logger" + meta "github.com/micro/go-micro/v3/metadata" + "github.com/micro/go-micro/v3/registry" + "github.com/micro/go-micro/v3/server" + "github.com/micro/go-micro/v3/util/addr" + "github.com/micro/go-micro/v3/util/backoff" + mgrpc "github.com/micro/go-micro/v3/util/grpc" + mnet "github.com/micro/go-micro/v3/util/net" "golang.org/x/net/netutil" "google.golang.org/grpc" diff --git a/grpc_test.go b/grpc_test.go index d0b0aab..e53240b 100644 --- a/grpc_test.go +++ b/grpc_test.go @@ -5,21 +5,20 @@ import ( "fmt" "testing" - "github.com/micro/go-micro/v2" - bmemory "github.com/micro/go-micro/v2/broker/memory" - "github.com/micro/go-micro/v2/client" - gcli "github.com/micro/go-micro/v2/client/grpc" - "github.com/micro/go-micro/v2/errors" - pberr "github.com/micro/go-micro/v2/errors/proto" - rmemory "github.com/micro/go-micro/v2/registry/memory" - "github.com/micro/go-micro/v2/router" - "github.com/micro/go-micro/v2/server" - gsrv "github.com/micro/go-micro/v2/server/grpc" - tgrpc "github.com/micro/go-micro/v2/transport/grpc" + bmemory "github.com/micro/go-micro/v3/broker/memory" + "github.com/micro/go-micro/v3/client" + gcli "github.com/micro/go-micro/v3/client/grpc" + "github.com/micro/go-micro/v3/errors" + pberr "github.com/micro/go-micro/v3/errors/proto" + rmemory "github.com/micro/go-micro/v3/registry/memory" + "github.com/micro/go-micro/v3/router" + rtreg "github.com/micro/go-micro/v3/router/registry" + "github.com/micro/go-micro/v3/server" + gsrv "github.com/micro/go-micro/v3/server/grpc" + pb "github.com/micro/go-micro/v3/server/grpc/proto" + tgrpc "github.com/micro/go-micro/v3/transport/grpc" "google.golang.org/grpc" "google.golang.org/grpc/status" - - pb "github.com/micro/go-micro/v2/server/grpc/proto" ) // server is used to implement helloworld.GreeterServer. @@ -117,7 +116,7 @@ func TestGRPCServer(t *testing.T) { r := rmemory.NewRegistry() b := bmemory.NewBroker() tr := tgrpc.NewTransport() - rtr := router.NewRouter(router.Registry(r)) + rtr := rtreg.NewRouter(router.Registry(r)) s := gsrv.NewServer( server.Broker(b), @@ -136,10 +135,7 @@ func TestGRPCServer(t *testing.T) { h := &testServer{} pb.RegisterTestHandler(s, h) - if err := micro.RegisterSubscriber("test_topic", s, h.Handle); err != nil { - t.Fatal(err) - } - if err := micro.RegisterSubscriber("error_topic", s, h.HandleError); err != nil { + if err := s.Subscribe(s.NewSubscriber("test_topic", h.Handle)); err != nil { t.Fatal(err) } @@ -159,11 +155,10 @@ func TestGRPCServer(t *testing.T) { } }() - pub := micro.NewEvent("test_topic", c) - pubErr := micro.NewEvent("error_topic", c) cnt := 4 for i := 0; i < cnt; i++ { - if err = pub.Publish(ctx, &pb.Request{Name: fmt.Sprintf("msg %d", i)}); err != nil { + msg := c.NewMessage("test_topic", &pb.Request{Name: fmt.Sprintf("msg %d", i)}) + if err = c.Publish(ctx, msg); err != nil { t.Fatal(err) } } @@ -171,9 +166,6 @@ func TestGRPCServer(t *testing.T) { if h.msgCount != cnt { t.Fatalf("pub/sub not work, or invalid message count %d", h.msgCount) } - if err = pubErr.Publish(ctx, &pb.Request{}); err == nil { - t.Fatal("this must return error, as we return error from handler") - } cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure()) if err != nil { diff --git a/handler.go b/handler.go index fb4e2bb..9e701c7 100644 --- a/handler.go +++ b/handler.go @@ -3,8 +3,8 @@ package grpc import ( "reflect" - "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/server" + "github.com/micro/go-micro/v3/registry" + "github.com/micro/go-micro/v3/server" ) type rpcHandler struct { diff --git a/options.go b/options.go index 6591fac..c2e00f5 100644 --- a/options.go +++ b/options.go @@ -5,12 +5,11 @@ import ( "crypto/tls" "net" - "github.com/micro/go-micro/v2/auth" - "github.com/micro/go-micro/v2/broker" - "github.com/micro/go-micro/v2/codec" - "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/server" - "github.com/micro/go-micro/v2/transport" + "github.com/micro/go-micro/v3/broker/http" + "github.com/micro/go-micro/v3/codec" + "github.com/micro/go-micro/v3/registry/mdns" + "github.com/micro/go-micro/v3/server" + "github.com/micro/go-micro/v3/transport" "google.golang.org/grpc" "google.golang.org/grpc/encoding" ) @@ -67,11 +66,10 @@ func MaxMsgSize(s int) server.Option { func newOptions(opt ...server.Option) server.Options { opts := server.Options{ - Auth: auth.DefaultAuth, Codecs: make(map[string]codec.NewCodec), Metadata: map[string]string{}, - Broker: broker.DefaultBroker, - Registry: registry.DefaultRegistry, + Broker: http.NewBroker(), + Registry: mdns.NewRegistry(), Transport: transport.DefaultTransport, Address: server.DefaultAddress, Name: server.DefaultName, diff --git a/proto/test.pb.micro.go b/proto/test.pb.micro.go index 8ca37bf..1f63685 100644 --- a/proto/test.pb.micro.go +++ b/proto/test.pb.micro.go @@ -12,9 +12,9 @@ import ( import ( context "context" - api "github.com/micro/go-micro/v2/api" - client "github.com/micro/go-micro/v2/client" - server "github.com/micro/go-micro/v2/server" + api "github.com/micro/go-micro/v3/api" + client "github.com/micro/go-micro/v3/client" + server "github.com/micro/go-micro/v3/server" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/request.go b/request.go index 1261bbc..437d0ed 100644 --- a/request.go +++ b/request.go @@ -1,8 +1,8 @@ package grpc import ( - "github.com/micro/go-micro/v2/codec" - "github.com/micro/go-micro/v2/codec/bytes" + "github.com/micro/go-micro/v3/codec" + "github.com/micro/go-micro/v3/codec/bytes" ) type rpcRequest struct { diff --git a/response.go b/response.go index e5c74fc..d78ec44 100644 --- a/response.go +++ b/response.go @@ -1,7 +1,7 @@ package grpc import ( - "github.com/micro/go-micro/v2/codec" + "github.com/micro/go-micro/v3/codec" ) type rpcResponse struct { diff --git a/server.go b/server.go index 80abb71..5a7d2a0 100644 --- a/server.go +++ b/server.go @@ -14,8 +14,8 @@ import ( "unicode" "unicode/utf8" - "github.com/micro/go-micro/v2/logger" - "github.com/micro/go-micro/v2/server" + "github.com/micro/go-micro/v3/logger" + "github.com/micro/go-micro/v3/server" ) var ( diff --git a/stream.go b/stream.go index 9216586..38139c5 100644 --- a/stream.go +++ b/stream.go @@ -3,7 +3,7 @@ package grpc import ( "context" - "github.com/micro/go-micro/v2/server" + "github.com/micro/go-micro/v3/server" "google.golang.org/grpc" ) diff --git a/subscriber.go b/subscriber.go index 5ac957c..47e3640 100644 --- a/subscriber.go +++ b/subscriber.go @@ -7,12 +7,12 @@ import ( "runtime/debug" "strings" - "github.com/micro/go-micro/v2/broker" - "github.com/micro/go-micro/v2/errors" - "github.com/micro/go-micro/v2/logger" - "github.com/micro/go-micro/v2/metadata" - "github.com/micro/go-micro/v2/registry" - "github.com/micro/go-micro/v2/server" + "github.com/micro/go-micro/v3/broker" + "github.com/micro/go-micro/v3/errors" + "github.com/micro/go-micro/v3/logger" + "github.com/micro/go-micro/v3/metadata" + "github.com/micro/go-micro/v3/registry" + "github.com/micro/go-micro/v3/server" ) const (