當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Value.MethodByName方法代碼示例

本文整理匯總了Golang中reflect.Value.MethodByName方法的典型用法代碼示例。如果您正苦於以下問題:Golang Value.MethodByName方法的具體用法?Golang Value.MethodByName怎麽用?Golang Value.MethodByName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在reflect.Value的用法示例。


在下文中一共展示了Value.MethodByName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getReflectValue

//get the value with with the matching name inside v.
//This value can be a struct field, a method attached to a struct or a value in a map
func getReflectValue(v reflect.Value, valueName string) (value reflect.Value, ok bool) {

	// first check if input was a map and handle that.
	// otherwise input was a struct or pointer to a struct
	if v.Kind() == reflect.Map {
		value = v.MapIndex(reflect.ValueOf(valueName))
		if value.IsValid() {
			if value.Kind() == reflect.Func {
				value = getFuncSingleReturnValue(value)
			}
			ok = true
			return value, true
		}
	}

	value = reflect.Indirect(v).FieldByName(valueName)
	if value.IsValid() {
		if value.Kind() == reflect.Func {
			value = getFuncSingleReturnValue(value)
		}
		ok = true
		return
	}

	value = v.MethodByName(valueName)
	if value.IsValid() {
		value = getFuncSingleReturnValue(value)
		ok = true
		return
	}

	return
}
開發者ID:MarkBruns,項目名稱:goson,代碼行數:35,代碼來源:reflect_utils.go

示例2: GetMethod

func GetMethod(s reflect.Value, name string) reflect.Value {
	method := s.MethodByName(name)
	if !method.IsValid() {
		method = s.Elem().MethodByName(name)
	}
	return method
}
開發者ID:chzyer,項目名稱:flagly,代碼行數:7,代碼來源:option.go

示例3: Invoke

func (p *Bean) Invoke(methodName string, args Args, callback ...interface{}) (returnFunc func(), err error) {
	var beanValue reflect.Value

	beanValue = reflect.ValueOf(p.instance)

	inputs := make([]reflect.Value, len(args))

	for i := range args {
		inputs[i] = reflect.ValueOf(args[i])
	}

	values := beanValue.MethodByName(methodName).Call(inputs)

	if values != nil && len(values) > 0 {
		lastV := values[len(values)-1]
		if lastV.Interface() != nil {
			if errV, ok := lastV.Interface().(error); ok {
				if errV != nil {
					err = errV
					return
				}
			}
		}
	}

	if callback != nil && len(callback) > 0 {
		returnFunc = func() {
			reflect.ValueOf(callback[0]).Call(values)
		}
	}

	return
}
開發者ID:gogap,項目名稱:aop,代碼行數:33,代碼來源:bean.go

示例4: runMethodIfExists

func runMethodIfExists(v reflect.Value, name string, args ...interface{}) {
	method := v.MethodByName(name)
	if method.Kind() == reflect.Invalid {
		return
	}

	if method.Type().NumIn() != len(args) {
		panic(fmt.Sprintf(
			"%s: expected %d args, actually %d.",
			name,
			len(args),
			method.Type().NumIn()))
	}

	// Create a slice of reflect.Values to pass to the method. Simultaneously
	// check types.
	argVals := make([]reflect.Value, len(args))
	for i, arg := range args {
		argVal := reflect.ValueOf(arg)

		if argVal.Type() != method.Type().In(i) {
			panic(fmt.Sprintf(
				"%s: expected arg %d to have type %v.",
				name,
				i,
				argVal.Type()))
		}

		argVals[i] = argVal
	}

	method.Call(argVals)
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:33,代碼來源:run_tests.go

示例5: applyToStruct

func applyToStruct(obj reflect.Value, head P, mid string, tail P, ctx *Context) error {
	if mid != "*" {
		result := obj.FieldByName(mid)

		if result.Kind() == reflect.Invalid {
			result = obj.MethodByName(mid)
		}

		if result.Kind() == reflect.Invalid {
			return fmt.Errorf("no field '%s' in type '%s' at '%s'", mid, obj.Type(), head)
		}

		return apply(result, append(head, mid), tail, ctx)
	}

	typ := obj.Type()

	for i := 0; i < typ.NumField() && !ctx.stop; i++ {
		if err := applyToStruct(obj, head, typ.Field(i).Name, tail, ctx); err != nil && err != ErrMissing {
			return err
		}
	}

	return nil
}
開發者ID:nativetouch,項目名稱:gopath,代碼行數:25,代碼來源:apply.go

示例6: isEmptyValue

// from encoding/json
func isEmptyValue(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
		return v.Len() == 0
	case reflect.Bool:
		return !v.Bool()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		return v.Float() == 0
	case reflect.Interface, reflect.Ptr:
		return v.IsNil()
	}
	z := v.MethodByName("IsZero")
	if z.IsValid() {
		return z.Call(nil)[0].Interface().(bool)
	}
	// This shouldn't really happen...
	zero := reflect.Zero(v.Type()).Interface()
	current := v.Interface()
	return reflect.DeepEqual(current, zero)

}
開發者ID:epek,項目名稱:structs,代碼行數:26,代碼來源:structs.go

