1 回答

TA贡献1807条经验 获得超9个赞
有几个选项,使用https://github.com/getkin/kin-openapi来解析您的 YAML。下面的示例将要求您将 YAML 转换为 JSON。
t := &openapi2.T{}
if err := t.UnmarshalJSON([]byte(jsdata)); err != nil {
panic(err)
}
for path, item := range t.Paths {
for method, op := range item.Operations() {
fmt.Printf("%s %s %s\n", path, method, op.OperationID)
}
}
或者您可以继续使用类型断言和 for 循环。
func main() {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatalf("error: %v", err)
}
for k, v := range m {
if k != "paths" {
continue
}
m2, ok := v.(map[interface{}]interface{})
if !ok {
continue
}
if ok {
for path, x := range m2 {
m3, ok := x.(map[interface{}]interface{})
if !ok {
continue
}
for method, x := range m3 {
m4, ok := x.(map[interface{}]interface{})
if !ok {
continue
}
operationId, ok := m4["operationId"]
if !ok {
continue
}
fmt.Println(path, method, operationId)
}
}
}
}
}
- 1 回答
- 0 关注
- 141 浏览
添加回答
举报