Skip to content

Commit

Permalink
Merge pull request #35 from link-duan/main
Browse files Browse the repository at this point in the history
refator: range map in order
  • Loading branch information
link-duan authored Dec 6, 2022
2 parents f7276e5 + 3000cb2 commit 568e4ba
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 67 deletions.
50 changes: 16 additions & 34 deletions generators/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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(
Expand Down
62 changes: 29 additions & 33 deletions generators/umi/umi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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...)
}
Expand Down
16 changes: 16 additions & 0 deletions utils/map.go
Original file line number Diff line number Diff line change
@@ -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])
}
}

0 comments on commit 568e4ba

Please sign in to comment.