英文:
Create nested struct based on url query parameters
问题
我的目标是从数据库中获取一些经过筛选的记录。筛选是基于一个依赖于另一个结构体的结构体进行的:
type Group struct {
ID primitive.ObjectID
Name string
}
type Role struct {
ID primitive.ObjectID
Name string
Description string
Groups []*group.Group
}
我从URL查询参数创建了一个Role结构体的对象:
var roleWP Role
if r.URL.Query().Has("name") {
name := r.URL.Query().Get("name")
roleWP.Name = name
}
if r.URL.Query().Has("description") {
description := r.URL.Query().Get("description")
roleWP.Description = description
}
if r.URL.Query().Has("groups") {
// groups参数会是什么样子?
}
填充Role结构体的name
和description
字段非常简单。整个URL应该是:myhost/roles?name=rolename&description=roledescription。
但是,如果我想传递Group
结构体的数据,URL会是什么样子呢?是否可以将数据作为JSON对象传递给查询参数?另外,我想提一下,Role
中的groups
字段是一个数组。我理想中的虚拟URL应该是:myhost/roles?name=rolename&description=roledescription&groups={name:groupname1}&groups={name:groupname2}。
英文:
My goal is to get some filtered records from database. Filtration is based on a struct which depends on another struct:
type Group struct {
ID primitive.ObjectID
Name string
}
type Role struct {
ID primitive.ObjectID
Name string
Description string
Groups []*group.Group
}
I create an object of Role struct from URL query parameters:
var roleWP Role
if r.URL.Query().Has("name") {
name := r.URL.Query().Get("name")
roleWP.Name = name
}
if r.URL.Query().Has("description") {
description := r.URL.Query().Get("description")
roleWP.Description = description
}
if r.URL.Query().Has("groups") {
//How would look groups parameter?
}
Filling name
and description
fields of Role
struct is pretty simple. The whole url would be: <br> myhost/roles?name=rolename&description=roledescription<br>
But how would look url if I want to pass data for Group
struct? Is it possible to pass data as a json object in query parameter? Also, I want to mention that groups
field in Role
is an array. My ideal dummy url would look like: myhost/roles?name=rolename&description=roledescription&groups={name:groupname1}&groups={name:groupname2}
答案1
得分: 1
循环遍历组,根据":"进行分割,创建组并添加到切片中:
roleWP := Role{
Name: r.FormValue("name"),
Description: r.FormValue("description"),
}
for _, g := range r.Form["groups"] {
g = strings.TrimPrefix(g, "{")
g = strings.TrimSuffix(g, "}")
i := strings.Index(g, ":")
if i < 0 {
// 处理错误
}
roleWP.Groups = append(roleWP.Groups, &Group{g[:i], g[i+1:]})
}
以下是如何使用JSON替代OP的理想格式:
roleWP := Role{
Name: r.FormValue("name"),
Description: r.FormValue("description"),
}
for _, s := range r.Form["groups"] {
var g Group
err := json.Unmarshal([]byte(s), &g)
if err != nil {
// 处理错误
}
roleWP.Groups = append(roleWP.Groups, &g)
}
英文:
Loop through the groups, split on :, create group and append to slice:
roleWP := Role{
Name: r.FormValue("name"),
Description: r.FormValue("description"),
}
for _, g := range r.Form["groups"] {
g = strings.TrimPrefix(g, "{")
g = strings.TrimSuffix(g, "}")
i := strings.Index(g, ":")
if i < 0 {
// handle error
}
roleWP.Groups = append(roleWP.Groups, &Group{g[:i], g[i+1:]})
}
Here's how to use JSON instead of OP's ideal format:
roleWP := Role{
Name: r.FormValue("name"),
Description: r.FormValue("description"),
}
for _, s := range r.Form["groups"] {
var g Group
err := json.Unmarshal([]byte(s), &v)
if err != nil {
// handle error
}
roleWP.Groups = append(roleWP.Groups, &g)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论