当前位置: 首页>>代码示例>>Golang>>正文


Golang Type.In方法代码示例

本文整理汇总了Golang中reflect.Type.In方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.In方法的具体用法?Golang Type.In怎么用?Golang Type.In使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Type的用法示例。


在下文中一共展示了Type.In方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: makeClosure

func makeClosure(fv reflect.Value, ft reflect.Type) *function {
	fn := &function{
		call: fv,
	}

	var rt = Void
	var at []Type

	if n := ft.NumOut(); n != 0 {
		rt = makeRetType(reflect.New(ft.Out(0)))
	}

	if n := ft.NumIn(); n != 0 {
		at = make([]Type, n)

		for i := 0; i != n; i++ {
			at[i] = makeArgType(reflect.Zero(ft.In(i)))
		}
	}

	fn.Interface = Prepare(rt, at...)

	if err := constructClosure(fn); err != nil {
		panic(err)
	}

	runtime.SetFinalizer(fn, destroyClosure)
	return fn
}
开发者ID:achille-roussel,项目名称:go-ffi,代码行数:29,代码来源:ffi.go

示例2: addImportsForType

// Add all necessary imports for the type, recursing as appropriate.
func addImportsForType(imports importMap, t reflect.Type) {
	// Add any import needed for the type itself.
	addImportForType(imports, t)

	// Handle special cases where recursion is needed.
	switch t.Kind() {
	case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
		addImportsForType(imports, t.Elem())

	case reflect.Func:
		// Input parameters.
		for i := 0; i < t.NumIn(); i++ {
			addImportsForType(imports, t.In(i))
		}

		// Return values.
		for i := 0; i < t.NumOut(); i++ {
			addImportsForType(imports, t.Out(i))
		}

	case reflect.Map:
		addImportsForType(imports, t.Key())
		addImportsForType(imports, t.Elem())
	}
}
开发者ID:BanzaiMan,项目名称:gcsfuse,代码行数:26,代码来源:generate.go

示例3: FormatFuncArguments

func FormatFuncArguments(t reflect.Type) (decl string) {
	decl = "("
	in := make([]string, t.NumIn())

	for i := range in {
		in[i] = ValType(t.In(i))
	}

	decl += strings.Join(in, ",") + ")"

	out := make([]string, t.NumOut())
	if len(out) > 0 {

		for i := range out {
			out[i] = ValType(t.Out(i))
		}

		s := strings.Join(out, ",")

		if len(out) != 1 {
			s = "(" + s + ")"
		}

		decl += " " + s
	}
	return
}
开发者ID:TShadwell,项目名称:fweight,代码行数:27,代码来源:goval.go

示例4: sigMatches

// Note: Methods take the receiver as the first argument, which the want
// signature doesn't include.
func sigMatches(got, want reflect.Type) bool {

	nin := want.NumIn()
	if got.NumIn()-1 != nin {
		return false
	}

	nout := want.NumOut()
	if got.NumOut() != nout {
		return false
	}

	for i := 0; i < nin; i++ {
		if got.In(i+1) != want.In(i) {
			return false
		}
	}

	for i := 0; i < nout; i++ {
		if got.Out(i) != want.Out(i) {
			return false
		}
	}
	return true
}
开发者ID:qinglong1983,项目名称:py,代码行数:27,代码来源:goregister.go

示例5: verifyDeepCopyFunctionSignature

