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


Golang Value.Call方法代碼示例

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


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

示例1: convertAndSet

func convertAndSet(to, from reflect.Value, setMethod reflect.Value) {
	var toType reflect.Type
	if setMethod.IsValid() {
		toType = setMethod.Type().In(0)
	} else {
		toType = to.Type()
	}
	fromType := from.Type()
	defer func() {
		if v := recover(); v != nil {
			panic(fmt.Sprintf("cannot use %s as a %s", fromType, toType))
		}
	}()
	if fromType == listType && toType.Kind() == reflect.Slice {
		list := from.Interface().(*List)
		from = reflect.MakeSlice(toType, len(list.data), len(list.data))
		elemType := toType.Elem()
		for i, elem := range list.data {
			from.Index(i).Set(reflect.ValueOf(elem).Convert(elemType))
		}
	} else if toType != fromType {
		from = from.Convert(toType)
	}
	if setMethod.IsValid() {
		setMethod.Call([]reflect.Value{from})
	} else {
		to.Set(from)
	}
}
開發者ID:reedobrien,項目名稱:qml,代碼行數:29,代碼來源:bridge.go

示例2: funcall

func funcall(m reflect.Value, args []reflect.Value) (v []reflect.Value, panicked interface{}) {
	// defer func() {
	// 	panicked = recover()
	// }()
	ret := m.Call(args)
	return ret, nil
}
開發者ID:dominikh,項目名稱:go-quickcheck,代碼行數:7,代碼來源:quick.go

示例3: endpoint

// Returns an endpoint that will be bound to a handler.  Internally calls the
// method endpoint
func (rm *ResourceManager) endpoint(
	res Resource, getData func(http.ResponseWriter, *http.Request) error,
	endpoint reflect.Value) http.HandlerFunc {

	callback := func(w http.ResponseWriter, r *http.Request) {

		// Call the get data function, attempting to get item and list for
		// resource if it allows
		err := getData(w, r)
		if err != nil {
			return
		}

		// Call original endpoint
		args := []reflect.Value{reflect.ValueOf(w), reflect.ValueOf(r)}
		endpoint.Call(args)
	}

	// If this resource has a wrapper around all calls to the resource
	// endpoints, then return a function that calls that wrapper.  Else,
	// just return the original call function.  Developer will have to manually
	// call the callback function in the wrapper method
	return func(w http.ResponseWriter, r *http.Request) {

		// Set the resource on the current context.
		context.Set(r, "Resource", res)

		if wrapper, ok := interface{}(res).(HandlerWrapper); ok {
			wrapper.HandlerWrapper(w, r, callback)
		} else {
			callback(w, r)
		}
	}
}
開發者ID:johnnadratowski,項目名稱:resourceful,代碼行數:36,代碼來源:resourceful.go

示例4: callCustom

// callCustom calls 'custom' with sv & dv. custom must be a conversion function.
func (c *Converter) callCustom(sv, dv, custom reflect.Value, scope *scope) error {
	if !sv.CanAddr() {
		sv2 := reflect.New(sv.Type())
		sv2.Elem().Set(sv)
		sv = sv2
	} else {
		sv = sv.Addr()
	}
	if !dv.CanAddr() {
		if !dv.CanSet() {
			return scope.errorf("can't addr or set dest.")
		}
		dvOrig := dv
		dv := reflect.New(dvOrig.Type())
		defer func() { dvOrig.Set(dv) }()
	} else {
		dv = dv.Addr()
	}
	args := []reflect.Value{sv, dv, reflect.ValueOf(scope)}
	ret := custom.Call(args)[0].Interface()
	// This convolution is necessary because nil interfaces won't convert
	// to errors.
	if ret == nil {
		return nil
	}
	return ret.(error)
}
開發者ID:MohamedFAhmed,項目名稱:heapster,代碼行數:28,代碼來源:converter.go

示例5: eachCall

func eachCall(fn, v, i reflect.Value) {
	args := []reflect.Value{v}
	if in := fn.Type().NumIn(); in == 2 {
		args = append(args, i)
	}
	fn.Call(args)
}
開發者ID:newday1,項目名稱:go-underscore,代碼行數:7,代碼來源:each.go

示例6: evalCall

