我是 Go 和 GoRest 的新手,但我对此有疑问。
如何在 gorest EndPoint 语法中为下面描述的输出数据提供 JSON 对象 ID?
我有一个简单的例子:
type HelloService struct {
gorest.RestService `root:"/api" consumes:"application/json" produces:"application/json"`
playList gorest.EndPoint `method:"GET" path:"/list/" output:"[]Item"`
playItem gorest.EndPoint `method:"PUT" path:"/go/{Id:int}" postdata:"Item"`
}
func(serv HelloService) PlayList() []Item{
serv.ResponseBuilder().SetResponseCode(200)
return itemStore
}
type Item struct{
Id int
FileName string
Active bool
}
var(
itemStore []Item
)
生成的 JSON 是:
[{"Id":1,"FileName":"test :1","Active":false},{"Id":2,"FileName":"test :2","Active":false}, ... ]
但是,Mustache.js 无法解析它,因为它首先需要对象 ID。小胡子想要这样的东西:
{
"repo": [{"Id":1,"FileName":"test :1","Active":false},{"Id":2,"FileName":"test
:2","Active":false}, ... ]
}