如何将一个结构体复制到具有相同字段的嵌套结构体中?

huangapple go评论76阅读模式
英文:

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

huangapple
  • 本文由 发表于 2022年10月6日 14:36:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73969516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定