2

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}, ... ]

しかし、オブジェクト ID が先に必要なため、Mustache.js はそれを解析できません。口ひげは次のようなものを望んでいます:

{
 "repo": [{"Id":1,"FileName":"test :1","Active":false},{"Id":2,"FileName":"test      
:2","Active":false}, ... ]
}
4

1 に答える 1

3

おそらく変更する必要があります

playList gorest.EndPointmethod:"GET" path:"/list/" output:"[]Item"`

メソッドへ playList gorest.EndPoint:"GET" パス:"/list/" 出力:"ItemStore"`

var(
    itemStore []Item
)

type ItemStore struct {
    Items []Item
}

var(
    itemStore ItemStore
)

完全に機能するプログラムは、デバッグがはるかに簡単になります。

于 2012-12-17T13:27:03.800 に答える