Support custom parsers for REST params
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user