Commit 24285e8d authored by Denis Arh's avatar Denis Arh
Browse files

Support custom parsers for REST params

parent 369e9d91
......@@ -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 }}
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment