From 227ce53b14b0f383f1a9da39dc0f6e487c8593a3 Mon Sep 17 00:00:00 2001 From: fachebot Date: Thu, 24 Mar 2022 17:22:59 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=A4=9A=E4=B8=AAoption=E6=97=B6optional=E4=BC=9A=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98=202.=20=E6=B7=BB=E5=8A=A0o?= =?UTF-8?q?mitempty=E6=94=AF=E6=8C=81=203.=20=E6=B7=BB=E5=8A=A0options?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E8=A7=A3=E6=9E=90=E4=B8=BAenum=204.?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0range=E6=94=AF=E6=8C=81=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E4=B8=BAminimum=E5=92=8Cmaximum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/parser.go | 82 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/generate/parser.go b/generate/parser.go index d6d3d78..472830a 100644 --- a/generate/parser.go +++ b/generate/parser.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "reflect" + "regexp" "strconv" "strings" "unsafe" @@ -19,12 +20,36 @@ const ( defaultOption = "default" stringOption = "string" optionalOption = "optional" + omitemptyOption = "omitempty" optionsOption = "options" rangeOption = "range" optionSeparator = "|" equalToken = "=" ) +func parseRangeOption(option string) (float64, float64, bool) { + const str = "\\[([+-]?\\d+(\\.\\d+)?):([+-]?\\d+(\\.\\d+)?)\\]" + result := regexp.MustCompile(str).FindStringSubmatch(option) + if len(result) != 5 { + return 0, 0, false + } + + min, err := strconv.ParseFloat(result[1], 64) + if err != nil { + return 0, 0, false + } + + max, err := strconv.ParseFloat(result[3], 64) + if err != nil { + return 0, 0, false + } + + if max < min { + return min, min, true + } + return min, max, true +} + func applyGenerate(p *plugin.Plugin, host string, basePath string) (*swaggerObject, error) { title, _ := strconv.Unquote(p.Api.Info.Properties["title"]) version, _ := strconv.Unquote(p.Api.Info.Properties["version"]) @@ -136,16 +161,37 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge sp.Required = true continue } + + required := true for _, option := range tag.Options { + if strings.HasPrefix(option, optionsOption) { + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + sp.Enum = strings.Split(segs[1], optionSeparator) + } + } + + if strings.HasPrefix(option, rangeOption) { + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + min, max, ok := parseRangeOption(segs[1]) + if ok { + sp.Schema.Minimum = min + sp.Schema.Maximum = max + } + } + } + if strings.HasPrefix(option, defaultOption) { segs := strings.Split(option, equalToken) if len(segs) == 2 { sp.Default = segs[1] } - } else if !strings.HasPrefix(option, optionalOption) { - sp.Required = true + } else if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) { + required = false } } + sp.Required = required } if len(member.Comment) > 0 { @@ -280,16 +326,20 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec. } continue } + + required := true for _, option := range tag.Options { - switch { - case !strings.HasPrefix(option, optionalOption): - if !contains(schema.Required, tag.Name) { - schema.Required = append(schema.Required, tag.Name) - } - // case strings.HasPrefix(option, defaultOption): - // case strings.HasPrefix(option, optionsOption): + // case strings.HasPrefix(option, defaultOption): + // case strings.HasPrefix(option, optionsOption): + + if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) { + required = false } } + + if required && !contains(schema.Required, tag.Name) { + schema.Required = append(schema.Required, tag.Name) + } } } @@ -392,7 +442,19 @@ func schemaOfField(member spec.Member) swaggerSchemaObject { ret.Default = segs[1] } case strings.HasPrefix(option, optionsOption): - + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + ret.Enum = strings.Split(segs[1], optionSeparator) + } + case strings.HasPrefix(option, rangeOption): + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + min, max, ok := parseRangeOption(segs[1]) + if ok { + ret.Minimum = min + ret.Maximum = max + } + } } } }