Exemplo n.º 1
0
func gt(arg1, arg2 interface{}) (bool, error) {
	v1 := reflect.ValueOf(arg1)
	v2 := reflect.ValueOf(arg2)
	t1 := v1.Type()
	t2 := v2.Type()
	switch {
	case types.IsInt(t1) && types.IsInt(t2):
		return v1.Int() > v2.Int(), nil
	case types.IsUint(t1) && types.IsUint(t2):
		return v1.Uint() > v2.Uint(), nil
	case types.IsFloat(t1) && types.IsFloat(t2):
		return v1.Float() > v2.Float(), nil
	}
	return false, fmt.Errorf("can't compare %T with %T", arg1, arg2)
}
Exemplo n.º 2
0
// IntValue returns the integer value of the field named
// by name, of def if the field does not exists or does
// not have the correct type.
func IntValue(obj interface{}, name string, def int) int {
	v := getField(obj, name)
	if v.IsValid() {
		if types.IsInt(v.Type()) {
			return int(v.Int())
		}
		if types.IsUint(v.Type()) {
			return int(v.Uint())
		}
	}
	return def
}
Exemplo n.º 3
0
func canParse(typ reflect.Type) bool {
	if types.IsInt(typ) || types.IsUint(typ) || types.IsFloat(typ) {
		return true
	}
	k := typ.Kind()
	if k == reflect.Bool || k == reflect.String {
		return true
	}
	val := reflect.New(typ)
	if _, ok := val.Interface().(input.Parser); ok {
		return true
	}
	return false
}