英文:
pass data from one Struct to another Struct and convert to json
问题
你好,我有一个JSON数据,我将其解析为一个机器切片(machines slice)的结构体。现在我想要将每个机器切片结构体中的集群信息和主机名复制/追加到Cluster结构体的[]Cluster中,并尝试在该结构体中填充不同的值。
每个机器记录都有一个关联的serviceName。我希望输出的JSON格式如下所示,即使从机器切片传递时,每个JSON记录都与service_name、required、vars、value相关联,但只传递一次。
当前代码:
https://go.dev/play/p/6zVRIaLIgdN
期望的输出:
{
"cluster_name": "dev",
"services": [
{
"service_name": "serviceA",
"required" : true,
"vars": {
"common_vars": {
"user": "custom-user",
"group": "custom-group"
}
},
"hosts": [
{
"host_name": "host1",
"vars": {
"common_vars" :{
"id": 1
}
}
},
{
"host_name": "host2",
"vars": {
"common_vars":{
"id": 2
}
}
}
]
},
{
"service_name": "serviceB",
"required" : false
.....
}
]
}
当前输出:
其中ServiceName在每个机器名称中重复,我希望它在切片中只有一个service_name,如上述输出所示。
"cluster_name": "dev",
"services": [
{
"service_name": "serviceA",
"required": true,
"hosts": [
{
"host_name": "Machine-1",
"vars": {
"common_vars": {
"id": "1"
},
"custom_listeners": {}
}
}
],
"vars": {
"custom_listeners": {}
}
},
{
"service_name": "serviceA",
"required": true,
"hosts": [
{
"host_name": "Machine-2",
"vars": {
"common_vars": {
"id": "2"
},
"custom_listeners": {}
}
}
],
"vars": {
"custom_listeners": {}
}
}
]
}
希望这能帮到你!如果有任何其他问题,请随时问我。
英文:
Hi I have json data which I Unmarshal to machines slice . Now I am looking to copy/append each cluster information, hostname from machines slice struct to Cluster Struct []Cluster and try to populate different values in that struct.
Each machine record has an associated serviceName . I am Looking for the out json format in the desired output below where service_name ,required, vars, value are only passed once even though they are associated with each json record when passed from machine slice.
current Code :
https://go.dev/play/p/6zVRIaLIgdN
Desired output :
{
"cluster_name": "dev",
"services": [
{
"service_name": "serviceA",
"required" : true,
"vars": {
"common_vars": {
"user": "custom-user",
"group": "custom-group"
}
},
"hosts": [
{
"host_name": "host1",
"vars": {
"common_vars" :{
"id": 1
}
}
},
{
"host_name": "host2",
"vars": {
"common_vars":{
"id": 2
}
}
}
]
},
{
"service_name": "serviceB",
"required" : false
.....
}
}
]
}
Current OutPut:
where the ServiceName is repeated with every machine name , I want it to have service_name once in the slice as above output
"cluster_name": "dev",
"services": [
{
"service_name": "serviceA",
"required": true,
"hosts": [
{
"host_name": "Machine-1",
"vars": {
"common_vars": {
"id": "1"
},
"custom_listeners": {}
}
}
],
"vars": {
"custom_listeners": {}
}
},
{
**"service_name": "serviceA"**,
"required": true,
"hosts": [
{
"host_name": "Machine-2",
"vars": {
"common_vars": {
"id": "2"
},
"custom_listeners": {}
}
}
],
"vars": {
"custom_listeners": {}
}
}
]
}
答案1
得分: 1
你必须实现一些逻辑来合并具有相同名称的服务记录。
可以使用map[<ServiceName>]<Service>
来避免每次迭代服务切片。
m := map[string]*Service{}
for i := range machines {
s, found := m[machines[i].Servicename]
if !found {
s = &Service{ServiceName: machines[i].Servicename, Required: true}
m[machines[i].Servicename] = s
}
s.Hosts = append(s.Hosts, Host{HostName: machines[i].Hostname, Vars: Var{CommonVars: map[string]interface{}{"id": machines[i].ID}}})
}
for _, s := range m {
cService.Services = append(cService.Services, *s)
}
请注意,这只是代码的翻译,我无法执行或测试它。
英文:
You have to implement some logic for merging service records with same name.
map[<ServiceName>]<Service>
could be used to avoid iterating through slice of services every time.
m := map[string]*Service{}
for i := range machines {
s, found := m[machines[i].Servicename]
if !found {
s = &Service{ServiceName: machines[i].Servicename, Required: true}
m[machines[i].Servicename] = s
}
s.Hosts = append(s.Hosts, Host{HostName: machines[i].Hostname, Vars: Var{CommonVars: map[string]interface{}{"id": machines[i].ID}}})
}
for _, s := range m {
cService.Services = append(cService.Services, *s)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论