当前位置: 首页>>代码示例>>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;未经允许,请勿转载。