swagger gin 文档接口排序,写了一个小工具,自定义接口排序

起因没找到swagger 自定义接口排序

代码原理就是替换 swag init 生成的docs.go paths 部分 ,取到swagger.json paths 部分排序,正则匹配docs.go paths 部分,然后通过自定义排序,替换paths部分,这个根据自定义的需求来

代码如下

go 复制代码
package main

import (
	"encoding/json"
	"fmt"
	"github.com/tidwall/gjson"
	orderedmap "github.com/wk8/go-ordered-map/v2"
	"log"
	"os"
	"regexp"
	"sort"
	"strconv"
	"strings"
)

// GOOS=darwin GOARCH=arm64 go build -x -v -ldflags "-s -w" -o swagger_sort main.go
type Endpoint struct {
	Post struct {
		Security    []map[string][]interface{} `json:"security"`
		Description string                     `json:"description"`
		Consumes    []string                   `json:"consumes"`
		Produces    []string                   `json:"produces"`
		Tags        []string                   `json:"tags"`
		Summary     string                     `json:"summary"`
		Parameters  []map[string]interface{}   `json:"parameters"`
		Responses   map[string]interface{}     `json:"responses"`
	} `json:"post"`
}

func main() {
	goPath := os.Args[1]
	swaggerPath := os.Args[2]
	if goPath == "" || swaggerPath == "" {
		log.Fatal("文件不存在")
		return
	}

	// 从doc.go中读取内容
	content, err := os.ReadFile(goPath)
	if err != nil {
		log.Fatalf("Error reading docs.go: %v", err)
	}

	newPathsContent := getNewContents(swaggerPath)
	// 正则匹配 "paths" 及其后面的 {} 内容,但保留 "definitions"
	r := regexp.MustCompile(`(?s)"paths": (\{.*?\}),\s*"definitions"`)
	matches := r.FindStringSubmatch(string(content))
	if matches == nil {
		fmt.Println("No match found!")
		return
	}
	//fmt.Println("Matched:", matches[1])


	// Replace matched content in docs.go with newPathsContent
	updatedContent := string(content)
	updatedContent = strings.Replace(updatedContent, matches[1], newPathsContent, 1)

	// Write the updated content back to docs.go
	err = os.WriteFile(goPath, []byte(updatedContent), 0644)
	if err != nil {
		log.Fatalf("Error writing to docs.go: %v", err)
	}

	fmt.Println("Successfully wrote to test.js and updated docs.go!")
}

func extractNumberFromSummary(summary string) int {
	parts := strings.Split(summary, ".")
	if len(parts) > 0 {
		num, err := strconv.Atoi(parts[0])
		if err == nil {
			return num
		}
	}
	return 0
}

func getNewContents(swaggerPath string) string {
	content, err := os.ReadFile(swaggerPath)

	if err != nil {
		log.Fatalf("Error reading swagger.json: %v", err)
	}
	str := gjson.Get(string(content), "paths").String()

	var data map[string]Endpoint
	if err := json.Unmarshal([]byte(str), &data); err != nil {
		panic(err)
	}

	type PathItem struct {
		Path string
		Ep   Endpoint
	}

	var paths []PathItem
	for k, v := range data {
		paths = append(paths, PathItem{k, v})
	}

	sort.Slice(paths, func(i, j int) bool {
		if len(paths[i].Ep.Post.Tags) > 0 {
			if paths[i].Ep.Post.Tags[0] == paths[j].Ep.Post.Tags[0] {
				numI := extractNumberFromSummary(paths[i].Ep.Post.Summary)
				numJ := extractNumberFromSummary(paths[j].Ep.Post.Summary)
				return numI < numJ
			} else if strings.Contains(paths[i].Ep.Post.Tags[0], "测试") {
				return false
			} else {
				return true
			}
		}
		return true

	})

	//bytePath, _ := json.MarshalIndent(paths, "", "    ")
	//fmt.Println(string(bytePath))

	// 创建一个新的 ordered map
	orderedData := orderedmap.New[string, Endpoint]()

	// 从排序后的切片中填充 ordered map
	for _, item := range paths {
		orderedData.Set(item.Path, item.Ep)
	}

	// 编码 ordered map
	sortedJSON, err := json.MarshalIndent(orderedData, "", "    ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(sortedJSON))
	return string(sortedJSON)
}

我的排序效果

相关推荐
crud3 天前
Spring Boot 3 整合 Swagger:打造现代化 API 文档系统(附完整代码 + 高级配置 + 最佳实践)
java·spring boot·swagger
我的golang之路果然有问题4 天前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
fashia6 天前
Java转Go日记(六十):gin其他常用知识
开发语言·后端·golang·go·gin
我的golang之路果然有问题7 天前
ElasticSearch+Gin+Gorm简单示例
大数据·开发语言·后端·elasticsearch·搜索引擎·golang·gin
fashia7 天前
Java转Go日记(五十七):gin 中间件
开发语言·后端·golang·go·gin
夏沫mds7 天前
不动产登记区块链系统(Vue3 + Go + Gin + Hyperledger Fabric)
linux·golang·区块链·gin·fabric
比特森林探险记7 天前
Go Gin框架深度解析:高性能Web开发实践
前端·golang·gin
在成都搬砖的鸭鸭8 天前
【Golang】使用gin框架导出excel和csv文件
golang·excel·gin
三金C_C9 天前
gin 框架
go·gin·后端框架
Chandler249 天前
Go 即时通讯系统:日志模块重构,并从main函数开始
后端·重构·golang·gin