// Verifies whether a deep-copy function has a correct signature.
func verifyDeepCopyFunctionSignature(ft reflect.Type) error {
	if ft.Kind() != reflect.Func {
		return fmt.Errorf("expected func, got: %v", ft)
	}
	if ft.NumIn() != 3 {
		return fmt.Errorf("expected three 'in' params, got $v", ft)
	}
	if ft.NumOut() != 1 {
		return fmt.Errorf("expected one 'out' param, got %v", ft)
	}
	if ft.In(1).Kind() != reflect.Ptr {
		return fmt.Errorf("expected pointer arg for 'in' param 1, got: %v", ft)
	}
	if ft.In(1).Elem() != ft.In(0) {
		return fmt.Errorf("expected 'in' param 0 the same as param 1, got: %v", ft)
	}
	var forClonerType Cloner
	if expected := reflect.TypeOf(&forClonerType); ft.In(2) != expected {
		return fmt.Errorf("expected '%v' arg for 'in' param 2, got: '%v'", expected, ft.In(2))
	}
	var forErrorType error
	// This convolution is necessary, otherwise TypeOf picks up on the fact
	// that forErrorType is nil
	errorType := reflect.TypeOf(&forErrorType).Elem()
	if ft.Out(0) != errorType {
		return fmt.Errorf("expected error return, got: %v", ft)
	}
	return nil
}
开发者ID:qingyuancloud,项目名称:qingyuan,代码行数:30,代码来源:cloner.go

示例6: funcArgsFromType

// t's Kind must be a reflect.Func.
func funcArgsFromType(t reflect.Type) (in []*Parameter, variadic *Parameter, out []*Parameter, err error) {
	nin := t.NumIn()
	if t.IsVariadic() {
		nin--
	}
	var p *Parameter
	for i := 0; i < nin; i++ {
		p, err = parameterFromType(t.In(i))
		if err != nil {
			return
		}
		in = append(in, p)
	}
	if t.IsVariadic() {
		p, err = parameterFromType(t.In(nin).Elem())
		if err != nil {
			return
		}
		variadic = p
	}
	for i := 0; i < t.NumOut(); i++ {
		p, err = parameterFromType(t.Out(i))
		if err != nil {
			return
		}
		out = append(out, p)
	}
	return
}
开发者ID:jloper3,项目名称:amazon-ecs-cli,代码行数:30,代码来源:model.go

示例7: unify

// unify attempts to satisfy a pair of types, where the `param` type is the
// expected type of a function argument and the `input` type is the known
// type of a function argument. The `param` type may be parametric (that is,
// it may contain a type that is convertible to TypeVariable) but the
// `input` type may *not* be parametric.
//
// Any failure to unify the two types results in a panic.
//
// The end result of unification is a type environment: a set of substitutions
// from type variable to a Go type.
func (tp typePair) unify(param, input reflect.Type) error {
	if tyname := tyvarName(input); len(tyname) > 0 {
		return tp.error("Type variables are not allowed in the types of " +
			"arguments.")
	}
	if tyname := tyvarName(param); len(tyname) > 0 {
		if cur, ok := tp.tyenv[tyname]; ok && cur != input {
			return tp.error("Type variable %s expected type '%s' but got '%s'.",
				tyname, cur, input)
		} else if !ok {
			tp.tyenv[tyname] = input
		}
		return nil
	}
	if param.Kind() != input.Kind() {
		return tp.error("Cannot unify different kinds of types '%s' and '%s'.",
			param, input)
	}

	switch param.Kind() {
	case reflect.Array:
		return tp.unify(param.Elem(), input.Elem())
	case reflect.Chan:
		if param.ChanDir() != input.ChanDir() {
			return tp.error("Cannot unify '%s' with '%s' "+
				"(channel directions are different: '%s' != '%s').",
				param, input, param.ChanDir(), input.ChanDir())
		}
		return tp.unify(param.Elem(), input.Elem())
	case reflect.Func:
		if param.NumIn() != input.NumIn() || param.NumOut() != input.NumOut() {
			return tp.error("Cannot unify '%s' with '%s'.", param, input)
		}
		for i := 0; i < param.NumIn(); i++ {
			if err := tp.unify(param.In(i), input.In(i)); err != nil {
				return err
			}
		}
		for i := 0; i < param.NumOut(); i++ {
			if err := tp.unify(param.Out(i), input.Out(i)); err != nil {
				return err
			}
		}
	case reflect.Map:
		if err := tp.unify(param.Key(), input.Key()); err != nil {
			return err
		}
		return tp.unify(param.Elem(), input.Elem())
	case reflect.Ptr:
		return tp.unify(param.Elem(), input.Elem())
	case reflect.Slice:
		return tp.unify(param.Elem(), input.Elem())
	}

	// The only other container types are Interface and Struct.
	// I am unsure about what to do with interfaces. Mind is fuzzy.
	// Structs? I don't think it really makes much sense to use type
	// variables inside of them.
	return nil
}
开发者ID:BurntSushi,项目名称:ty,代码行数:70,代码来源:type-check.go

