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 ...@@ -87,15 +87,7 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er
// GET params // GET params
tmp := req.URL.Query() tmp := req.URL.Query()
{{ range $p := $a.Params.Get }} {{ range $p := $a.Params.Get }}
{{- if not $p.IsSlice }} {{- if or $p.IsSlice $p.HasExplicitParser }}
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 val, ok := tmp["{{ $p.Name }}[]"]; ok { if val, ok := tmp["{{ $p.Name }}[]"]; ok {
r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} r.{{ export $p.Name }}, err = {{ $p.Parser "val" }}
if err != nil { if err != nil {
...@@ -107,6 +99,13 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er ...@@ -107,6 +99,13 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er
return err 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 }}
{{- end }} {{- end }}
} }
...@@ -125,21 +124,32 @@ func (r *{{ export $.Endpoint.Entrypoint $a.Name }}) Fill(req *http.Request) (er ...@@ -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) return fmt.Errorf("error processing uploaded file: %w", err)
} }
{{ else }} {{ else }}
{{- if not $p.IsSlice }} {{- if or $p.HasExplicitParser }}
if val, ok := req.Form["{{ $p.Name }}"]; ok && len(val) > 0 { if val, ok := req.Form["{{ $p.Name }}[]"]; ok {
r.{{ export $p.Name }}, err = {{ $p.Parser "val[0]" }} r.{{ export $p.Name }}, err = {{ $p.Parser "val" }}
if err != nil { if err != nil {
return err return err
} }
} } else if val, ok := req.Form["{{ $p.Name }}"]; ok {
{{- end }} r.{{ export $p.Name }}, err = {{ $p.Parser "val" }}
{{- if $p.IsSlice }} if err != nil {
return err
}
}
{{- else if or $p.IsSlice }}
//if val, ok := req.Form["{{ $p.Name }}[]"]; ok && len(val) > 0 { //if val, ok := req.Form["{{ $p.Name }}[]"]; ok && len(val) > 0 {
// r.{{ export $p.Name }}, err = {{ $p.Parser "val" }} // r.{{ export $p.Name }}, err = {{ $p.Parser "val" }}
// if err != nil { // if err != nil {
// return err // 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 }}
{{- end }} {{- end }}
......
...@@ -55,6 +55,8 @@ type ( ...@@ -55,6 +55,8 @@ type (
Required bool `yaml:"required"` Required bool `yaml:"required"`
Title string `yaml:"title"` Title string `yaml:"title"`
Origin string Origin string
DefinedParser string `yaml:"parser"`
} }
) )
...@@ -191,7 +193,15 @@ func (d *restEndpointParamDef) FieldTag() string { ...@@ -191,7 +193,15 @@ func (d *restEndpointParamDef) FieldTag() string {
return "" return ""
} }
func (d *restEndpointParamDef) HasExplicitParser() bool {
return d.DefinedParser != ""
}
func (d *restEndpointParamDef) Parser(arg string) string { func (d *restEndpointParamDef) Parser(arg string) string {
if d.HasExplicitParser() {
return fmt.Sprintf("%s(%s)", d.DefinedParser, arg)
}
switch d.Type { switch d.Type {
case "[]uint64": case "[]uint64":
return fmt.Sprintf("payload.ParseUint64s(%s), nil", arg) 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