Beego/Golang - 查询结构值
Beego/Golang - Query Values to Struct
我有一个使用 Beego 框架在 Golang 中构建的 API,我有一个具有多个连接的查询,然后 returns 一个 JSON 结果。
我希望有一种方法可以将每一行转换为具有更好键名的结构,IE 而不是 "Stage__Description" 只是 "Stage" 等...所以我构建了一个包含我想要的键名的 ResultMap 结构,我应该创建一个 Map 吗?
查询:
type ResultMap struct {
Id int
DateAdded time.Time
FirstName string
LastName string
Username string
Stage string
station string
status string
}
//var maps []ResultMap
var maps []orm.Params
num, err := o.QueryTable("test_result_detail").
Filter("Result__Serial", "121994-0001").
Values(&maps, "id", "date_added", "stage__description", "station__station", "status__status", "operator__username", "operator__first_name", "operator__last_name" )
JSON 结果
[
{
"DateAdded": "2016-10-20T00:00:00-05:00",
"Id": 8306105,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "VR1 Test",
"Station__Station": "TS0653",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-20T00:00:00-05:00",
"Id": 8306465,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "QA Lab X-Ray Inspection",
"Station__Station": "LABEQP-0004",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-28T00:00:00-05:00",
"Id": 8547267,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "Capture Customer SN",
"Station__Station": "N/A",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-28T00:00:00-05:00",
"Id": 8547851,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "Final Test",
"Station__Station": "TS0653",
"Status__Status": "PASS"
},
{
"DateAdded": "2017-02-14T00:00:00-06:00",
"Id": 10993864,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "QA Mechanical Final Inspection",
"Station__Station": "VISUAL INSPECTION",
"Status__Status": "PASS"
}
]
您想在您的结构上使用 json 注释,然后使用 json.Unmarshal
将数据放入您的结构中。
这是一个例子:
package main
import (
"encoding/json"
"fmt"
)
func main() {
// The struct you want to store the data in
// Note the json tags that show which json field corresponds to which struct field
type Data struct {
FirstName string `json:"Operator__FirstName"`
LastName string `json:"Operator__LastName"`
}
// Create a slice of these structs since our json is an array of results
var structData []Data
// The json you want to dump into the structs
jsonData := []byte(`[{"Operator__FirstName": "Bob", "Operator__LastName": "Smith"},{"Operator__FirstName": "John", "Operator__LastName": "Adams"}]`)
_ = json.Unmarshal(jsonData, &structData)
fmt.Printf("%+v", structData)
}
在 Go Playground 中:https://play.golang.org/p/Z2P7oUVT9i
您也可以使用 MVC 模式。如果你想使用MVC,你需要创建模型包并将模型注册到数据库。
package models
import (
"time"
"github.com/astaxie/beego/orm"
)
type ResultMap struct {
Id int `orm:"auto"`
DateAdded time.Time `orm:"type(datetime)"`
FirstName string `orm:"size(255)"`
LastName string `orm:"size(255)"`
Username string `orm:"size(255)"`
Stage string `orm:"size(255)"`
station string `orm:"size(255)"`
status string `orm:"size(255)"`
}
func init() {
orm.RegisterModel(
new(ResultMap),
)
}
然后你可以select你的数据作为你想要的控制器中的模型(切片或单个变量)。
func main() {
var mySlice []models.ResultMap
_,err := orm.NewOrm().QueryTable("ResultMap").Filter("FirstName","Bob").All(&mySlice)
if err != nil {
fmt.Println("Database Error: ",err)
return
}
for _, value := range mySlice {
fmt.Println("Firstname: ",value.FirstName)
}
}
你可以查看model definition and ORM usage
我有一个使用 Beego 框架在 Golang 中构建的 API,我有一个具有多个连接的查询,然后 returns 一个 JSON 结果。
我希望有一种方法可以将每一行转换为具有更好键名的结构,IE 而不是 "Stage__Description" 只是 "Stage" 等...所以我构建了一个包含我想要的键名的 ResultMap 结构,我应该创建一个 Map 吗?
查询:
type ResultMap struct {
Id int
DateAdded time.Time
FirstName string
LastName string
Username string
Stage string
station string
status string
}
//var maps []ResultMap
var maps []orm.Params
num, err := o.QueryTable("test_result_detail").
Filter("Result__Serial", "121994-0001").
Values(&maps, "id", "date_added", "stage__description", "station__station", "status__status", "operator__username", "operator__first_name", "operator__last_name" )
JSON 结果
[
{
"DateAdded": "2016-10-20T00:00:00-05:00",
"Id": 8306105,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "VR1 Test",
"Station__Station": "TS0653",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-20T00:00:00-05:00",
"Id": 8306465,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "QA Lab X-Ray Inspection",
"Station__Station": "LABEQP-0004",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-28T00:00:00-05:00",
"Id": 8547267,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "Capture Customer SN",
"Station__Station": "N/A",
"Status__Status": "PASS"
},
{
"DateAdded": "2016-10-28T00:00:00-05:00",
"Id": 8547851,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "Final Test",
"Station__Station": "TS0653",
"Status__Status": "PASS"
},
{
"DateAdded": "2017-02-14T00:00:00-06:00",
"Id": 10993864,
"Operator__FirstName": "Jose",
"Operator__LastName": "Mendez",
"Operator__Username": "3362",
"Stage__Description": "QA Mechanical Final Inspection",
"Station__Station": "VISUAL INSPECTION",
"Status__Status": "PASS"
}
]
您想在您的结构上使用 json 注释,然后使用 json.Unmarshal
将数据放入您的结构中。
这是一个例子:
package main
import (
"encoding/json"
"fmt"
)
func main() {
// The struct you want to store the data in
// Note the json tags that show which json field corresponds to which struct field
type Data struct {
FirstName string `json:"Operator__FirstName"`
LastName string `json:"Operator__LastName"`
}
// Create a slice of these structs since our json is an array of results
var structData []Data
// The json you want to dump into the structs
jsonData := []byte(`[{"Operator__FirstName": "Bob", "Operator__LastName": "Smith"},{"Operator__FirstName": "John", "Operator__LastName": "Adams"}]`)
_ = json.Unmarshal(jsonData, &structData)
fmt.Printf("%+v", structData)
}
在 Go Playground 中:https://play.golang.org/p/Z2P7oUVT9i
您也可以使用 MVC 模式。如果你想使用MVC,你需要创建模型包并将模型注册到数据库。
package models
import (
"time"
"github.com/astaxie/beego/orm"
)
type ResultMap struct {
Id int `orm:"auto"`
DateAdded time.Time `orm:"type(datetime)"`
FirstName string `orm:"size(255)"`
LastName string `orm:"size(255)"`
Username string `orm:"size(255)"`
Stage string `orm:"size(255)"`
station string `orm:"size(255)"`
status string `orm:"size(255)"`
}
func init() {
orm.RegisterModel(
new(ResultMap),
)
}
然后你可以select你的数据作为你想要的控制器中的模型(切片或单个变量)。
func main() {
var mySlice []models.ResultMap
_,err := orm.NewOrm().QueryTable("ResultMap").Filter("FirstName","Bob").All(&mySlice)
if err != nil {
fmt.Println("Database Error: ",err)
return
}
for _, value := range mySlice {
fmt.Println("Firstname: ",value.FirstName)
}
}
你可以查看model definition and ORM usage