示例8: Prepare

func (ms *GobMarshaller) Prepare(name string, fn interface{}) (err error) {
	var (
		fT reflect.Type
	)
	// Gob needs to register type before encode/decode
	if fT = reflect.TypeOf(fn); fT.Kind() != reflect.Func {
		err = fmt.Errorf("fn is not a function but %v", fn)
		return
	}

	reg := func(v reflect.Value) (err error) {
		if !v.CanInterface() {
			err = fmt.Errorf("Can't convert to value in input of %v for name:%v", fn, name)
			return
		}

		gob.Register(v.Interface())
		return
	}

	for i := 0; i < fT.NumIn(); i++ {
		// create a zero value of the type of parameters
		if err = reg(reflect.Zero(fT.In(i))); err != nil {
			return
		}
	}

	for i := 0; i < fT.NumOut(); i++ {
		if err = reg(reflect.Zero(fT.Out(i))); err != nil {
			return
		}
	}

	return
}
开发者ID:MogeiWang,项目名称:dingo,代码行数:35,代码来源:marshaller_gob.go

示例9: typeString_FuncOrMethod

func typeString_FuncOrMethod(
	name string,
	t reflect.Type,
	pkgPath string) (s string) {
	// Deal with input types.
	var in []string
	for i := 0; i < t.NumIn(); i++ {
		in = append(in, typeString(t.In(i), pkgPath))
	}

	// And output types.
	var out []string
	for i := 0; i < t.NumOut(); i++ {
		out = append(out, typeString(t.Out(i), pkgPath))
	}

	// Put it all together.
	s = fmt.Sprintf(
		"%s(%s) (%s)",
		name,
		strings.Join(in, ", "),
		strings.Join(out, ", "))

	return
}
开发者ID:chenqi123,项目名称:cq_tkbl_api,代码行数:25,代码来源:type_string.go

示例10: IsEqual

func (p *MethodMetadata) IsEqual(t reflect.Type) bool {
	if t.ConvertibleTo(p.Method.Type) {
		return false
	}

	baseIndex := 0
	if p.Method.Index >= 0 {
		baseIndex = 1
	}

	if t.NumIn()+baseIndex != p.Method.Type.NumIn() {
		return false
	}

	for i := 0; i < p.Method.Type.NumIn()-baseIndex; i++ {
		if p.Method.Type.In(baseIndex+i) != t.In(i) {
			return false
		}
	}

	for i := 0; i < p.Method.Type.NumOut(); i++ {
		if p.Method.Type.Out(baseIndex+i) != t.Out(i) {
			return false
		}
	}

	return true
}
开发者ID:gogap,项目名称:aop,代码行数:28,代码来源:method_metadata.go

示例11: InTypes

func InTypes(fnType reflect.Type) []reflect.Type {
	var types []reflect.Type
	for i := 0; i < fnType.NumIn(); i++ {
		types = append(types, fnType.In(i))
	}
	return types
}
开发者ID:gemrs,项目名称:gem,代码行数:7,代码来源:conversions.go

示例12: checkFunc

