From 24285e8d32ffec2c9ab8aa28137a6f38349fa9d4 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 26 Oct 2020 07:15:59 +0100 Subject: [PATCH] Support custom parsers for REST params --- pkg/codegen/assets/rest_request.go.tpl | 40 ++++++++++++++++---------- pkg/codegen/rest.go | 10 +++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/pkg/codegen/assets/rest_request.go.tpl b/pkg/codegen/assets/rest_request.go.tpl index 5640088d0..ca80621cc 100644 --- a/pkg/codegen/assets/rest_request.go.tpl +++ b/pkg/codegen/assets/rest_request.go.tpl @@ -87,15 +87,7 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er // GET params tmp := req.URL.Query() {{ range $p := $a.Params.Get }} - {{- if not $p.IsSlice }} - if val, ok := tmp["{{ $p.Name }}"]; ok && len(val) > 0 { - r.{{ export $p.Name }}, err = {{ $p.Parser "val[0]" }} - if err != nil { - return err - } - } - {{- end }} - {{- if $p.IsSlice }} + {{- if or $p.IsSlice $p.HasExplicitParser }} if val, ok := tmp["{{ $p.Name }}[]"]; ok { r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} if err != nil { @@ -107,6 +99,13 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er return err } } + {{- else }} + if val, ok := tmp["{{ $p.Name }}"]; ok && len(val) > 0 { + r.{{ export $p.Name }}, err = {{ $p.Parser "val[0]" }} + if err != nil { + return err + } + } {{- end }} {{- end }} } @@ -125,21 +124,32 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er return fmt.Errorf("error processing uploaded file: %w", err) } {{ else }} - {{- if not $p.IsSlice }} - if val, ok := req.Form["{{ $p.Name }}"]; ok && len(val) > 0 { - r.{{ export $p.Name }}, err = {{ $p.Parser "val[0]" }} + {{- if or $p.HasExplicitParser }} + if val, ok := req.Form["{{ $p.Name }}[]"]; ok { + r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} if err != nil { return err } - } - {{- end }} - {{- if $p.IsSlice }} + } else if val, ok := req.Form["{{ $p.Name }}"]; ok { + r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} + if err != nil { + return err + } + } + {{- else if or $p.IsSlice }} //if val, ok := req.Form["{{ $p.Name }}[]"]; ok && len(val) > 0 { // r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} // if err != nil { // return err // } //} + {{- else }} + if val, ok := req.Form["{{ $p.Name }}"]; ok && len(val) > 0 { + r.{{ export $p.Name }}, err = {{ $p.Parser "val[0]" }} + if err != nil { + return err + } + } {{- end }} {{- end }} diff --git a/pkg/codegen/rest.go b/pkg/codegen/rest.go index 5750975e1..14da5041a 100644 --- a/pkg/codegen/rest.go +++ b/pkg/codegen/rest.go @@ -55,6 +55,8 @@ type ( Required bool `yaml:"required"` Title string `yaml:"title"` Origin string + + DefinedParser string `yaml:"parser"` } ) @@ -191,7 +193,15 @@ func (d *restEndpointParamDef) FieldTag() string { return "" } +func (d *restEndpointParamDef) HasExplicitParser() bool { + return d.DefinedParser != "" +} + func (d *restEndpointParamDef) Parser(arg string) string { + if d.HasExplicitParser() { + return fmt.Sprintf("%s(%s)", d.DefinedParser, arg) + } + switch d.Type { case "[]uint64": return fmt.Sprintf("payload.ParseUint64s(%s), nil", arg)