英文:
How to copy one struct to nested struct having same fields
问题
我需要使用golang自动创建故事JIRA。为此,我可以将所需的嵌套JSON映射到golang,并且我能够创建故事。现在,我想尝试使用简单的JSON输入,并将其复制到具有相同字段的嵌套结构中。
例如,
我有以下输入JSON:
{
"project": "cdo",
"summary": "sample test story",
"issueType": "Story",
"userStory" : "this is jira",
"assignee": "pradnya.shinde",
"teamOwner" : "TEAM-59",
"productOwner": "alex.anguiano"
}
我已经将上述JSON映射到golang结构中,如下所示:
type InputJson struct {
Project string `json:"project"`
Summary string `json:"summary"`
Issuetype string `json:"issueType"`
UserStory string `json:"userStory"`
Assignee string `json:"assignee"`
TeamOwner string `json:"teamOwner"`
ProductOwner string `json:"productOwner"`
}
我想将此结构复制到另一个嵌套结构中。
嵌套结构如下所示:
type JiraCreateStory struct {
Fields struct {
Project struct {
Key string
}
Summary string
Issuetype struct {
Name string
}
UserStory string
Assignee struct {
Name string
}
ProductOwner struct {
Name string
}
TeamOwner string
}
}
有人可以建议我如何用简单的代码进行复制吗?
英文:
I need to automate creation of story JIRA using golang. For this I can mapped required nested json to golang and I am able to create story also. Now I want to try with simple input as json and copy to nested struct having same fields.
Ex.
I have input json like
{
"project": "cdo",
"summary": "sample test story",
"issueType": "Story",
"userStory" : "this is jira",
"assignee": "pradnya.shinde",
"teamOwner" : "TEAM-59",
"productOwner": "alex.anguiano"
}
I have mapped above json in golang struct like
type InputJson struct {
Project string `json:"project"`
Summary string `json:"summary"`
Issuetype string `json:"issueType"`
UserStory string `json:"userStory"`
Assignee string `json:"assignee"`
TeamOwner string `json:"teamOwner"`
ProductOwner string `json:"productOwner"`
}
I want to copy this struct to another nested struct.
Nested struct is like
type JiraCreateStory struct {
Fields struct {
Project struct {
Key string
}
Summary string
Issuetype struct {
Name string
}
UserStory string
Assignee struct {
Name string
}
ProductOwner struct {
Name string
}
TeamOwner string
}
}
Can anyone suggest how can I copy it with simple code?
答案1
得分: 0
声明所有结构体的类型:
type JiraCreateStory struct {
Fields Fields
}
type Fields struct {
Project Project
Summary string
Issuetype Issuetype
UserStory string
Assignee Assignee
ProductOwner ProductOwner
}
type Project struct {
Key string
}
type Issuetype struct {
Name string
}
type Assignee struct {
Name string
}
type ProductOwner struct {
Name string
}
使用复合字面量创建一个JiraCreateStory
:
x := JiraCreateStory{Fields: Fields{
Project: Project{Key: src.Project},
Summary: src.Summary,
Issuetype: Issuetype{Name: src.Issuetype},
UserStory: src.UserStory,
Assignee: Assignee{Name: src.Assignee},
ProductOwner: ProductOwner{Name: src.ProductOwner},
}}
英文:
Declare types for all structs:
type JiraCreateStory struct {
Fields Fields
}
type Fields struct {
Project Project
Summary string
Issuetype Issuetype
UserStory string
Assignee Assignee
ProductOwner ProductOwner
}
type Project struct { Key string }
type Issuetype struct { Name string }
type Assignee struct { Name string }
type ProductOwner struct { Name string }
Create a JiraCreateStory
using a composite literal:
x := JiraCreateStory{Fields: Fields{
Project: Project{Key: src.Project},
Summary: src.Summary,
Issuetype: Issuetype{Name: src.Issuetype},
UserStory: src.UserStory,
Assignee: Assignee{Name: src.Assignee},
ProductOwner: ProductOwner{Name: src.ProductOwner},
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论