将数据从一个结构体传递到另一个结构体,并转换为 JSON。

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

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[&lt;ServiceName&gt;]&lt;Service&gt; 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 = &amp;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{}{&quot;id&quot;: machines[i].ID}}})
}
for _, s := range m {
	cService.Services = append(cService.Services, *s)
}

huangapple
  • 本文由 发表于 2022年3月7日 05:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/71374386.html
匿名

发表评论

匿名网友

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

确定