【发布时间】:2021-05-03 14:20:22
【问题描述】:
我正在做一个项目,我需要使用一个结构,其中一个字段(下一个)递归地使用相同的结构。原因是我用于解析的 JSON 数据来自一个过程图,其中连接了以下后续步骤。
我必须遵循结构:
type Component struct {
Title string `json:"title"`
Name string `json:"name"`
}
type Inputs struct {
VMname string `json:"VMname"`
ServerRole string `json:"serverRole"`
Os string `json:"os"`
AppSearch string `json:"appsearch"`
Platform string `json:"platform"`
Location string `json:"location"`
Msp string `json:"msp"`
Zone string `json:"zone"`
Networklot string `json:"networklot"`
FrontendSubnet string `json:"frontendSubnet"`
CPU string `json:"cpu"`
Memory string `json:"memory"`
Storage int `json:"storage"`
ApplicationName string `json:"applicationName"`
}
type Components []struct {
Component Component `json:"component"`
Inputs Inputs `json:"inputs"`
InputErrors struct {
} `json:"inputErrors"`
ID string `json:"id"`
Next []interface{} `json:"next"` //this could be a Components []struct or []
}
我通过使用JSON-to-go converter 进入了这个结构,我在其中放入了以下 JSON:
[
{
"component": {
"title": "title1",
"name": "name1"
},
"inputs": {
"serverRole": "server1",
"os": "coolOS",
"platform": "platform",
"location": "New York",
"msp": "msp",
"zone": "red",
"networklot": "kavel",
"frontendSubnet": "0.0.0.0/8080",
"cpu": "2",
"memory": "2",
"VMname": "hola1",
"appsearch": "ann",
"storage": 0,
"applicationName": "app1"
},
"inputErrors": {},
"id": "UwLfQBijlijMqKOCeWdI",
"next": [
{
"component": {
"title": "title1",
"name": "name2"
},
"inputs": {
"serverRole": "server2",
"os": "coolOS2",
"platform": "platform",
"location": "Amsterdam",
"msp": "msp",
"zone": "red",
"networklot": "kavel",
"frontendSubnet": "0.0.0.0/8081",
"cpu": "4",
"memory": "4",
"VMname": "hola2",
"appsearch": "ann",
"storage": 10,
"applicationName": "app2"
},
"inputErrors": {},
"id": "1hzFLlSX6T7B3q8IwcpX",
"next": []
}
]
}
]
问题/挑战在于 JSON 数据中的 "next" 和 Components 结构中对应的 Next []interface{}。您可以看到在 JSON 数据中,第一个 next 包含另一个 JSON 数组,其数据结构与其父级相同,但情况并非总是如此,因为您可以看到第二个 next 有一个空数组。这意味着不再有 childern 并且流程反应它的结束。
简而言之,[]Components struct 中的 Next 字段可以是 []Components struct 或空切片。因此,在查看互联网后,我发现您可以使用 []interface{} 作为 struct[] 或空切片的字段。
接下来我使用json.Unmarshal 将 JSON 数据解析到结构中,我得到以下正确结果:
components: <Components> (length: 1, cap: 4)
[0]: <struct { Component, Inputs, InputErrors struct{}, ID string, Next []interface{} }>
Component: Component
Inputs: Inputs
InputsErrors: struct{}
ID: "dkjfjdkjfd"
Next: []interface {} (length: 1, cap: 4)
其中Next 字段等于以下内容:
Next <[]interface {}> (length: 1, cap: 4)
[0] <interface {}(map[string]interface {})>)
data: <map[string]interface {}> (length: 5)
"component" <interface {}(map[string]interface {})>)
"inputs" <interface {}(map[string]interface {})>)
"inputErrors" <interface {}(map[string]interface {})>)
"id" <interface {}(string)>)
"next" <interface {}([]interface {})>)
data: <[]interface {}> (length: 0, cap: 0) //when the length is 0 then the flow is at it's end.
现在我想执行以下步骤:
- 循环遍历
[]Components struct(在函数中) - 如果是下一个
length == 0,则在循环中检查,如果是,则中断循环,因为我们在最后。 - 在
[]Component struct循环内的 Next 字段上循环。 - 设置一个名为 nextComponents 的新变量。
- 从mapstructure 包中调用
mapstructure.decode(n, &nextComponents)。 - 以
nextComponents为参数递归调用[]Components struct上的循环
小代码示例:
func DeployDiagram(input interface{}) map[int]types.NewSystemResponse {
components := input.(types.Components)
for i, component := range components {
//do some logic
//...
//...
//1. check if next slice == 0 if so then break loop
//2. loop over the next []interface to interface{}
//3. convert interface{} to types.Components
//4. DeployDiagram(types.Components) recursive call
if len(component.Next) == 0 {
break //we are in the last object from the diagram no more next objects.
}
for _, n := range component.Next {
var nextComponents types.Components
mapstructure.Decode(n, &nextComponents) //this end's up being nil?
DeployDiagram(nextComponents)
}
}
return someThing
}
问题是 nextComponents 返回 nil,而它应该返回一个新的 []Components{} 结构。我认为我真的很接近,但我看不出我做错了什么,因为我没有收到任何错误,我不确定这是否是正确的方法。
欢迎任何建议或意见。谢谢你。 :)
【问题讨论】:
-
你为什么首先使用界面?为什么不直接使用 slice?
-
因为 Next 字段可以是两种不同的类型 []struct{} 或 []。这就是 []interface{} 的原因。
-
首先,你为什么要存储 []strunct{},那绝对没用。其次,golang中不存在[]这样的sutch类型。如果 json 数组为空,则它解析成什么类型的切片都没有关系。
-
所以我可以只使用一个切片而不用担心下一个字段是否为空切片?你能给我举个小例子吗?那么切片应该是什么类型呢?因为它可能来自类型组件结构或空?
-
只需将 interface{} 更改为 Components 并尝试解析 json,它应该可以完美运行
标签: go struct interface type-conversion