From 3000cb2e72af82de3aaa3ca7529fb8831bbcdf89 Mon Sep 17 00:00:00 2001 From: duanlv Date: Tue, 6 Dec 2022 19:14:08 +0800 Subject: [PATCH] refator: range map in order --- generators/ts/ts.go | 50 +++++++++++----------------------- generators/umi/umi.go | 62 ++++++++++++++++++++----------------------- utils/map.go | 16 +++++++++++ 3 files changed, 61 insertions(+), 67 deletions(-) create mode 100644 utils/map.go diff --git a/generators/ts/ts.go b/generators/ts/ts.go index 400a7db..2cd1e7f 100644 --- a/generators/ts/ts.go +++ b/generators/ts/ts.go @@ -2,11 +2,11 @@ package ts import ( _ "embed" - "sort" f "github.com/gotomicro/eapi/formatter" "github.com/gotomicro/eapi/generators" "github.com/gotomicro/eapi/spec" + "github.com/gotomicro/eapi/utils" "github.com/samber/lo" "github.com/spf13/cast" ) @@ -45,23 +45,14 @@ func NewPrinter(schema *spec.T) *Printer { } func (p *Printer) Print() f.Doc { - type Schama struct { - Key string - Value *spec.SchemaRef - } - var schemas []Schama - for key, schema := range p.schema.Components.Schemas { - schemas = append(schemas, Schama{ - Key: key, - Value: schema, - }) - } - sort.Slice(schemas, func(i, j int) bool { return schemas[i].Key < schemas[j].Key }) - var docs []f.Doc - for _, schema := range schemas { - docs = append(docs, p.definition(schema.Value)) - } + utils.RangeMapInOrder( + p.schema.Components.Schemas, + func(a, b string) bool { return a < b }, + func(key string, schema *spec.SchemaRef) { + docs = append(docs, p.definition(schema)) + }, + ) return f.Join(f.Group(f.LineBreak(), f.LineBreak()), docs...) } @@ -125,24 +116,15 @@ func (p *Printer) PrintType(definition *spec.SchemaRef) f.Doc { } func (p *Printer) printInterface(definition *spec.SchemaRef) f.Doc { - type Property struct { - Name string - Property *spec.SchemaRef - } - var properties []Property - for name, schema := range definition.Value.Properties { - properties = append(properties, Property{ - Name: name, - Property: schema, - }) - } - sort.Slice(properties, func(i, j int) bool { return properties[i].Name < properties[j].Name }) - var fields []f.Doc - for _, property := range properties { - required := lo.Contains(definition.Value.Required, property.Name) - fields = append(fields, p.property(property.Name, property.Property, required)) - } + utils.RangeMapInOrder( + definition.Value.Properties, + func(a, b string) bool { return a < b }, + func(name string, property *spec.SchemaRef) { + required := lo.Contains(definition.Value.Required, name) + fields = append(fields, p.property(name, property, required)) + }, + ) if p.TypeFieldsInLine { return f.Group( diff --git a/generators/umi/umi.go b/generators/umi/umi.go index 4bf53bd..76c3f33 100644 --- a/generators/umi/umi.go +++ b/generators/umi/umi.go @@ -4,13 +4,13 @@ package umi import ( "regexp" - "sort" "strings" f "github.com/gotomicro/eapi/formatter" "github.com/gotomicro/eapi/generators" "github.com/gotomicro/eapi/generators/ts" "github.com/gotomicro/eapi/spec" + "github.com/gotomicro/eapi/utils" "github.com/iancoleman/strcase" "github.com/samber/lo" ) @@ -70,38 +70,34 @@ type pathItem struct { func (p *Printer) requests() f.Doc { var docs []f.Doc - var paths []pathItem - for path, item := range p.schema.Paths { - paths = append(paths, pathItem{path: path, PathItem: item}) - } - sort.Slice(paths, func(i, j int) bool { - return paths[i].path < paths[j].path - }) - - for _, item := range paths { - path := item.path - if item.Get != nil { - docs = append(docs, p.request(path, "get", item.Get)) - } - if item.Put != nil { - docs = append(docs, p.request(path, "put", item.Put)) - } - if item.Post != nil { - docs = append(docs, p.request(path, "post", item.Post)) - } - if item.Delete != nil { - docs = append(docs, p.request(path, "delete", item.Delete)) - } - if item.Options != nil { - docs = append(docs, p.request(path, "options", item.Options)) - } - if item.Head != nil { - docs = append(docs, p.request(path, "head", item.Head)) - } - if item.Patch != nil { - docs = append(docs, p.request(path, "patch", item.Patch)) - } - } + + utils.RangeMapInOrder( + p.schema.Paths, + func(a, b string) bool { return a < b }, + func(path string, item *spec.PathItem) { + if item.Get != nil { + docs = append(docs, p.request(path, "get", item.Get)) + } + if item.Put != nil { + docs = append(docs, p.request(path, "put", item.Put)) + } + if item.Post != nil { + docs = append(docs, p.request(path, "post", item.Post)) + } + if item.Delete != nil { + docs = append(docs, p.request(path, "delete", item.Delete)) + } + if item.Options != nil { + docs = append(docs, p.request(path, "options", item.Options)) + } + if item.Head != nil { + docs = append(docs, p.request(path, "head", item.Head)) + } + if item.Patch != nil { + docs = append(docs, p.request(path, "patch", item.Patch)) + } + }, + ) return f.Join(f.Group(f.LineBreak(), f.LineBreak()), docs...) } diff --git a/utils/map.go b/utils/map.go new file mode 100644 index 0000000..7799e8c --- /dev/null +++ b/utils/map.go @@ -0,0 +1,16 @@ +package utils + +import "sort" + +func RangeMapInOrder[K comparable, V any](values map[K]V, sorter func(i, j K) bool, visitor func(K, V)) { + var keys []K + for k, _ := range values { + keys = append(keys, k) + } + + sort.Slice(keys, func(i, j int) bool { return sorter(keys[i], keys[j]) }) + + for _, key := range keys { + visitor(key, values[key]) + } +}