github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/grpc/util.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/server/grpc/util.go 16 17 package grpc 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "os" 24 "strings" 25 "sync" 26 27 "google.golang.org/grpc/codes" 28 ) 29 30 // convertCode converts a standard Go error into its canonical code. Note that 31 // this is only used to translate the error returned by the server applications. 32 func convertCode(err error) codes.Code { 33 switch err { 34 case nil: 35 return codes.OK 36 case io.EOF: 37 return codes.OutOfRange 38 case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF: 39 return codes.FailedPrecondition 40 case os.ErrInvalid: 41 return codes.InvalidArgument 42 case context.Canceled: 43 return codes.Canceled 44 case context.DeadlineExceeded: 45 return codes.DeadlineExceeded 46 } 47 switch { 48 case os.IsExist(err): 49 return codes.AlreadyExists 50 case os.IsNotExist(err): 51 return codes.NotFound 52 case os.IsPermission(err): 53 return codes.PermissionDenied 54 } 55 return codes.Unknown 56 } 57 58 func wait(ctx context.Context) *sync.WaitGroup { 59 if ctx == nil { 60 return nil 61 } 62 wg, ok := ctx.Value("wait").(*sync.WaitGroup) 63 if !ok { 64 return nil 65 } 66 return wg 67 } 68 69 // ServiceMethod converts a gRPC method to a Go method 70 // Input: 71 // Foo.Bar, /Foo/Bar, /package.Foo/Bar, /a.package.Foo/Bar 72 // Output: 73 // [Foo, Bar] 74 func ServiceMethod(m string) (string, string, error) { 75 if len(m) == 0 { 76 return "", "", fmt.Errorf("malformed method name: %q", m) 77 } 78 79 // grpc method 80 if m[0] == '/' { 81 // [ , Foo, Bar] 82 // [ , package.Foo, Bar] 83 // [ , a.package.Foo, Bar] 84 parts := strings.Split(m, "/") 85 if len(parts) != 3 || len(parts[1]) == 0 || len(parts[2]) == 0 { 86 return "", "", fmt.Errorf("malformed method name: %q", m) 87 } 88 service := strings.Split(parts[1], ".") 89 return service[len(service)-1], parts[2], nil 90 } 91 92 // non grpc method 93 parts := strings.Split(m, ".") 94 95 // expect [Foo, Bar] 96 if len(parts) != 2 { 97 return "", "", fmt.Errorf("malformed method name: %q", m) 98 } 99 100 return parts[0], parts[1], nil 101 } 102 103 // ServiceFromMethod returns the service 104 // /service.Foo/Bar => service 105 func ServiceFromMethod(m string) string { 106 if len(m) == 0 { 107 return m 108 } 109 if m[0] != '/' { 110 return m 111 } 112 parts := strings.Split(m, "/") 113 if len(parts) < 3 { 114 return m 115 } 116 parts = strings.Split(parts[1], ".") 117 return strings.Join(parts[:len(parts)-1], ".") 118 }