url parameters
This commit is contained in:
parent
9e2eec901e
commit
5dff96f958
@ -1,3 +1,3 @@
|
|||||||
package example
|
package example
|
||||||
|
|
||||||
//go:generate goctl api plugin -plugin goctl-swagger="swagger" -api test.api -dir .
|
//go:generate goctl api plugin -plugin goctl-swagger="swagger" -api user.api -dir .
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
strColon = []byte(":")
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyGenerate(p Plugin) (*swaggerObject, error) {
|
func applyGenerate(p Plugin) (*swaggerObject, error) {
|
||||||
@ -37,12 +43,24 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
|
|||||||
for _, group := range groups {
|
for _, group := range groups {
|
||||||
|
|
||||||
for _, route := range group.Routes {
|
for _, route := range group.Routes {
|
||||||
|
|
||||||
path := route.Path
|
path := route.Path
|
||||||
if strings.Contains(path, "/:") {
|
|
||||||
|
|
||||||
}
|
|
||||||
parameters := swaggerParametersObject{}
|
parameters := swaggerParametersObject{}
|
||||||
|
if countParams(path) > 0 {
|
||||||
|
p := strings.Split(path, "/")
|
||||||
|
for i := range p {
|
||||||
|
part := p[i]
|
||||||
|
if strings.Contains(part, ":") {
|
||||||
|
key := strings.TrimPrefix(p[i], ":")
|
||||||
|
path = strings.Replace(path, fmt.Sprintf(":%s", key), fmt.Sprintf("{%s}", key), 1)
|
||||||
|
parameters = append(parameters, swaggerParameterObject{
|
||||||
|
Name: key,
|
||||||
|
In: "path",
|
||||||
|
Required: true,
|
||||||
|
Type: "string",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name)
|
reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name)
|
||||||
if len(route.RequestType.Name) > 0 {
|
if len(route.RequestType.Name) > 0 {
|
||||||
@ -63,6 +81,7 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
|
|||||||
if !ok {
|
if !ok {
|
||||||
pathItemObject = swaggerPathItemObject{}
|
pathItemObject = swaggerPathItemObject{}
|
||||||
}
|
}
|
||||||
|
|
||||||
desc := "A successful response."
|
desc := "A successful response."
|
||||||
respRef := fmt.Sprintf("#/definitions/%s", route.ResponseType.Name)
|
respRef := fmt.Sprintf("#/definitions/%s", route.ResponseType.Name)
|
||||||
if len(route.ResponseType.Name) < 1 {
|
if len(route.ResponseType.Name) < 1 {
|
||||||
@ -132,6 +151,9 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
|
|||||||
for _, member := range i2.Members {
|
for _, member := range i2.Members {
|
||||||
kv := keyVal{Value: schemaOfField(member)}
|
kv := keyVal{Value: schemaOfField(member)}
|
||||||
kv.Key = member.Name
|
kv.Key = member.Name
|
||||||
|
if tag, err := member.GetPropertyName(); err == nil {
|
||||||
|
kv.Key = tag
|
||||||
|
}
|
||||||
if schema.Properties == nil {
|
if schema.Properties == nil {
|
||||||
schema.Properties = &swaggerSchemaObjectProperties{}
|
schema.Properties = &swaggerSchemaObjectProperties{}
|
||||||
}
|
}
|
||||||
@ -151,7 +173,7 @@ func schemaOfField(member spec.Member) swaggerSchemaObject {
|
|||||||
var props *swaggerSchemaObjectProperties
|
var props *swaggerSchemaObjectProperties
|
||||||
|
|
||||||
switch ft := kind; ft {
|
switch ft := kind; ft {
|
||||||
case reflect.Invalid:
|
case reflect.Invalid: //[]Struct 也有可能是 Struct
|
||||||
// []Struct
|
// []Struct
|
||||||
refTypeName := strings.Replace(member.Type, "[", "", 1)
|
refTypeName := strings.Replace(member.Type, "[", "", 1)
|
||||||
refTypeName = strings.Replace(refTypeName, "]", "", 1)
|
refTypeName = strings.Replace(refTypeName, "]", "", 1)
|
||||||
@ -168,13 +190,29 @@ func schemaOfField(member spec.Member) swaggerSchemaObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch ft := kind; ft {
|
switch ft := kind; ft {
|
||||||
case reflect.Slice, reflect.Invalid:
|
case reflect.Slice:
|
||||||
ret = swaggerSchemaObject{
|
ret = swaggerSchemaObject{
|
||||||
schemaCore: schemaCore{
|
schemaCore: schemaCore{
|
||||||
Type: "array",
|
Type: "array",
|
||||||
Items: (*swaggerItemsObject)(&core),
|
Items: (*swaggerItemsObject)(&core),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
case reflect.Invalid:
|
||||||
|
// 判断是否数组
|
||||||
|
if strings.HasPrefix(member.Type, "[]") {
|
||||||
|
ret = swaggerSchemaObject{
|
||||||
|
schemaCore: schemaCore{
|
||||||
|
Type: "array",
|
||||||
|
Items: (*swaggerItemsObject)(&core),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
ret = swaggerSchemaObject{
|
||||||
|
schemaCore: core,
|
||||||
|
Properties: props,
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
ret = swaggerSchemaObject{
|
ret = swaggerSchemaObject{
|
||||||
schemaCore: core,
|
schemaCore: core,
|
||||||
@ -202,3 +240,20 @@ func primitiveSchema(kind reflect.Kind, t string) (ftype, format string, ok bool
|
|||||||
return "", "", false
|
return "", "", false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringToBytes converts string to byte slice without a memory allocation.
|
||||||
|
func stringToBytes(s string) (b []byte) {
|
||||||
|
return *(*[]byte)(unsafe.Pointer(
|
||||||
|
&struct {
|
||||||
|
string
|
||||||
|
Cap int
|
||||||
|
}{s, len(s)},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
func countParams(path string) uint16 {
|
||||||
|
var n uint16
|
||||||
|
s := stringToBytes(path)
|
||||||
|
n += uint16(bytes.Count(s, strColon))
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module github.com/zeromicro/goctl-swagger
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/grpc-ecosystem/grpc-gateway v1.14.3
|
github.com/grpc-ecosystem/grpc-gateway v1.14.3
|
||||||
github.com/tal-tech/go-zero v1.1.1
|
github.com/tal-tech/go-zero v1.1.1
|
||||||
github.com/urfave/cli/v2 v2.3.0
|
github.com/urfave/cli/v2 v2.3.0
|
||||||
|
2
main.go
2
main.go
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"github.com/zeromicro/goctl-swagger/action"
|
"github.com/zeromicro/goctl-swagger/action"
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
@ -22,6 +21,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Usage = "a plugin of goctl to generate swagger.json"
|
app.Usage = "a plugin of goctl to generate swagger.json"
|
||||||
app.Version = fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH)
|
app.Version = fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user