亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Go 中遍歷 Swagger yaml 中的每個元素

在 Go 中遍歷 Swagger yaml 中的每個元素

Go
倚天杖 2022-10-17 15:53:26
我正在嘗試使用 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 mainimport (    "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
查看完整描述

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)

                }

            }

        }

    }

}


查看完整回答
反對 回復 2022-10-17
  • 1 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號