move avay grpc stuff to micro-server-grpc

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-05 23:46:11 +03:00
parent ce25a41fe1
commit 31180758b4
2 changed files with 0 additions and 103 deletions

View File

@ -1,57 +0,0 @@
package grpc
import (
"fmt"
"strings"
)
// ServiceMethod converts a gRPC method to a Go method
// Input:
// Foo.Bar, /Foo/Bar, /package.Foo/Bar, /a.package.Foo/Bar
// Output:
// [Foo, Bar]
func ServiceMethod(m string) (string, string, error) {
if len(m) == 0 {
return "", "", fmt.Errorf("malformed method name: %q", m)
}
// grpc method
if m[0] == '/' {
// [ , Foo, Bar]
// [ , package.Foo, Bar]
// [ , a.package.Foo, Bar]
parts := strings.Split(m, "/")
if len(parts) != 3 || len(parts[1]) == 0 || len(parts[2]) == 0 {
return "", "", fmt.Errorf("malformed method name: %q", m)
}
service := strings.Split(parts[1], ".")
return service[len(service)-1], parts[2], nil
}
// non grpc method
parts := strings.Split(m, ".")
// expect [Foo, Bar]
if len(parts) != 2 {
return "", "", fmt.Errorf("malformed method name: %q", m)
}
return parts[0], parts[1], nil
}
// ServiceFromMethod returns the service
// /service.Foo/Bar => service
func ServiceFromMethod(m string) string {
if len(m) == 0 {
return m
}
if m[0] != '/' {
return m
}
parts := strings.Split(m, "/")
if len(parts) < 3 {
return m
}
parts = strings.Split(parts[1], ".")
return strings.Join(parts[:len(parts)-1], ".")
}

View File

@ -1,46 +0,0 @@
package grpc
import (
"testing"
)
func TestServiceMethod(t *testing.T) {
type testCase struct {
input string
service string
method string
err bool
}
methods := []testCase{
{"Foo.Bar", "Foo", "Bar", false},
{"/Foo/Bar", "Foo", "Bar", false},
{"/package.Foo/Bar", "Foo", "Bar", false},
{"/a.package.Foo/Bar", "Foo", "Bar", false},
{"a.package.Foo/Bar", "", "", true},
{"/Foo/Bar/Baz", "", "", true},
{"Foo.Bar.Baz", "", "", true},
}
for _, test := range methods {
service, method, err := ServiceMethod(test.input)
if err != nil && test.err == true {
continue
}
// unexpected error
if err != nil && test.err == false {
t.Fatalf("unexpected err %v for %+v", err, test)
}
// expecter error
if test.err == true && err == nil {
t.Fatalf("expected error for %+v: got service: %s method: %s", test, service, method)
}
if service != test.service {
t.Fatalf("wrong service for %+v: got service: %s method: %s", test, service, method)
}
if method != test.method {
t.Fatalf("wrong method for %+v: got service: %s method: %s", test, service, method)
}
}
}