示例7: getReflectValue

//get the value with with the matching name inside v.
//This value can be a struct field, a method attached to a struct or a value in a map
func getReflectValue(v reflect.Value, valueName string) (reflect.Value, bool) {
	value := reflect.Indirect(v).FieldByName(valueName)
	if value.IsValid() {
		if value.Kind() == reflect.Func {
			value = getFuncSingleReturnValue(value)
		}
		return value, true
	}

	if v.Kind() == reflect.Map {
		value = v.MapIndex(reflect.ValueOf(valueName))
		if value.IsValid() {
			if value.Kind() == reflect.Func {
				value = getFuncSingleReturnValue(value)
			}
			return value, true
		}
	}

	value = v.MethodByName(valueName)
	if value.IsValid() {
		value = getFuncSingleReturnValue(value)
		return value, true
	}

	return reflect.Value{}, false
}
開發者ID:johnvilsack,項目名稱:golang-stuff,代碼行數:29,代碼來源:reflect_utils.go

示例8: getResult

func getResult(method string, vResourcePtr *reflect.Value) (result interface{}) {
	defer func() {
		if r := recover(); r != nil {
			rstr := fmt.Sprintf("%s", r)
			result = internalError{
				where: lineInfo(3),
				why:   rstr + fullTrace(5, "\n\t"),
				code:  errorCode(rstr),
			}
		}
	}()

	methodName := strings.Title(strings.ToLower(method))
	if methodName == "Head" {
		methodName = "Get"
	}
	vMethod := vResourcePtr.MethodByName(methodName)
	if vMethod.IsValid() {
		result = vMethod.Call([]reflect.Value{})[0].Interface()
	}

	if result == nil {
		return methodNotAllowed{getAllowed(vResourcePtr)}
	}
	return
}
開發者ID:aoyama-nanami,項目名稱:vitali,代碼行數:26,代碼來源:vitali.go

示例9: MethodByName

// get public method by name
func MethodByName(v reflect.Value, name string) (m reflect.Value, ok bool) {
	m = v.MethodByName(strings.Title(name))
	if m.IsValid() && m.Kind() == reflect.Func {
		return m, true
	}
	return
}
開發者ID:samuelyao314,項目名稱:mygo,代碼行數:8,代碼來源:gotype.go

示例10: getMethodValue

// getMethodValue the method value from the method context and method name
func getMethodValue(t *Task) (method reflect.Value, err error) {
	// Will look for the method on both a
	// pointer and a value receiver
	var ptr reflect.Value
	var val reflect.Value

	val = reflect.ValueOf(t.MethodState)

	if val.Type().Kind() == reflect.Ptr {
		ptr = val
		val = ptr.Elem()
	} else {
		ptr = reflect.New(val.Type())
		ptr.Elem().Set(val)
	}

	method = val.MethodByName(t.Method)
	if method.IsValid() {
		return
	}

	method = ptr.MethodByName(t.Method)
	if method.IsValid() {
		return
	}

	err = errors.New("Method not found")

	return
}
開發者ID:kzvezdarov,項目名稱:alexander,代碼行數:31,代碼來源:task_handler.go

