mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 09:50:00 +08:00
feature/goctl-api-swagger (#4780)
This commit is contained in:
123
tools/goctl/api/swagger/options.go
Normal file
123
tools/goctl/api/swagger/options.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package swagger
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
func rangeValueFromOptions(options []string) (minimum *float64, maximum *float64, exclusiveMinimum bool, exclusiveMaximum bool) {
|
||||
if len(options) == 0 {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
for _, option := range options {
|
||||
if strings.HasPrefix(option, rangeFlag) {
|
||||
val := option[6:]
|
||||
start, end := val[0], val[len(val)-1]
|
||||
if start != '[' && start != '(' {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
if end != ']' && end != ')' {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
exclusiveMinimum = start == '('
|
||||
exclusiveMaximum = end == ')'
|
||||
|
||||
content := val[1 : len(val)-1]
|
||||
idxColon := strings.Index(content, ":")
|
||||
if idxColon < 0 {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
var (
|
||||
minStr, maxStr string
|
||||
minVal, maxVal *float64
|
||||
)
|
||||
minStr = util.TrimWhiteSpace(content[:idxColon])
|
||||
if len(val) >= idxColon+1 {
|
||||
maxStr = util.TrimWhiteSpace(content[idxColon+1:])
|
||||
}
|
||||
|
||||
if len(minStr) > 0 {
|
||||
min, err := strconv.ParseFloat(minStr, 64)
|
||||
if err != nil {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
minVal = &min
|
||||
}
|
||||
|
||||
if len(maxStr) > 0 {
|
||||
max, err := strconv.ParseFloat(maxStr, 64)
|
||||
if err != nil {
|
||||
return nil, nil, false, false
|
||||
}
|
||||
maxVal = &max
|
||||
}
|
||||
|
||||
return minVal, maxVal, exclusiveMinimum, exclusiveMaximum
|
||||
}
|
||||
}
|
||||
return nil, nil, false, false
|
||||
}
|
||||
|
||||
func enumsValueFromOptions(options []string) []any {
|
||||
if len(options) == 0 {
|
||||
return []any{}
|
||||
}
|
||||
for _, option := range options {
|
||||
if strings.HasPrefix(option, enumFlag) {
|
||||
var resp = make([]any, 0)
|
||||
val := option[8:]
|
||||
fields := util.FieldsAndTrimSpace(val, func(r rune) bool {
|
||||
return r == '|'
|
||||
})
|
||||
for _, field := range fields {
|
||||
resp = append(resp, field)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
}
|
||||
return []any{}
|
||||
}
|
||||
|
||||
func defValueFromOptions(options []string, apiType spec.Type) any {
|
||||
tp := sampleTypeFromGoType(apiType)
|
||||
return valueFromOptions(options, defFlag, tp)
|
||||
}
|
||||
|
||||
func exampleValueFromOptions(options []string, apiType spec.Type) any {
|
||||
tp := sampleTypeFromGoType(apiType)
|
||||
val := valueFromOptions(options, exampleFlag, tp)
|
||||
if val != nil {
|
||||
return val
|
||||
}
|
||||
return defValueFromOptions(options, apiType)
|
||||
}
|
||||
|
||||
func valueFromOptions(options []string, key string, tp string) any {
|
||||
if len(options) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, option := range options {
|
||||
if strings.HasPrefix(option, key) {
|
||||
s := option[len(key):]
|
||||
switch tp {
|
||||
case "integer":
|
||||
val, _ := strconv.ParseInt(s, 10, 64)
|
||||
return val
|
||||
case "boolean":
|
||||
val, _ := strconv.ParseBool(s)
|
||||
return val
|
||||
case "number":
|
||||
val, _ := strconv.ParseFloat(s, 64)
|
||||
return val
|
||||
case "string":
|
||||
return s
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user