diff --git a/extractor.go b/extractor.go deleted file mode 100644 index 6f1f888..0000000 --- a/extractor.go +++ /dev/null @@ -1,122 +0,0 @@ -package grpc - -import ( - "fmt" - "reflect" - "strings" - - "github.com/unistack-org/micro/v3/registry" -) - -func extractValue(v reflect.Type, d int) *registry.Value { - if d == 3 { - return nil - } - if v == nil { - return nil - } - - if v.Kind() == reflect.Ptr { - v = v.Elem() - } - - arg := ®istry.Value{ - Name: v.Name(), - Type: v.Name(), - } - - switch v.Kind() { - case reflect.Struct: - for i := 0; i < v.NumField(); i++ { - f := v.Field(i) - val := extractValue(f.Type, d+1) - if val == nil { - continue - } - - // if we can find a json tag use it - if tags := f.Tag.Get("json"); len(tags) > 0 { - parts := strings.Split(tags, ",") - if parts[0] == "-" || parts[0] == "omitempty" { - continue - } - val.Name = parts[0] - } - - // if there's no name default it - if len(val.Name) == 0 { - val.Name = v.Field(i).Name - } - - arg.Values = append(arg.Values, val) - } - case reflect.Slice: - p := v.Elem() - if p.Kind() == reflect.Ptr { - p = p.Elem() - } - arg.Type = "[]" + p.Name() - } - - return arg -} - -func extractEndpoint(method reflect.Method) *registry.Endpoint { - if method.PkgPath != "" { - return nil - } - - var rspType, reqType reflect.Type - var stream bool - mt := method.Type - - switch mt.NumIn() { - case 3: - reqType = mt.In(1) - rspType = mt.In(2) - case 4: - reqType = mt.In(2) - rspType = mt.In(3) - default: - return nil - } - - // are we dealing with a stream? - switch rspType.Kind() { - case reflect.Func, reflect.Interface: - stream = true - } - - request := extractValue(reqType, 0) - response := extractValue(rspType, 0) - - ep := ®istry.Endpoint{ - Name: method.Name, - Request: request, - Response: response, - Metadata: make(map[string]string), - } - - if stream { - ep.Metadata = map[string]string{ - "stream": fmt.Sprintf("%v", stream), - } - } - - return ep -} - -func extractSubValue(typ reflect.Type) *registry.Value { - var reqType reflect.Type - switch typ.NumIn() { - case 1: - reqType = typ.In(0) - case 2: - reqType = typ.In(1) - case 3: - reqType = typ.In(2) - default: - return nil - } - return extractValue(reqType, 0) -} diff --git a/extractor_test.go b/extractor_test.go deleted file mode 100644 index a41aeea..0000000 --- a/extractor_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package grpc - -import ( - "context" - "reflect" - "testing" - - "github.com/unistack-org/micro/v3/registry" -) - -type testHandler struct{} - -type testRequest struct{} - -type testResponse struct{} - -func (t *testHandler) Test(ctx context.Context, req *testRequest, rsp *testResponse) error { - return nil -} - -func TestExtractEndpoint(t *testing.T) { - handler := &testHandler{} - typ := reflect.TypeOf(handler) - - var endpoints []*registry.Endpoint - - for m := 0; m < typ.NumMethod(); m++ { - if e := extractEndpoint(typ.Method(m)); e != nil { - endpoints = append(endpoints, e) - } - } - - if i := len(endpoints); i != 1 { - t.Errorf("Expected 1 endpoint, have %d", i) - } - - if endpoints[0].Name != "Test" { - t.Errorf("Expected handler Test, got %s", endpoints[0].Name) - } - - if endpoints[0].Request == nil { - t.Error("Expected non nil request") - } - - if endpoints[0].Response == nil { - t.Error("Expected non nil request") - } - - if endpoints[0].Request.Name != "testRequest" { - t.Errorf("Expected testRequest got %s", endpoints[0].Request.Name) - } - - if endpoints[0].Response.Name != "testResponse" { - t.Errorf("Expected testResponse got %s", endpoints[0].Response.Name) - } - - if endpoints[0].Request.Type != "testRequest" { - t.Errorf("Expected testRequest type got %s", endpoints[0].Request.Type) - } - - if endpoints[0].Response.Type != "testResponse" { - t.Errorf("Expected testResponse type got %s", endpoints[0].Response.Type) - } - -} diff --git a/go.mod b/go.mod index e1c08cf..b192646 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/golang/protobuf v1.4.2 github.com/google/go-cmp v0.5.1 // indirect github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844 - github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920124807-9b11ea527aeb + github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d golang.org/x/net v0.0.0-20200904194848-62affa334b73 golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect golang.org/x/text v0.3.3 // indirect diff --git a/go.sum b/go.sum index 4f8761a..a44beb2 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cloudflare/cloudflare-go v0.10.2/go.mod h1:qhVI5MKwBGhdNU89ZRz2plgYutcJ5PCekLxXn56w6SY= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cpu/goacmedns v0.0.1/go.mod h1:sesf/pNnCYwUevQEQfEwY0Y3DydlQWSGZbaMElOWxok= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= @@ -259,10 +260,13 @@ github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844/go. github.com/unistack-org/micro-config-cmd v0.0.0-20200909210346-ec89783dc46c/go.mod h1:6pm1cadbwsFcEW1ZbV5Fp0i3goR3TNfROMNSPih3I8k= github.com/unistack-org/micro-config-cmd v0.0.0-20200909210755-6e7e85eeab34 h1:VHc98t4SoiCF/jbkFu2e/j+IyJ/+MFQ1T+INNL7LubU= github.com/unistack-org/micro-config-cmd v0.0.0-20200909210755-6e7e85eeab34/go.mod h1:fT1gYn+TtfVZZ5tNx56bZIncJjmlji66g7GKdWua5hE= +github.com/unistack-org/micro-config-cmd v0.0.0-20200920140133-0853deb2e5dc h1:hHAU3rgeiA0LaudfNdMLf9/jkOBeFxvJdnwXevviZF8= +github.com/unistack-org/micro-config-cmd v0.0.0-20200920140133-0853deb2e5dc/go.mod h1:il8nz4ZEcX3Usyfrtwy+YtQcb7xSUSFJdSe8PBJ9gOA= github.com/unistack-org/micro/v3 v3.0.0-20200827083227-aa99378adc6e/go.mod h1:rPQbnry3nboAnMczj8B1Gzlcyv/HYoMZLgd3/3nttJ4= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200909210629-caec730248b1/go.mod h1:mmqHR9WelHUXqg2mELjsQ+FJHcWs6mNmXg+wEYO2T3c= -github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920124807-9b11ea527aeb h1:ETCShJ27nTE7kMQgaf7S6p1Tbfr2tdGO6MYxZ2LTMWY= -github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920124807-9b11ea527aeb/go.mod h1:HUzMG4Mcy97958VxWTg8zuazZgwQ/aoLZ8wtBVONwRE= +github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920135754-1cbd1d2bad83/go.mod h1:HUzMG4Mcy97958VxWTg8zuazZgwQ/aoLZ8wtBVONwRE= +github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d h1:LcDlJMSPibasCxm8WNWM92bz+7NFX8StPfM+4u/fme8= +github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/grpc.go b/grpc.go index eab1b9f..fc59247 100644 --- a/grpc.go +++ b/grpc.go @@ -662,7 +662,7 @@ func (g *grpcServer) Subscribe(sb server.Subscriber) error { return fmt.Errorf("invalid subscriber: no handler functions") } - if err := validateSubscriber(sb); err != nil { + if err := server.ValidateSubscriber(sb); err != nil { return err } diff --git a/handler.go b/handler.go index 59a5947..95e695e 100644 --- a/handler.go +++ b/handler.go @@ -30,7 +30,7 @@ func newRpcHandler(handler interface{}, opts ...server.HandlerOption) server.Han var endpoints []*registry.Endpoint for m := 0; m < typ.NumMethod(); m++ { - if e := extractEndpoint(typ.Method(m)); e != nil { + if e := registry.ExtractEndpoint(typ.Method(m)); e != nil { e.Name = name + "." + e.Name for k, v := range options.Metadata[e.Name] { diff --git a/subscriber.go b/subscriber.go index 176105e..7630a58 100644 --- a/subscriber.go +++ b/subscriber.go @@ -61,7 +61,7 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio endpoints = append(endpoints, ®istry.Endpoint{ Name: "Func", - Request: extractSubValue(typ), + Request: registry.ExtractSubValue(typ), Metadata: map[string]string{ "topic": topic, "subscriber": "true", @@ -89,7 +89,7 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio endpoints = append(endpoints, ®istry.Endpoint{ Name: name + "." + method.Name, - Request: extractSubValue(method.Type), + Request: registry.ExtractSubValue(method.Type), Metadata: map[string]string{ "topic": topic, "subscriber": "true", @@ -109,60 +109,6 @@ func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOptio } } -func validateSubscriber(sub server.Subscriber) error { - typ := reflect.TypeOf(sub.Subscriber()) - var argType reflect.Type - - if typ.Kind() == reflect.Func { - name := "Func" - switch typ.NumIn() { - case 2: - argType = typ.In(1) - default: - return fmt.Errorf("subscriber %v takes wrong number of args: %v required signature %s", name, typ.NumIn(), subSig) - } - if !isExportedOrBuiltinType(argType) { - return fmt.Errorf("subscriber %v argument type not exported: %v", name, argType) - } - if typ.NumOut() != 1 { - return fmt.Errorf("subscriber %v has wrong number of outs: %v require signature %s", - name, typ.NumOut(), subSig) - } - if returnType := typ.Out(0); returnType != typeOfError { - return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String()) - } - } else { - hdlr := reflect.ValueOf(sub.Subscriber()) - name := reflect.Indirect(hdlr).Type().Name() - - for m := 0; m < typ.NumMethod(); m++ { - method := typ.Method(m) - - switch method.Type.NumIn() { - case 3: - argType = method.Type.In(2) - default: - return fmt.Errorf("subscriber %v.%v takes wrong number of args: %v required signature %s", - name, method.Name, method.Type.NumIn(), subSig) - } - - if !isExportedOrBuiltinType(argType) { - return fmt.Errorf("%v argument type not exported: %v", name, argType) - } - if method.Type.NumOut() != 1 { - return fmt.Errorf( - "subscriber %v.%v has wrong number of outs: %v require signature %s", - name, method.Name, method.Type.NumOut(), subSig) - } - if returnType := method.Type.Out(0); returnType != typeOfError { - return fmt.Errorf("subscriber %v.%v returns %v not error", name, method.Name, returnType.String()) - } - } - } - - return nil -} - func (g *grpcServer) createSubHandler(sb *subscriber, opts server.Options) broker.Handler { return func(p broker.Event) (err error) { defer func() {