// evalCall executes a function or method call. If it's a method, fun already has the receiver bound, so
// it looks just like a function call. The arg list, if non-nil, includes (in the manner of the shell), arg[0]
// as the function itself.
func (s *state) evalCall(dot, fun reflect.Value, node parse.Node, name string, args []parse.Node, final reflect.Value) reflect.Value {
	if args != nil {
		args = args[1:] // Zeroth arg is function name/node; not passed to function.
	}
	typ := fun.Type()
	numIn := len(args)
	if final.IsValid() {
		numIn++
	}
	numFixed := len(args)
	if typ.IsVariadic() {
		numFixed = typ.NumIn() - 1 // last arg is the variadic one.
		if numIn < numFixed {
			s.errorf("wrong number of args for %s: want at least %d got %d", name, typ.NumIn()-1, len(args))
		}
	} else if numIn < typ.NumIn()-1 || !typ.IsVariadic() && numIn != typ.NumIn() {
		s.errorf("wrong number of args for %s: want %d got %d", name, typ.NumIn(), len(args))
	}
	if !goodFunc(typ) {
		// TODO: This could still be a confusing error; maybe goodFunc should provide info.
		s.errorf("can't call method/function %q with %d results", name, typ.NumOut())
	}
	// Build the arg list.
	argv := make([]reflect.Value, numIn)
	// Args must be evaluated. Fixed args first.
	i := 0
	for ; i < numFixed && i < len(args); i++ {
		argv[i] = s.evalArg(dot, typ.In(i), args[i])
	}
	// Now the ... args.
	if typ.IsVariadic() {
		argType := typ.In(typ.NumIn() - 1).Elem() // Argument is a slice.
		for ; i < len(args); i++ {
			argv[i] = s.evalArg(dot, argType, args[i])
		}
	}
	// Add final value if necessary.
	if final.IsValid() {
		t := typ.In(typ.NumIn() - 1)
		if typ.IsVariadic() {
			if numIn-1 < numFixed {
				// The added final argument corresponds to a fixed parameter of the function.
				// Validate against the type of the actual parameter.
				t = typ.In(numIn - 1)
			} else {
				// The added final argument corresponds to the variadic part.
				// Validate against the type of the elements of the variadic slice.
				t = t.Elem()
			}
		}
		argv[i] = s.validateType(final, t)
	}
	result := fun.Call(argv)
	// If we have an error that is not nil, stop execution and return that error to the caller.
	if len(result) == 2 && !result[1].IsNil() {
		s.at(node)
		s.errorf("error calling %s: %s", name, result[1].Interface().(error))
	}
	return result[0]
}
開發者ID:sreis,項目名稱:go,代碼行數:63,代碼來源:exec.go

示例7: Invoke

func Invoke(FuncValue reflect.Value, args ...interface{}) []reflect.Value {
	in := make([]reflect.Value, len(args))
	for k, arg := range args {
		in[k] = reflect.ValueOf(arg)
	}
	return FuncValue.Call(in)
}
開發者ID:ianwoolf,項目名稱:golib,代碼行數:7,代碼來源:funcmap.go

示例8: generateIt

func (t TableEntry) generateIt(itBody reflect.Value) {
	if t.Pending {
		ginkgo.PIt(t.Description)
		return
	}

	values := []reflect.Value{}
	for i, param := range t.Parameters {
		var value reflect.Value

		if param == nil {
			inType := itBody.Type().In(i)
			value = reflect.Zero(inType)
		} else {
			value = reflect.ValueOf(param)
		}

		values = append(values, value)
	}

	body := func() {
		itBody.Call(values)
	}

	if t.Focused {
		ginkgo.FIt(t.Description, body)
	} else {
		ginkgo.It(t.Description, body)
	}
}
開發者ID:Clarifai,項目名稱:kubernetes,代碼行數:30,代碼來源:table_entry.go

示例9: funcEvaluate

func funcEvaluate(L *lua.LState, fn reflect.Value) int {
	fnType := fn.Type()
	top := L.GetTop()
	expected := fnType.NumIn()
	variadic := fnType.IsVariadic()
	if !variadic && top != expected {
		L.RaiseError("invalid number of function argument (%d expected, got %d)", expected, top)
	}
	if variadic && top < expected-1 {
		L.RaiseError("invalid number of function argument (%d or more expected, got %d)", expected-1, top)
	}
	args := make([]reflect.Value, top)
	for i := 0; i < L.GetTop(); i++ {
		var hint reflect.Type
		if variadic && i >= expected-1 {
			hint = fnType.In(expected - 1).Elem()
		} else {
			hint = fnType.In(i)
		}
		args[i] = lValueToReflect(L.Get(i+1), hint)
	}
	ret := fn.Call(args)
	for _, val := range ret {
		L.Push(New(L, val.Interface()))
	}
	return len(ret)
}
開發者ID:evangelistcollective,項目名稱:hustlebot,代碼行數:27,代碼來源:func.go

