From 762f20d179548aa2e395b4443a1fc779f1853cfe Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 13 Dec 2020 13:51:09 +0300 Subject: [PATCH] registry: adopt micro/micro 69b0ac2e9140fee1cde043f5ecdab438a41898ee Signed-off-by: Vasiliy Tolstov --- registry/extractor.go | 17 +++++++++++++++++ registry/extractor_test.go | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/registry/extractor.go b/registry/extractor.go index 038d4c48..779e85fd 100644 --- a/registry/extractor.go +++ b/registry/extractor.go @@ -4,6 +4,8 @@ import ( "fmt" "reflect" "strings" + "unicode" + "unicode/utf8" "github.com/unistack-org/micro/v3/metadata" ) @@ -21,6 +23,18 @@ func ExtractValue(v reflect.Type, d int) *Value { v = v.Elem() } + if len(v.Name()) == 0 { + return nil + } + + // get the rune character + a, _ := utf8.DecodeRuneInString(string(v.Name()[0])) + + // crude check for is unexported field + if unicode.IsLower(a) { + return nil + } + arg := &Value{ Name: v.Name(), Type: v.Name(), @@ -91,6 +105,9 @@ func ExtractEndpoint(method reflect.Method) *Endpoint { request := ExtractValue(reqType, 0) response := ExtractValue(rspType, 0) + if request == nil || response == nil { + return nil + } ep := &Endpoint{ Name: method.Name, diff --git a/registry/extractor_test.go b/registry/extractor_test.go index a30d95ef..be5bd996 100644 --- a/registry/extractor_test.go +++ b/registry/extractor_test.go @@ -6,18 +6,18 @@ import ( "testing" ) -type testHandler struct{} +type TestHandler struct{} -type testRequest struct{} +type TestRequest struct{} -type testResponse struct{} +type TestResponse struct{} -func (t *testHandler) Test(ctx context.Context, req *testRequest, rsp *testResponse) error { +func (t *TestHandler) Test(ctx context.Context, req *TestRequest, rsp *TestResponse) error { return nil } func TestExtractEndpoint(t *testing.T) { - handler := &testHandler{} + handler := &TestHandler{} typ := reflect.TypeOf(handler) var endpoints []*Endpoint @@ -29,35 +29,35 @@ func TestExtractEndpoint(t *testing.T) { } if i := len(endpoints); i != 1 { - t.Errorf("Expected 1 endpoint, have %d", i) + t.Fatalf("Expected 1 endpoint, have %d", i) } if endpoints[0].Name != "Test" { - t.Errorf("Expected handler Test, got %s", endpoints[0].Name) + t.Fatalf("Expected handler Test, got %s", endpoints[0].Name) } if endpoints[0].Request == nil { - t.Error("Expected non nil request") + t.Fatal("Expected non nil Request") } if endpoints[0].Response == nil { - t.Error("Expected non nil request") + t.Fatal("Expected non nil Request") } - if endpoints[0].Request.Name != "testRequest" { - t.Errorf("Expected testRequest got %s", endpoints[0].Request.Name) + if endpoints[0].Request.Name != "TestRequest" { + t.Fatalf("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].Response.Name != "TestResponse" { + t.Fatalf("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].Request.Type != "TestRequest" { + t.Fatalf("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) + if endpoints[0].Response.Type != "TestResponse" { + t.Fatalf("Expected TestResponse type got %s", endpoints[0].Response.Type) } }