我有以下结构:
type A1 struct {
IdA1 string
NameA1 string
key string
}
type B1 struct {
IdB1 string
NameB1 string
Size string
key string
}
type C1 struct {
IdA1 string
Name string
B1Set B1
}
我需要创建一个 C1 类型的 []结构,它包含 B1 的 B1Set,B1 比 2K 条目多,而 A1 只有 10 个条目,一个真正缓慢且低效的实现是遍历 A1,并询问 B1 键是否等于 A1 键并将结果存储在映射中, 但。。有没有更好的方法来实现这一点?
提前感谢,
添加更多信息:
它们是两个不同的 JSON 文件:
Json1:
[
{
"id": "device1",
"name": "dev1",
"pool": "pool1"
},
{
"id": "device2",
"name": "dev2",
"pool": "pool2"
}
...
]
而另一个Json:
[
{
"name": "port1",
"size": 10,
"pool": "pool1",
"status": "active"
},
{
"name": "port2",
"size": 60,
"pool": "pool1",
"status": "active"
},
{
"name": "port3",
"size": 20,
"pool": "pool2",
"status": "down"
},
{
"name": "port8",
"size": 100,
"pool": "pool2",
"status": "active"
},
{
"name": "port10",
"size": 8000,
"pool": "pool1",
"status": "active"
},
...
]
因此,我需要从这两个文件创建一个新的 JSON 文件,其中包含以下内容:
[
{
"id": "device1",
"name": "dev1",
"pool": "pool1",
"ports": [
{
"name": "port1",
"size": 10,
"pool": "pool1",
"status": "active"
},
{
"name": "port2",
"size": 60,
"pool": "pool1",
"status": "active"
},
{
"name": "port10",
"size": 8000,
"pool": "pool1",
"status": "active"
}
]
},
{
"id": "device2",
"name": "dev2",
"pool": "pool2",
"ports": [
{
"name": "port3",
"size": 20,
"pool": "pool2",
"status": "active"
},
{
"name": "port8",
"size": 100,
"pool": "pool2",
"status": "active"
}
]
}
]
考虑到 Json1 不超过 10-12 个条目,而 Json2 的条目超过 800-1000 个。
迄今:
读取 Json1(设备(和 Json2(端口(并将它们传递到两个 [] 结构中。然后:
results := make(map[string]*models.PortDevices}
portDevs := []models.PortDevices{}
for i := range devices {
results[devices[i].Pool] = &models.PortDevices{}
m := results[devices[i].Pool]
m.Id = devices[i].Id
m.Name = devices[i].Name
m.Pool = devices[i].Pool
m.Status = devices[i].Status
for p := range ports {
if val, ok := results[ports[p].Pool]; ok {
m := val
m.Ports = ports
}
}
portDevs = append(portDevs, *m)
}
devports := []models.PortDevices{}
for _, value := range results {
devports = append(devports, *value)
}
这是您要做的吗? https://play.golang.org/p/AZNzQAwRhN0
这样做是构建一个按池对所有端口进行分组的映射。 然后,它循环遍历我标记clusters
的内容,并通过按值抓取匹配的切片Pool
将Port
切片分配给匹配的Cluster
。
package main
import (
"encoding/json"
"fmt"
)
type Cluster struct {
ID string `json:"id"`
Name string `json:"name"`
Pool string `json:"pool"`
Ports []Port `json:"ports"`
}
type Port struct {
Name string `json:"name"`
Size int `json:"size"`
Pool string `json:"pool"`
Status string `json:"status"`
}
func main() {
var resources []Port
err := json.Unmarshal([]byte(resourceJSON), &resources)
if err != nil {
panic(err)
}
resourcesByPool := make(map[string][]Port)
for _, resource := range resources {
if _, ok := resourcesByPool[resource.Pool]; !ok {
resourcesByPool[resource.Pool] = []Port{}
}
resourcesByPool[resource.Pool] = append(resourcesByPool[resource.Pool], resource)
}
var clusters []Cluster
err = json.Unmarshal([]byte(clusterJSON), &clusters)
if err != nil {
panic(err)
}
for i := 0; i < len(clusters); i++ {
clusters[i].Ports = resourcesByPool[clusters[i].Pool]
}
out, err := json.MarshalIndent(clusters, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
var (
clusterJSON = `[
{
"id": "device1",
"name": "dev1",
"pool": "pool1"
},
{
"id": "device2",
"name": "dev2",
"pool": "pool2"
}
]`
resourceJSON = `[
{
"name": "port1",
"size": 10,
"pool": "pool1",
"status": "active"
},
{
"name": "port2",
"size": 60,
"pool": "pool1",
"status": "active"
},
{
"name": "port3",
"size": 20,
"pool": "pool2",
"status": "down"
},
{
"name": "port8",
"size": 100,
"pool": "pool2",
"status": "active"
},
{
"name": "port10",
"size": 8000,
"pool": "pool1",
"status": "active"
}]`
)