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)
}

我的排序效果

相关推荐
皓月盈江6 天前
GO使用Gin框架实现模板定义、解析与渲染
linux·golang·go·gin·定义模板·解析模板·渲染模板
码剑客6 天前
Gin+GORM+Redis 项目开发常见性能坑汇总(Go后端避坑指南)
redis·后端·golang·gin
qq_4523962313 天前
第四篇:《索引原理与实战:B-tree、Hash、GIN、GiST、BRIN》
算法·哈希算法·gin
csdn_aspnet22 天前
C# .NetCore Swagger ISchemaFilter 处理枚举、特殊类型
c#·.netcore·swagger
csdn_aspnet23 天前
C# .NetCore Swagger ISchemaFilter Long 转 String 防精度丢失
c#·.netcore·swagger
csdn_aspnet24 天前
C# .NetCore Swagger ISchemaFilter 注入示例值、描述
c#·.netcore·swagger
信也科技布道师24 天前
把 Swagger 变成联调工作台:Swagger Pro 工程实践
swagger
ly768925 天前
PostgreSQL JSONB 在 Spring Boot 中的读写边界:从 JDBC 类型映射到 GIN 索引失效
spring boot·postgresql·gin·jsonb·pgjdbc·gin 索引
谢亮_vipxieliang1 个月前
Go Gin 中间件与参数验证:从入门到实战
中间件·golang·gin
墨鱼老师1 个月前
Go+Gin+Vue 毕设项目:Gin 框架搭建后端基础接口实战
vue.js·golang·go·gin·前后端分离·计算机毕业设计·go 后端