func checkFunc(fnType reflect.Type) (bool, error) {
	var state State
	foundState := 0
	nin := fnType.NumIn()
	for i := 0; i < nin; i++ {
		if fnType.In(i) == reflect.TypeOf(state) {
			foundState++
		} else if fnType.In(i) == reflect.TypeOf(&state) {
			return false, fmt.Errorf("raw function can not use `*State' as arg, instead using `State'")
		}
	}

	wrongRawFunc := false
	if foundState > 1 {
		wrongRawFunc = true
	} else if foundState == 1 {
		nout := fnType.NumOut()
		if nin != 1 || nout != 1 {
			wrongRawFunc = true
		} else {
			if fnType.Out(0).Kind() != reflect.Int {
				wrongRawFunc = true
			}
		}
	}

	if wrongRawFunc {
		return false, fmt.Errorf("raw function must be type: `func(State) int'")
	}

	return true, nil
}
开发者ID:hxyxj,项目名称:goinfi,代码行数:32,代码来源:lua.go

示例13: BuildArgs

// BuildArgs creates an args slice than can be used to make a f.Call(args)
func BuildArgs(t crossdock.T, desc string, ft reflect.Type, give []interface{}, initialArgs int) (_ []reflect.Value, ok bool) {
	check := crossdock.Checks(t)
	wantIn := len(give) + initialArgs // +2 for ctx and reqMeta
	if !check.Equal(wantIn, ft.NumIn(), "%v: should accept %d arguments", desc, wantIn) {
		return nil, false
	}

	var args []reflect.Value
	for i, v := range give {
		var val reflect.Value
		vt := ft.In(i + initialArgs)
		if v == nil {
			// nil is an invalid argument to ValueOf. For nil, use the zero
			// value for that argument.
			val = reflect.Zero(vt)
		} else {
			val = reflect.ValueOf(v)
		}
		if !check.Equal(vt, val.Type(), "%v: argument %v type mismatch", desc, i) {
			return nil, false
		}
		args = append(args, val)
	}

	return args, true
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:27,代码来源:behavior.go

示例14: createArguments

// Get all the parameters setup for invocation.
func (method ApiMethod) createArguments(userId UserId, userName UserName, token Token, response http.ResponseWriter, request *http.Request) (bool, []reflect.Value) {
	var handlerType reflect.Type = reflect.TypeOf(method.handler)
	var numParams int = handlerType.NumIn()

	var apiParamIndex = 0
	var paramValues []reflect.Value = make([]reflect.Value, numParams)

	for i := 0; i < numParams; i++ {
		var ParamType reflect.Type = handlerType.In(i)

		// The user id, token, request, and response get handled specially.
		if method.auth && ParamType.String() == "goapi.Token" {
			paramValues[i] = reflect.ValueOf(token)
		} else if method.auth && ParamType.String() == "goapi.UserId" {
			paramValues[i] = reflect.ValueOf(userId)
		} else if method.auth && ParamType.String() == "goapi.UserName" {
			paramValues[i] = reflect.ValueOf(userName)
		} else if ParamType.String() == "*http.Request" {
			paramValues[i] = reflect.ValueOf(request)
		} else if ParamType.String() == "http.ResponseWriter" {
			paramValues[i] = reflect.ValueOf(response)
		} else {
			// Normal param, fetch the next api parameter and pass it along.
			ok, val := method.fetchParam(apiParamIndex, request)
			if !ok {
				return false, []reflect.Value{}
			}

			paramValues[i] = val
			apiParamIndex++
		}
	}

	return true, paramValues
}
开发者ID:eriq-augustine,项目名称:goapi,代码行数:36,代码来源:apimethod.go

示例15: args

func args(f reflect.Type) []reflect.Type {
	in := make([]reflect.Type, f.NumIn())
	for i := 0; i < f.NumIn(); i++ {
		in[i] = f.In(i)
	}
	return in
}
开发者ID:thriftrw,项目名称:thriftrw-go,代码行数:7,代码来源:one.go


注:本文中的reflect.Type.In方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。