go.uber.org/yarpc@v1.72.1/yarpcconfig/util_for_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package yarpcconfig 22 23 import ( 24 "reflect" 25 26 "github.com/golang/mock/gomock" 27 ) 28 29 // builderFunc builds a `build` function that calls into the controller. 30 // 31 // The generated function has the signature, 32 // 33 // func $name($args[0], $args[1], ..., $args[N]) ($output, error) 34 // 35 // This function may be fed as an argument to BuildTransport, BuildInbound, 36 // etc. and it will be interpreted correctly. 37 func builderFunc(c *gomock.Controller, o interface{}, name string, argTypes []reflect.Type, output reflect.Type) interface{} { 38 // We dynamically generate a function with the correct arg type rather 39 // than interface{} because we want to verify we're getting the correct 40 // type decoded. 41 42 resultTypes := []reflect.Type{output, _typeOfError} 43 return reflect.MakeFunc( 44 reflect.FuncOf(argTypes, resultTypes, false), 45 func(callArgs []reflect.Value) []reflect.Value { 46 args := make([]interface{}, len(callArgs)) 47 for i, a := range callArgs { 48 args[i] = a.Interface() 49 } 50 51 results := c.Call(o, name, args...) 52 callResults := make([]reflect.Value, len(results)) 53 for i, r := range results { 54 // Use zero-value where the result is nil because 55 // reflect.ValueOf(nil) is an error. 56 if r == nil { 57 callResults[i] = reflect.Zero(resultTypes[i]) 58 continue 59 } 60 61 callResults[i] = reflect.ValueOf(r).Convert(resultTypes[i]) 62 } 63 64 return callResults 65 }, 66 ).Interface() 67 }