2 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
因?yàn)槟惶幚砗瘮?shù),所以可以使用 reflect 包在運(yùn)行時(shí)生成函數(shù)。
使用對(duì)象類(lèi)型 (Volume, Vpc) 可以導(dǎo)出所有后續(xù)信息,以提供一個(gè)完全通用的實(shí)現(xiàn),該實(shí)現(xiàn)非常枯燥,甚至更復(fù)雜、更慢。
package main
import (
? ? "errors"
? ? "fmt"
? ? "reflect"
)
func main() {
? ? fmt.Printf("%T\n", getter(Volume{}))
? ? fmt.Printf("%T\n", getter(Vpc{}))
}
type DescribeVolumesInput struct{}
type DescribeVpcs struct{}
type Volume struct{}
type Vpc struct{}
type Session struct{}
type Client struct{}
func New(s *Session) Client { return Client{} }
var typeRegistry = make(map[string]reflect.Type)
func init() {
? ? some := []interface{}{DescribeVolumesInput{}, DescribeVpcs{}}
? ? for _, v := range some {
? ? ? ? typeRegistry[fmt.Sprintf("%T", v)] = reflect.TypeOf(v)
? ? }
}
var errV = errors.New("")
var errType = reflect.ValueOf(&errV).Elem().Type()
var zeroErr = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
var nilErr = []reflect.Value{zeroErr}
func getter(of interface{}) interface{} {
? ? outType := reflect.SliceOf(reflect.PtrTo(reflect.TypeOf(of)))
? ? fnType := reflect.FuncOf([]reflect.Type{reflect.TypeOf(new(Session))}, []reflect.Type{outType, errType}, false)
? ? fnBody := func(input []reflect.Value) []reflect.Value {
? ? ? ? client := reflect.ValueOf(New).Call(input)[0]
? ? ? ? t := reflect.MakeSlice(outType, 0, 0)
? ? ? ? name := fmt.Sprintf("Describe%TsInput", of)
? ? ? ? descInput := reflect.New(typeRegistry[name]).Elem()
? ? ? ? mName := fmt.Sprintf("Describe%Ts", of)
? ? ? ? meth := client.MethodByName(mName)
? ? ? ? if !meth.IsValid() {
? ? ? ? ? ? return []reflect.Value{
? ? ? ? ? ? ? ? t,
? ? ? ? ? ? ? ? reflect.ValueOf(fmt.Errorf("no such method %q", mName)),
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for {
? ? ? ? ? ? out := meth.Call([]reflect.Value{descInput.Addr()})
? ? ? ? ? ? if len(out) > 0 {
? ? ? ? ? ? ? ? errOut := out[len(out)-1]
? ? ? ? ? ? ? ? if errOut.Type().Implements(errType) && errOut.IsNil() == false {
? ? ? ? ? ? ? ? ? ? return []reflect.Value{t, errOut}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? result := out[1]
? ? ? ? ? ? fName := fmt.Sprintf("%Ts", of)
? ? ? ? ? ? if x := result.FieldByName(fName); x.IsValid() {
? ? ? ? ? ? ? ? t = reflect.AppendSlice(t, x)
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? return []reflect.Value{
? ? ? ? ? ? ? ? ? ? t,
? ? ? ? ? ? ? ? ? ? reflect.ValueOf(fmt.Errorf("field not found %q", fName)),
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if x := result.FieldByName("NextToken"); x.IsValid() {
? ? ? ? ? ? ? ? descInput.FieldByName("NextToken").Set(x)
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? break
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return []reflect.Value{t, zeroErr}
? ? }
? ? fn := reflect.MakeFunc(fnType, fnBody)
? ? return fn.Interface()
}

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
代理第 3 方 API,用 go 實(shí)現(xiàn)非常簡(jiǎn)單,下面是它是如何用 endly e2e test runner?AWS 代理實(shí)現(xiàn)的
我會(huì)說(shuō) AWS API 是代理的完美候選者,只要反射性能價(jià)格不是問(wèn)題。
像kubernetes這樣的其他一些第三方 API?更具挑戰(zhàn)性,但仍然很容易用 go 代理,這是反射和代碼生成的結(jié)合。
- 2 回答
- 0 關(guān)注
- 155 瀏覽
添加回答
舉報(bào)