登录
首页 >  Golang >  Go问答

使用 Go 遍历 Swagger yaml 中的所有元素

来源:stackoverflow

时间:2024-03-21 14:03:32 102浏览 收藏

本文探讨了如何使用 Go 遍历 Swagger yaml 文档中的所有元素,以提取字段,如每个路径和方法的 operationid 值。提供了解决方案,包括使用 kin-openapi 库解析 yaml,或使用类型断言和 for 循环。示例代码演示了如何迭代路径和方法,并打印 operationid 值。

问题内容

我试图使用 golang 循环遍历 swagger 文档中的所有路径和方法,并从中获取一些字段,例如:获取每个路径和方法的 operationid 值。 下面是 swagger 文档示例 -

swagger: "2.0"
host: "petstore.swagger.io"
basepath: "/v2"
schemes:
- "https"
- "http"
paths:
  /pet:
    post:
      tags:
      - "pet"
      summary: "add a new pet to the store"
      operationid: "addpet"
    put:
      tags:
      - "pet"
      summary: "update an existing pet"
      operationid: "updatepet"
  /pet/findbystatus:
    get:
      tags:
      - "pet"
      summary: "finds pets by status"
      operationid: "findpetsbystatus"

这个 swagger 文档不是静态的,它会发生变化。所以我没有为此定义一个结构,因为路径和方法不会保持不变。 下面是示例代码 -

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

func main() {

m := make(map[interface{}]interface{})
err := yaml.unmarshal([]byte(data), &m)
if err != nil {
    log.fatalf("error: %v", err)
}
fmt.printf("--- m:\n%v\n\n", m)

for k, v := range m {
    fmt.println(k, ":", v)
    }
}

我可以打印如下地图 -

paths : map[/pet:map[post:map[operationid:addpet summary:add a new pet to the store tags:[pet]] put:map[operationid:updatepet summary:update an existing pet tags:[pet]]] /pet/findbystatus:map[get:map[operationid:findpetsbystatus summary:finds pets by status tags:[pet]]]]

如何打印每个路径和方法,如下所示 -

/pet post addPet
/pet put updatePet
/pet/findByStatus get findPetsByStatus

正确答案


有几个选项,使用 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)
                }
            }
        }
    }
}

今天关于《使用 Go 遍历 Swagger yaml 中的所有元素》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>