Ejemplo n.º 1
0
func TestCompare(t *testing.T) {
	t1 := newSimpleType()
	t1.A_string = "string"
	t1.A_string_p = &t1.A_string
	t1.A_interface = int(0)

	t2 := newSimpleType()
	t2.A_string = "string"
	t2.A_string_p = &t2.A_string
	t2.A_interface = float64(0)

	for name1, f1 := range gotype.ToMap(reflect.ValueOf(t1)) {
		for name2, f2 := range gotype.ToMap(reflect.ValueOf(&t2)) {
			if gotype.CanCompareKind(gotype.Underlying(f1).Kind(), gotype.Underlying(f2).Kind()) {
				equal := gotype.Equal(f1.Interface(), f2.Interface())
				if !equal {
					t.Errorf("Test Equal fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}

				greater := gotype.Greater(f1.Interface(), f2.Interface())
				if greater {
					t.Errorf("Test Greater fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}

				less := gotype.Less(f1.Interface(), f2.Interface())
				if less {
					t.Errorf("Test Less fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}
			}
		}
	}
}
Ejemplo n.º 2
0
// Bind
func (binder *structBinder) Bind(ctx *HttpContext) ([]reflect.Value, error) {
	numIn := binder.method.NumIn
	args := make([]reflect.Value, numIn, numIn)

	for i := 0; i < numIn; i++ {
		in := binder.method.In[i]
		args[i] = reflect.Zero(in)

		if in.Kind() == reflect.Struct {
			args[i] = reflect.New(in).Elem()
		} else if in.Kind() == reflect.Ptr && in.Elem().Kind() == reflect.Struct {
			args[i] = reflect.New(in.Elem())
		} else {
			continue
		}

		uType := gotype.UnderlyingType(in)
		uValue := gotype.Underlying(args[i])
		filedNum := uType.NumField()

		for f := 0; f < filedNum; f++ {
			field := uType.Field(f)
			fieldValue := uValue.Field(f)
			fieldType := field.Type

			if !fieldValue.CanSet() {
				continue
			}

			fieldKind := fieldType.Kind()
			name := strings.ToLower(field.Name)

			str, ok := ctx.RouteData[name]
			if !ok {
				str = ctx.Request.FormValue(name)
			}
			if str == "" {
				continue
			}

			if gotype.IsSimple(fieldKind) {
				gotype.Value(fieldValue).Parse(str)
			} else {
				//TODO
			}
		}
	}

	return args, nil
}