示例10: run

func (t *Task) run(m reflect.Value, input Reader, output Writer) (err error) {
	collector := NewWriterCollector(output)
	colValue := reflect.ValueOf(collector)

	defer func() {
		if e := output.Close(); e != nil && err == nil {
			err = e
		}
	}()

	var k, v interface{}
	for {
		k, v, err = input.Next()
		if err != nil {
			if err == io.EOF {
				return nil
			}
			log.Printf("Read error: %s", err)
			return
		}
		m.Call([]reflect.Value{
			reflect.ValueOf(k),
			reflect.ValueOf(v),
			colValue,
		})
	}

	return
}
開發者ID:kidoman,項目名稱:gossamr,代碼行數:29,代碼來源:task.go

示例11: TryCall

// TryCall attempts to call the task with the supplied arguments.
//
// `err` is set in the return value in two cases:
// 1. The reflected function invocation panics (e.g. due to a mismatched
//    argument list).
// 2. The task func itself returns a non-nil error.
func TryCall(f reflect.Value, args []reflect.Value, uuid reflect.Value) (results []reflect.Value, err error) {
	defer func() {
		// Recover from panic and set err.
		if e := recover(); e != nil {
			switch e := e.(type) {
			default:
				err = errors.New("Invoking task caused a panic")
			case error:
				err = e
			case string:
				err = errors.New(e)
			}
		}
	}()

	// results = f.Call(args)
	results = f.Call(append([]reflect.Value{uuid}, args...))
	// If an error was returned by the task func, propagate it
	// to the caller via err.
	if !results[1].IsNil() {
		return nil, results[1].Interface().(error)
	}

	return results, err
}
開發者ID:gooops,項目名稱:machinery,代碼行數:31,代碼來源:worker.go

示例12: shrinkOne

func shrinkOne(f reflect.Value, args []reflect.Value, ai int, shrinker Shrinker) (bool, error) {

	if shrinker == nil {
		return false, nil
	}

	for tactic := 0; tactic < MaxTactics; tactic++ {
		cur := args[ai]
		nv, err := shrinker(cur, tactic)
		if err != nil {
			switch err {
			case ErrNoMoreTactics:
				return false, nil
			case ErrDeadEnd:
				continue
			default:
				return false, err
			}
		}

		args[ai] = nv

		fret := f.Call(args)[0].Bool()
		if !fret {
			return true, nil
		}
		args[ai] = cur
	}

	return false, nil
}
開發者ID:dgryski,項目名稱:go-shrink,代碼行數:31,代碼來源:shrink.go

示例13: safelyCall

// safelyCall invokes `function` in recover block
func (s *Server) safelyCall(function reflect.Value, args []reflect.Value) (resp []reflect.Value, e interface{}) {
	defer func() {
		if err := recover(); err != nil {
			if !s.Config.RecoverPanic {
				// go back to panic
				panic(err)
			} else {
				e = err
				resp = nil
				trace := ""
				for i := 1; ; i += 1 {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					}
					if len(trace) > 1 {
						trace = trace + "\n"
					}
					trace = trace + file + ":" + strconv.Itoa(line)
				}
				s.Logger.Error("Handler crashed with error: %s\n%s", err, trace)
			}
		}
	}()
	return function.Call(args), nil
}
開發者ID:Craftserve,項目名稱:web,代碼行數:27,代碼來源:server.go

示例14: Exec

func (this *container) Exec(fv reflect.Value, args []reflect.Type) ([]reflect.Value, error) {
	if vals, err := this.getVals(args); err == nil {
		return fv.Call(vals), nil
	} else {
		return nil, err
	}
}
開發者ID:eynStudio,項目名稱:gobreak,代碼行數:7,代碼來源:di.go

示例15: _functionCall

func _functionCall(fn reflect.Value, inputs ...interface{}) []reflect.Value {
	var args []reflect.Value
	for _, input := range inputs {
		args = append(args, reflect.ValueOf(input))
	}
	return fn.Call(args)
}
開發者ID:llitfkitfk,項目名稱:glow,代碼行數:7,代碼來源:dataset_map.go


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