Exemplo n.º 1
0
// Bind
func (binder *namedBinder) Bind(ctx *HttpContext) ([]reflect.Value, error) {
	numIn := binder.method.NumIn

	if len(binder.argsMap) != numIn {
		return nil, errors.New("args length doesn not match")
	}

	args := make([]reflect.Value, numIn, numIn)

	for i := 0; i < numIn; i++ {
		in := binder.method.In[i]
		name := binder.argsMap[i]
		str, ok := ctx.RouteData[name]
		if !ok {
			str = ctx.Request.FormValue(name)
		}
		if str == "" {
			args[i] = reflect.Zero(in)
			continue
		}

		if v, err := gotype.Atok(str, in.Kind()); err != nil {
			return nil, err
		} else {
			args[i] = v
		}
	}
	return args, nil
}
Exemplo n.º 2
0
// Bind
func (binder *indexBinder) 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]
		name := "p" + strconv.Itoa(i)

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

		if str == "" {
			args[i] = reflect.Zero(in)
			continue
		}
		if v, err := gotype.Atok(str, in.Kind()); err != nil {
			return nil, err
		} else {
			args[i] = v
		}
	}
	return args, nil
}
Exemplo n.º 3
0
func (n *Node) setMap(v reflect.Value) {

	// fmt.Println("setmap", nameOfNodeType(n.Type), v.Type(), v.Kind())
	// fmt.Println(n.Dump())

	kind := v.Kind()
	if n.Type != NodeHash || kind != reflect.Map {
		return
	}

	typ := v.Type()
	if n.Hash == nil {
		if v.CanSet() {
			v.Set(reflect.Zero(typ))
		}
		return
	}

	if typ.Key() != gotype.TypeString { // only support string key
		return
	}

	if v.IsNil() {
		if v.CanSet() {
			v.Set(reflect.MakeMap(typ))
		} else {
			return
		}
	}

	elemType := typ.Elem()
	elemKind := elemType.Kind()
	simple := gotype.IsSimple(elemKind)

	for name, x := range n.Hash {
		if simple && x.Type == NodeLiteral {
			mapElem, err := gotype.Atok(x.Literal, elemKind)
			if err == nil {
				v.SetMapIndex(reflect.ValueOf(name), mapElem)
			}
		} else {
			var mapElem reflect.Value
			if elemType.Kind() == reflect.Ptr {
				mapElem = reflect.New(elemType.Elem())
			} else {
				mapElem = reflect.New(elemType)
			}
			x.set(mapElem)
			v.SetMapIndex(reflect.ValueOf(name), mapElem)
		}
	}
}