本文整理匯總了Golang中reflect.Type.IsVariadic方法的典型用法代碼示例。如果您正苦於以下問題:Golang Type.IsVariadic方法的具體用法?Golang Type.IsVariadic怎麽用?Golang Type.IsVariadic使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reflect.Type
的用法示例。
在下文中一共展示了Type.IsVariadic方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newTypeFromFactoryFunction
func newTypeFromFactoryFunction(function interface{}, factoryType reflect.Type, parameters []interface{}) TypeFactory {
if factoryType.NumOut() != 1 {
return newInvalidType(fmt.Errorf("invalid number of return parameters: %d", factoryType.NumOut()))
}
kindOfGeneratedType := factoryType.Out(0).Kind()
if kindOfGeneratedType != reflect.Interface && kindOfGeneratedType != reflect.Ptr {
return newInvalidType(fmt.Errorf("return parameter is no interface or pointer but a %v", kindOfGeneratedType))
}
if factoryType.IsVariadic() {
if factoryType.NumIn() > len(parameters) {
return newInvalidType(fmt.Errorf("invalid number of input parameters for variadic function: got %d but expected at least %d", len(parameters), factoryType.NumIn()))
}
} else {
if factoryType.NumIn() != len(parameters) {
return newInvalidType(fmt.Errorf("invalid number of input parameters: got %d but expected %d", len(parameters), factoryType.NumIn()))
}
}
t := &typeFactory{
factory: reflect.ValueOf(function),
factoryType: factoryType,
}
var err error
t.factoryArguments, err = buildFactoryCallArguments(factoryType, parameters)
if err != nil {
return newInvalidType(err)
}
return t
}
示例2: 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
}
示例3: getInputTypeString
func getInputTypeString(i int, ft reflect.Type) string {
numInputs := ft.NumIn()
if i == numInputs-1 && ft.IsVariadic() {
return "..." + getTypeString(ft.In(i).Elem())
}
return getTypeString(ft.In(i))
}
示例4: safeNumericConvert
// safeNumericConvert converts numeric parameter val from js to the type that the function fn expects if its safe to do so.
// This allows literals (int64) and the general js numeric form (float64) to be passed as parameters to go functions easily.
func safeNumericConvert(fn reflect.Type, i int, val interface{}) reflect.Value {
switch val.(type) {
default:
// Not a supported conversion
return reflect.ValueOf(val)
case float64, int64:
// What type is the func expecting?
var ptype reflect.Type
switch {
case fn.IsVariadic() && fn.NumIn() <= i+1:
// This argument is variadic so use the variadics element type.
ptype = fn.In(fn.NumIn() - 1).Elem()
case fn.NumIn() > i:
ptype = fn.In(i)
}
if f64, ok := val.(float64); ok {
switch ptype.Kind() {
case reflect.Float64:
return reflect.ValueOf(val)
case reflect.Float32:
if reflect.Zero(ptype).OverflowFloat(f64) {
// Not safe to convert
return reflect.ValueOf(val)
}
return reflect.ValueOf(val).Convert(ptype)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i64 := int64(f64)
if float64(i64) != f64 {
// Not safe to convert
return reflect.ValueOf(val)
}
// The float represents an integer
val = i64
default:
// Not a supported conversion
return reflect.ValueOf(val)
}
}
i64 := val.(int64)
switch ptype.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
if !reflect.Zero(ptype).OverflowInt(i64) {
return reflect.ValueOf(val).Convert(ptype)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if i64 > 0 && !reflect.Zero(ptype).OverflowUint(uint64(i64)) {
return reflect.ValueOf(val).Convert(ptype)
}
}
}
// Not a supported conversion
return reflect.ValueOf(val)
}
示例5: extractMethodArgs
func (r concreteRunner) extractMethodArgs(runMethodType reflect.Type, protocolVersion ProtocolVersion, args []interface{}) (methodArgs []reflect.Value, err error) {
numberOfArgs := runMethodType.NumIn()
numberOfReqArgs := numberOfArgs
if runMethodType.IsVariadic() {
numberOfReqArgs--
}
argsOffset := 0
if numberOfArgs > 0 {
firstArgType := runMethodType.In(0)
if firstArgType.Name() == "ProtocolVersion" {
methodArgs = append(methodArgs, reflect.ValueOf(protocolVersion))
numberOfReqArgs--
argsOffset++
}
}
if len(args) < numberOfReqArgs {
err = bosherr.Errorf("Not enough arguments, expected %d, got %d", numberOfReqArgs, len(args))
return
}
for i, argFromPayload := range args {
var rawArgBytes []byte
rawArgBytes, err = json.Marshal(argFromPayload)
if err != nil {
err = bosherr.WrapError(err, "Marshalling action argument")
return
}
argType, typeFound := r.getMethodArgType(runMethodType, i+argsOffset)
if !typeFound {
continue
}
argValuePtr := reflect.New(argType)
err = json.Unmarshal(rawArgBytes, argValuePtr.Interface())
if err != nil {
err = bosherr.WrapError(err, "Unmarshalling action argument")
return
}
methodArgs = append(methodArgs, reflect.Indirect(argValuePtr))
}
return
}
示例6: getMethodArgType
func (r JSONCaller) getMethodArgType(methodType reflect.Type, index int) (argType reflect.Type, found bool) {
numberOfArgs := methodType.NumIn()
switch {
case !methodType.IsVariadic() && index >= numberOfArgs:
return nil, false
case methodType.IsVariadic() && index >= numberOfArgs-1:
sliceType := methodType.In(numberOfArgs - 1)
return sliceType.Elem(), true
default:
return methodType.In(index), true
}
}
示例7: argsMatch
// Funkcja sprawdza zgodnosc typow argumentow. Jesli to konieczne konwertuje
// argumenty do typu interface{}. Jesli to potrzebne, funkcja odpowiednio
// dostosowuje args dla funkcji.
func argsMatch(ft reflect.Type, args []reflect.Value, method int) int {
if ft.NumOut() == 0 {
return RUN_NOT_RET
}
// Liczba arguemntow akceptowanych przez funkcje/metode
num_in := ft.NumIn() - method
// Sprawdzamy zgodnosc liczby argumentow i obecnosc funkcji dotdotdot
var head_args, tail_args []reflect.Value
if ft.IsVariadic() {
num_in--
if len(args) < num_in {
return RUN_WRONG_ARG_NUM
}
head_args = args[0:num_in]
tail_args = args[num_in:]
} else {
if num_in != len(args) {
return RUN_WRONG_ARG_NUM
}
head_args = args
}
// Sprawdzamy zgodnosc typow poczatkowych argumentow funkcji
for kk, av := range head_args {
at := ft.In(kk + method) // Typ argumentu
if !av.Type().AssignableTo(at) {
return RUN_WRONG_ARG_TYP
}
}
if !ft.IsVariadic() {
return RUN_OK
}
// Okreslamy typ argumentów zawartych w dotdotdot
st := ft.In(ft.NumIn() - 1) // zawsze slice
at := st.Elem() // Konkretny typ argumentu dotdotdot
for _, av := range tail_args {
if !av.Type().AssignableTo(at) {
return RUN_WRONG_ARG_TYP
}
}
return RUN_OK
}
示例8: TypeInfo
func (info *Info) TypeInfo(t reflect.Type) *Type {
var name TypeName
if t.Name() != "" {
name = mkName(t.PkgPath(), t.Name())
}
inPackage := t.PkgPath() != ""
if inPackage && name != "" {
if oldt := info.Types[name]; oldt != nil {
if oldt.goType != nil && oldt.goType != t {
panic(fmt.Errorf("duplicate type name with different types %q (%v)", name, t))
}
return oldt
}
}
jt := &Type{
Name: name,
Kind: Kind(t.Kind().String()),
goType: t,
}
if inPackage && name != "" {
// Add the type to the info first to prevent infinite recursion.
info.Types[name] = jt
}
info.addMethods(jt, t)
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
jt.Elem = info.Ref(t.Elem())
case reflect.Map:
jt.Key, jt.Elem = info.Ref(t.Key()), info.Ref(t.Elem())
case reflect.Struct:
info.addFields(jt, t)
case reflect.Func:
jt.Variadic = t.IsVariadic()
jt.In = make([]*Type, t.NumIn())
for i := range jt.In {
jt.In[i] = info.Ref(t.In(i))
}
jt.Out = make([]*Type, t.NumOut())
for i := range jt.Out {
jt.Out[i] = info.Ref(t.Out(i))
}
}
return jt
}
示例9: getlArgs
// getArgs retrieves argument list from args.
// This tries to avoid common panics, and returns the corresponding errors instead.
// panics can still occur but at least the ones related with arguments are mitigated.
func getlArgs(typ reflect.Type, args []interface{}) ([]reflect.Value, error) {
var rst []reflect.Value
for _, v := range args {
val := reflect.ValueOf(v)
if !val.IsValid() { // eliminate zero value panic
return nil, ErrZeroValue
}
rst = append(rst, val)
}
switch {
case typ.IsVariadic():
if typ.NumIn() < len(args)-1 { // eliminate few argumenst panic
return nil, ErrTooFewArgs
}
case !typ.IsVariadic() && typ.NumIn() > len(args): // eliminate too many arguments panic
return nil, ErrTooManyArgs
}
return rst, nil
}
示例10: extractMethodArgs
func (r JSONCaller) extractMethodArgs(runMethodType reflect.Type, args []interface{}) (methodArgs []reflect.Value, err error) {
numberOfArgs := runMethodType.NumIn()
numberOfReqArgs := numberOfArgs
if runMethodType.IsVariadic() {
numberOfReqArgs--
}
if len(args) < numberOfReqArgs {
err = bosherr.Errorf("Not enough arguments, expected %d, got %d", numberOfReqArgs, len(args))
return
}
for i, argFromPayload := range args {
var rawArgBytes []byte
rawArgBytes, err = json.Marshal(argFromPayload)
if err != nil {
err = bosherr.WrapError(err, "Marshalling action argument")
return
}
argType, typeFound := r.getMethodArgType(runMethodType, i)
if !typeFound {
continue
}
argValuePtr := reflect.New(argType)
err = json.Unmarshal(rawArgBytes, argValuePtr.Interface())
if err != nil {
err = bosherr.WrapError(err, "Unmarshalling action argument")
return
}
methodArgs = append(methodArgs, reflect.Indirect(argValuePtr))
}
return
}
示例11: newCtor
func newCtor(t reflect.Type, v reflect.Value) *ctor {
if t.Kind() != reflect.Func || t.IsVariadic() {
return nil
}
numOut := t.NumOut()
if numOut == 0 || numOut > 2 || (numOut == 2 && t.Out(1) != terror) {
return nil
}
outType := t.Out(0)
numIn := t.NumIn()
inTypes := make([]reflect.Type, numIn)
for i := range inTypes {
inTypes[i] = t.In(i)
}
construct := func(in []reflect.Value) (reflect.Value, error) {
for i, arg := range in {
if !arg.IsValid() {
return reflect.Value{},
fmt.Errorf("unable to create arg %d (%s) of %s constructor <%v>",
i, inTypes[i], outType, arg)
}
}
out := v.Call(in)
var err error
if len(out) == 2 && !out[1].IsNil() {
err = out[1].Interface().(error)
}
return out[0], err
}
return &ctor{
funcType: t,
outType: outType,
inTypes: inTypes,
construct: construct,
errChan: make(chan error),
once: &sync.Once{},
}
}
示例12: getFuncIn
func (L *State) getFuncIn(ft rf.Type) []rf.Value {
var in []rf.Value
var i int
for i = 0; i < ft.NumIn()-1; i++ {
in = append(in, *L.getValueByType(ft.In(i).Kind(), i))
}
switch {
case ft.IsVariadic():
ek := ft.In(i).Elem().Kind()
for ; i < int(C.lua_gettop(L.s)); i++ {
switch ek {
case rf.Interface:
in = append(in, *L.getValueByLuaType(i))
default:
in = append(in, *L.getValueByType(ek, i))
}
}
case i < ft.NumIn():
in = append(in, *L.getValueByType(ft.In(i).Kind(), i))
}
return in
}
示例13: buildFactoryCallArguments
func buildFactoryCallArguments(t reflect.Type, allParameters []interface{}) ([]reflect.Value, error) {
actualNumberOfArgs := t.NumIn()
args := make([]reflect.Value, len(allParameters))
for i, argument := range allParameters {
var expectedArgumentType reflect.Type
if t.IsVariadic() && i >= actualNumberOfArgs-1 {
// variadic argument
expectedArgumentType = t.In(actualNumberOfArgs - 1).Elem()
} else {
// regular argument
expectedArgumentType = t.In(i)
}
args[i] = reflect.ValueOf(argument)
if args[i].Kind() != expectedArgumentType.Kind() {
if stringArg, isString := argument.(string); isString && !IsParameterOrTypeReference(stringArg) {
return nil, fmt.Errorf("input argument %d is of type %s but needs to be a %s", i+1, args[i].Kind(), expectedArgumentType.Kind())
}
}
}
return args, nil
}
示例14: TypeFromNative
// TypeFromNative converts a regular Go type into a the corresponding
// interpreter Type.
func TypeFromNative(t reflect.Type) Type {
if et, ok := evalTypes[t]; ok {
return et
}
var nt *NamedType
if t.Name() != "" {
name := t.PkgPath() + "·" + t.Name()
nt = &NamedType{token.NoPos, name, nil, true, make(map[string]Method)}
evalTypes[t] = nt
}
var et Type
switch t.Kind() {
case reflect.Bool:
et = BoolType
case reflect.Float32:
et = Float32Type
case reflect.Float64:
et = Float64Type
case reflect.Int16:
et = Int16Type
case reflect.Int32:
et = Int32Type
case reflect.Int64:
et = Int64Type
case reflect.Int8:
et = Int8Type
case reflect.Int:
et = IntType
case reflect.Uint16:
et = Uint16Type
case reflect.Uint32:
et = Uint32Type
case reflect.Uint64:
et = Uint64Type
case reflect.Uint8:
et = Uint8Type
case reflect.Uint:
et = UintType
case reflect.Uintptr:
et = UintptrType
case reflect.String:
et = StringType
case reflect.Array:
et = NewArrayType(int64(t.Len()), TypeFromNative(t.Elem()))
case reflect.Chan:
log.Panicf("%T not implemented", t)
case reflect.Func:
nin := t.NumIn()
// Variadic functions have DotDotDotType at the end
variadic := t.IsVariadic()
if variadic {
nin--
}
in := make([]Type, nin)
for i := range in {
in[i] = TypeFromNative(t.In(i))
}
out := make([]Type, t.NumOut())
for i := range out {
out[i] = TypeFromNative(t.Out(i))
}
et = NewFuncType(in, variadic, out)
case reflect.Interface:
log.Panicf("%T not implemented", t)
case reflect.Map:
et = NewMapType(TypeFromNative(t.Key()), TypeFromNative(t.Elem()))
case reflect.Ptr:
et = NewPtrType(TypeFromNative(t.Elem()))
case reflect.Slice:
et = NewSliceType(TypeFromNative(t.Elem()))
case reflect.Struct:
n := t.NumField()
fields := make([]StructField, n)
for i := 0; i < n; i++ {
sf := t.Field(i)
// TODO(austin) What to do about private fields?
fields[i].Name = sf.Name
fields[i].Type = TypeFromNative(sf.Type)
fields[i].Anonymous = sf.Anonymous
}
et = NewStructType(fields)
case reflect.UnsafePointer:
log.Panicf("%T not implemented", t)
default:
log.Panicf("unexpected reflect.Type: %T", t)
}
if nt != nil {
if _, ok := et.(*NamedType); !ok {
nt.Complete(et)
et = nt
}
//.........這裏部分代碼省略.........
示例15: call
func (p *Call) call(push bool, is []interface{}) (caller Caller) {
defer func() {
if err := recover(); err != nil {
p.err = errors.New(fmt.Sprint(err))
}
}()
caller = p
if p.failed || p.err != nil || len(is) == 0 {
return
}
var fn reflect.Value
var typ reflect.Type
l := len(is)
for i := 0; i < l; {
fn = reflect.ValueOf(is[i])
if reflect.Func != fn.Kind() {
p.args = append(p.args, is[i])
i++
continue
}
typ = fn.Type()
la := typ.NumIn()
numIn := 0
if typ.IsVariadic() || la != 0 {
for j := 0; typ.IsVariadic() || j < la; j++ {
i++
if i >= l || reflect.Func == reflect.TypeOf(is[i]).Kind() {
break
}
p.args = append(p.args, is[i])
numIn++
}
} else if la == 0 {
i++
}
la = len(p.args)
if la < typ.NumIn() {
p.setFailed(NotEnoughArgs)
break
}
if !typ.IsVariadic() {
numIn = typ.NumIn()
}
args := p.args[la-numIn:]
p.args = p.args[:la-numIn]
in := make([]reflect.Value, len(args))
for i, v := range args {
in[i] = reflect.ValueOf(v)
}
out := fn.Call(in)
la = len(out)
// 總是保存輸出
o := make([]interface{}, la)
for i, v := range out {
o[i] = v.Interface()
}
p.outs = append(p.outs, o)
if la == 0 {
continue
}
fn = out[la-1]
switch fn.Kind().String() {
case "bool":
if !fn.Bool() {
p.setFailed(Failed)
return
}
la--
case "error":
if !fn.IsNil() {
p.setFailed(fn.Interface().(error))
return
}
la--
}
// 輸出壓入備用參數
if !push {
continue
}
for i := 0; i < la; i++ {
p.args = append(p.args, out[i].Interface())
}
}
return
}