Skip to content

Commit

Permalink
update: 生成ts的枚举有序显示
Browse files Browse the repository at this point in the history
  • Loading branch information
zodial committed Dec 14, 2023
1 parent 2b9387b commit aec7ff6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions console/commands/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
)

Expand Down Expand Up @@ -284,12 +286,19 @@ func (t *Ts) getTsTypeFromSchema(schema *openapi.Schema, ref string) string {

func (t *Ts) genEnums(enumName string, schema *openapi.Schema) {
str := fmt.Sprintf("export enum %s {\n", enumName)
for k, item := range schema.Properties {
desc := t.clearEmpty(strings.TrimLeft(item.Description, "enum|"))
var keys []int
for k, _ := range schema.Properties {
i, _ := strconv.Atoi(k)
keys = append(keys, i)
}
sort.Ints(keys)
for _, i := range keys {
k := strconv.Itoa(i)
desc := t.clearEmpty(strings.TrimLeft(schema.Properties[k].Description, "enum|"))
if desc != "" {
str += fmt.Sprintf(" // %s\n", desc)
}
str += fmt.Sprintf(" %s = %s,\n", item.Type, k)
str += fmt.Sprintf(" %s = %s,\n", schema.Properties[k].Type, k)
}
str += "}\n"
t.enums[enumName] = str
Expand Down

0 comments on commit aec7ff6

Please sign in to comment.