goa-5-使用积累-版本变更 | KaiQ.Gu|KerwinKoo Blog

JerryXia 发表于 , 阅读 (0)

Goa使用经验

今天更新了Goagen的最新commit。虽然版本还是0.0.1,但功能变化了很多,上一篇记录的内容甚至已经不能那样使用了。

新版本(虽然还是0.0.1)全部使用Structure代替之前的marshaling code,避免每次HTTP method都进行一次结构体渲染的操作,提高了效率。

下面是一些用到了的变更:

自定义response状态

之前版本的返回状态中需要提供指定View-name的参数,比如有一个名为“tiny”的view,返回的代码是:

1
ctx.OK(&res, app.tiny)

现在的返回状态代码是:

1
ctx.OKTiny(&res)

同时在定义返回状态时,也不限与在API实现函数内定义,可以在ResourceAction-Response中进行定义:

1
2
3
4
Response("NotFound", func() {
Status(404)
Media(UserDataError)
})

Reference

Reference对Payload进行健全性检查,通过自定义检查范围来对Http 访问参数进行约束。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var Bottle = Type("bottle", func() {
Attribute("name", func() {
MinLength(3)
})
Attribute("vintage", Integer, func() {
Minimum(1970)
})
Attribute("somethingelse")
})

Declaring the following media type:

var BottleMedia = MediaType("vnd.goa.bottle", func() {
Reference(Bottle)
Attributes(func() {
Attribute("id", Integer)
Attribute("name")
Attribute("vintage")
})
})

代码说明:

  • Reference 可以指定既存类型(如String,Integer),也可以指定自定义的Media。
  • Type中定义的变量名即为Reference指定的类型名。存在Reference时,Attribute指定的成员可以不指定类型(Reference已有指定)。