Go Web开发之Revel - 参数绑定

Revel尝试尽可能简单的转换参数到Go的类型.这个转换从string到另一种类型被称为数据绑定.

参数

全部的请求参数被收集到一个Params对象中.它包括如下:

  • URL 路径参数
  • URL 查询参数
  • 表单值 (Multipart or not)
  • 上传文件

定义如下(godoc)

type Params struct {
    url.Values
    Files map[string][]*multipart.FileHeader
}

这个嵌入的 url.Values (godoc)提供了访问简单值的方式,但是开发人员将使用更容易的Revel数据绑定机制来查找非字符串值.

Action参数

参数可以直接作为方法的参数被接收,如下:

func (c AppController) Action(name string, ids []int, user User, img []byte) rev.Result {
    ...
}

在调用action前,Revel要求它的绑定器来对这些名字的参数进行类型转换,如果由于任何原因造成绑定失败,参数将有一个默认值.

绑定器

绑定参数到数据类型,使用Revel的绑定器(godoc),它整合参数对象如下:

func (c SomeController) Action() rev.Result {
    var ids []int = c.Params.Bind("ids[]", reflect.TypeOf([]int{}))
    ...
}

下面的数据类型是被支持的:

  • Ints of all widths
  • Bools
  • Pointers to any supported type
  • Slices of any supported type
  • Structs
  • time.Time for dates and times
  • *os.File, []byte, io.Reader, io.ReadSeeker for file uploads

下面的部分将描述这些类型的语法,你也可以参考源代码

Booleans

字符串的值"true","on"和"1"都被视为true, 其他的绑定值为false.

Slices

绑定Slices有两个被支持的语法:排序和未排序.

排序:

?ids[0]=1
&ids[1]=2
&ids[3]=4

slice中的结果为: []int{1, 2, 0, 4}

未排序:

?ids[]=1
&ids[]=2
&ids[]=3

slice中的结果为: []int{1, 2, 3}

注意:绑定一个slice结构时,只有排序的slice应该被使用.

?user[0].Id=1
&user[0].Name=rob
&user[1].Id=2
&user[1].Name=jenny

Structs

结构使用简单的点符号绑定:

?user.Id=1
&user.Name=rob
&user.Friends[]=2
&user.Friends[]=3
&user.Father.Id=5
&user.Father.Name=Hermes

将绑定下面的结构定义:

type User struct {
    Id int
    Name string
    Friends []int
    Father User
}

注意:为了绑定,属性必须被导出.

Date / Time

内建的SQL标准时间格式是 “2006-01-02”, “2006-01-02 15:04”

应用程序可以添加更多的,使用官方的模式.简单的添加模式来识别TimeFormats变量,如下:

func init() {
    rev.TimeFormats = append(rev.TimeFormats, "01/02/2006")
}

文件上传

文件上传可以被绑定到下面的类型:

  • os.File
  • []byte
  • io.Reader
  • io.ReadSeeker

这是一个有Go multipart包提供的包装器,bytes存放在内存中除非他们超过了一个阀值(默认10MB), 在这种情况下它们被写入临时文件.

注意:绑定一个文件上传到 os.File 需要Revel把它写入到一个临时文件, 它的效率要低于其他的类型.

自定义绑定器

应用程序可以定义它自己的绑定器来利用这个框架.

它只需要实现绑定器接口和注册这个类型它将在哪里被调用:

func myBinder(params Params, name string, typ reflect.Type) reflect.Value {
    ...
}

func init() {
    rev.TypeBinders[reflect.TypeOf(MyType{})] = myBinder
}

至此结束.