Beispiel #1
0
func TemplateVars(typ interface{}) template.VarMap {
	fin := []reflect.Type{reflect.TypeOf(typ)}
	stringOut := []reflect.Type{reflect.TypeOf("")}
	stringFuncTyp := reflect.FuncOf(fin, stringOut, false)
	varFunc := func(in []reflect.Value) []reflect.Value {
		url, _ := updatesURL(in[0])
		return []reflect.Value{reflect.ValueOf(url)}
	}
	boolOut := []reflect.Type{reflect.TypeOf(true)}
	boolFuncTyp := reflect.FuncOf(fin, boolOut, false)
	enabledFunc := func(in []reflect.Value) []reflect.Value {
		url, _ := updatesURL(in[0])
		enabled := url != ""
		return []reflect.Value{reflect.ValueOf(enabled)}
	}
	template.AddFunc(&template.Func{
		Name:   "__gondola_is_live_reload_enabled",
		Fn:     reflect.MakeFunc(boolFuncTyp, enabledFunc).Interface(),
		Traits: template.FuncTraitContext,
	})
	reasonFunc := func(in []reflect.Value) []reflect.Value {
		_, reason := updatesURL(in[0])
		return []reflect.Value{reflect.ValueOf(reason)}
	}
	template.AddFunc(&template.Func{
		Name:   "__gondola_is_live_reload_disabled_reason",
		Fn:     reflect.MakeFunc(stringFuncTyp, reasonFunc).Interface(),
		Traits: template.FuncTraitContext,
	})

	return template.VarMap{
		"BroadcasterWebsocketUrl": reflect.MakeFunc(stringFuncTyp, varFunc).Interface(),
	}
}
Beispiel #2
0
// One wraps the given function to require one less argument, passing in the
// given value instead. The value MUST NOT be nil.
//
// BUG(abg): Variadic functions are not supported.
func One(f interface{}, a interface{}) interface{} {
	if f == nil {
		panic("f is required")
	}
	if a == nil {
		panic("a is required and cannot be nil")
	}

	fType := reflect.TypeOf(f)
	if fType.Kind() != reflect.Func {
		panic(fmt.Sprintf("%v (%v) is not a function", f, fType))
	}

	if fType.IsVariadic() {
		panic(fmt.Sprintf("%v (%v) is variadic", f, fType))
	}

	in := args(fType)
	out := returns(fType)
	if len(in) == 0 {
		panic(fmt.Sprintf("%v (%v) does not accept enough arguments to curry in %v", f, fType, a))
	}

	fVal := reflect.ValueOf(f)
	aVal := reflect.ValueOf(a)
	newFType := reflect.FuncOf(in[1:], out, false)
	return reflect.MakeFunc(newFType, func(args []reflect.Value) []reflect.Value {
		newArgs := make([]reflect.Value, 0, fType.NumIn())
		newArgs = append(newArgs, aVal)
		newArgs = append(newArgs, args...)
		return fVal.Call(newArgs)
	}).Interface()
}
Beispiel #3
0
func (fi *FieldInfo) validateKeyField() error {
	if fi.KeyField == "" {
		return nil
	}
	if fi.ElemType == nil {
		return nil
	}
	if err := validateName(fi.KeyField); err != nil {
		return err
	}
	if fi.ElemType.Kind() != reflect.Struct {
		return errors.Errorf("element type %s not supported; must be struct", fi.ElemType)
	}
	if fi.KeyType.Kind() != reflect.String {
		return errors.Errorf("key type %s not supported; must be string", fi.KeyType)
	}
	elemKeyField, ok := fi.ElemType.FieldByName(fi.KeyField)
	if !ok {
		return errors.Errorf("%s has no field %q", fi.ElemType, fi.KeyField)
	}
	if elemKeyField.Type != fi.KeyType {
		return errors.Errorf("%s.%s is %s; want %s (from %s)",
			fi.ElemType, elemKeyField.Name, elemKeyField.Type, fi.KeyType, fi.Type)
	}
	getFuncType := reflect.FuncOf([]reflect.Type{fi.ElemType}, []reflect.Type{fi.KeyType}, false)
	ptrToElem := reflect.PtrTo(fi.ElemType)
	setFuncType := reflect.FuncOf([]reflect.Type{ptrToElem, fi.KeyType}, nil, false)
	fi.GetKeyFunc = reflect.MakeFunc(getFuncType, func(in []reflect.Value) []reflect.Value {
		return []reflect.Value{in[0].FieldByName(fi.KeyField)}
	})
	fi.SetKeyFunc = reflect.MakeFunc(setFuncType, func(in []reflect.Value) []reflect.Value {
		elem := in[0].Elem()
		if !elem.IsValid() {
			return nil
		}
		in[0].Elem().FieldByName(fi.KeyField).Set(in[1])
		return nil
	})
	return nil
}
Beispiel #4
0
func (q *quangoMatcher) Match(userPredicate interface{}) (successfulMatch bool, matchErr error) {

	typeOfPredicate := reflect.TypeOf(userPredicate)

	if typeOfPredicate.Kind() != reflect.Func {
		return false, fmt.Errorf("Expected a function, not a %s", typeOfPredicate)
	}

	cleanedPredicate := userPredicate

	if typeOfPredicate.NumOut() == 0 {
		predicateValue := reflect.ValueOf(userPredicate)

		predicateWrapper := func(args []reflect.Value) (returnVals []reflect.Value) {
			returnVals = []reflect.Value{reflect.ValueOf(true)}

			defer func() {
				err := recover()
				if err != nil {
					returnVals = []reflect.Value{reflect.ValueOf(false)}
				}
			}()

			predicateValue.Call(args)

			return
		}

		inType := []reflect.Type{}
		for i := 0; i < typeOfPredicate.NumIn(); i++ {
			inType = append(inType, typeOfPredicate.In(i))
		}

		predicateType := reflect.FuncOf(inType, []reflect.Type{reflect.TypeOf(true)}, false)
		cleanedPredicate = reflect.MakeFunc(predicateType, predicateWrapper).Interface()
	}

	err := quick.Check(cleanedPredicate, nil)

	q.counterexample = "False"
	if err != nil {
		cerr, ok := err.(*quick.CheckError)
		if !ok {
			return false, err
		}
		q.counterexample = fmt.Sprintf("%s", cerr)
		return false, nil
	}

	return true, nil
}
Beispiel #5
0
// hand made bullet
func bullet_hm(f interface{}, v interface{}) (ret func(int) error, err error) {
	V_ret := reflect.ValueOf(&ret).Elem()
	T_fd := reflect.TypeOf(int(0))
	V_f := reflect.ValueOf(f)
	V_v := reflect.ValueOf(v)
	T_exp := reflect.FuncOf([]reflect.Type{T_fd, V_v.Type()}, []reflect.Type{reflect.ValueOf(&err).Elem().Type()}, false)

	if V_f.Type() != T_exp {
		return nil, &gatlingerror{T_exp, V_f.Type()}
	}

	V_ret.Set(reflect.MakeFunc(V_ret.Type(), func(v []reflect.Value) []reflect.Value {
		return V_f.Call([]reflect.Value{v[0], V_v})
	}))

	return ret, nil
}
Beispiel #6
0
func alwaysReturnNoError(fn reflect.Value) reflect.Value {
	var (
		ft       = fn.Type()
		in       []reflect.Type
		variadic = ft.IsVariadic()
	)

	for i := 0; i < ft.NumIn(); i++ {
		in = append(in, ft.In(i))
	}

	newFt := reflect.FuncOf(in, []reflect.Type{_typeOfError}, variadic)
	return reflect.MakeFunc(newFt, func(args []reflect.Value) []reflect.Value {
		fn.Call(args)
		return []reflect.Value{reflect.Zero(_typeOfError)}
	})
}
Beispiel #7
0
Datei: wrap.go Projekt: gemrs/gem
func GenerateConstructor(def *py.Class) interface{} {
	init := getInit(def.Pointer)
	if init == nil {
		panic("can't generate constructor: no Init method")
	}

	initType := reflect.TypeOf(init)
	initVal := reflect.ValueOf(init)
	constructedType := reflect.TypeOf(def.Pointer)

	// [1:] to remove the first arg of Init, which should be the Alloc()ated object
	inTypes := InTypes(initType)[1:]
	// return types of the constructor are the object + whatever init returns
	outTypes := OutTypes(initType)
	outTypes = append([]reflect.Type{constructedType}, outTypes...)

	constructorType := reflect.FuncOf(inTypes, outTypes, false)

	genericNew := func(args []reflect.Value) (results []reflect.Value) {
		lock := py.NewLock()
		defer lock.Unlock()

		pyObj, err := def.Alloc(0)
		if err != nil {
			// We assume that a failure to allocate a python object is unrecoverable
			panic(err)
		}

		// prepend the constructed object
		args = append([]reflect.Value{reflect.ValueOf(pyObj)}, args...)
		results = initVal.Call(args)

		// prepend the constructed object
		results = append([]reflect.Value{reflect.ValueOf(pyObj)}, results...)
		return results
	}

	constructorFn := reflect.MakeFunc(constructorType, genericNew)
	return constructorFn.Interface()
}
Beispiel #8
0
func TestFuncOf(t *testing.T) {
	var k = reflect.TypeOf(0)
	var e = reflect.TypeOf("")
	assert.Equal(t, "func(int) string", reflect.FuncOf([]reflect.Type{k}, []reflect.Type{e}, false).String())
}
Beispiel #9
0
func FuncOf(in, out []reflect.Type, dotdotdot bool) reflect.Type {
	return reflect.FuncOf(in, out, dotdotdot)
}