示例11: function2Func

func function2Func(in *reflect.Value, t reflect.Type) {

	fn := in.MethodByName("Call")
	wrap := func(args []reflect.Value) (results []reflect.Value) {
		ret := fn.Call(args)[0]
		n := t.NumOut()
		if n == 0 {
			return
		}
		if n == 1 {
			if t.Out(0) != typeIntf {
				ret = ret.Elem()
			}
			return []reflect.Value{ret}
		}
		if ret.Kind() != reflect.Slice || ret.Len() != n {
			panic(fmt.Sprintf("unexpected return value count, we need `%d` values", n))
		}
		results = make([]reflect.Value, n)
		for i := 0; i < n; i++ {
			result := ret.Index(i)
			if t.Out(i) != typeIntf {
				result = result.Elem()
			}
			results[i] = result
		}
		return
	}
	*in = reflect.MakeFunc(t, wrap)
}
開發者ID:ttthzy,項目名稱:qlang,代碼行數:30,代碼來源:call.go

示例12: SafelyCall

// safelyCall invokes `function` in recover block
func (a *App) SafelyCall(vc reflect.Value, method string, args []reflect.Value) (resp []reflect.Value, err error) {
	defer func() {
		if e := recover(); e != nil {
			if !a.Server.Config.RecoverPanic {
				// go back to panic
				panic(e)
			} else {
				resp = nil
				var content string
				content = fmt.Sprintf("Handler crashed with error: %v", e)
				for i := 1; ; i += 1 {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					} else {
						content += "\n"
					}
					content += fmt.Sprintf("%v %v", file, line)
				}
				a.Error(content)
				err = errors.New(content)
				return
			}
		}
	}()
	function := vc.MethodByName(method)
	return function.Call(args), err
}
開發者ID:tempbottle,項目名稱:xweb,代碼行數:29,代碼來源:app.go

示例13: structCanBeMarshalled

func structCanBeMarshalled(v reflect.Value) bool {
	m := v.MethodByName("String")
	if m.IsValid() && !m.IsNil() {
		return true
	}
	return false
}
開發者ID:ssokol,項目名稱:stratux,代碼行數:7,代碼來源:datalog.go

示例14: callMethod

func (scope *Scope) callMethod(methodName string, reflectValue reflect.Value) {
	// Only get address from non-pointer
	if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
		reflectValue = reflectValue.Addr()
	}

	if methodValue := reflectValue.MethodByName(methodName); methodValue.IsValid() {
		switch method := methodValue.Interface().(type) {
		case func():
			method()
		case func(*Scope):
			method(scope)
		case func(*DB):
			newDB := scope.NewDB()
			method(newDB)
			scope.Err(newDB.Error)
		case func() error:
			scope.Err(method())
		case func(*Scope) error:
			scope.Err(method(scope))
		case func(*DB) error:
			newDB := scope.NewDB()
			scope.Err(method(newDB))
			scope.Err(newDB.Error)
		default:
			scope.Err(fmt.Errorf("unsupported function %v", methodName))
		}
	}
}
開發者ID:RichardKnop,項目名稱:example-api,代碼行數:29,代碼來源:scope.go

示例15: callGoMethod

func callGoMethod(L *lua.State, name string, st reflect.Value) {
	ret := st.MethodByName(name)
	if !ret.IsValid() {
		fmt.Println("whoops")
	}
	L.PushGoFunction(GoLuaFunc(L, ret))
}
開發者ID:sanyaade-embedded-systems,項目名稱:luar,代碼行數:7,代碼來源:luar.go


注:本文中的reflect.Value.MethodByName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。