Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
corteza
corteza-server
Commits
24285e8d
Commit
24285e8d
authored
4 years ago
by
Denis Arh
Browse files
Options
Download
Email Patches
Plain Diff
Support custom parsers for REST params
parent
369e9d91
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
15 deletions
+35
-15
pkg/codegen/assets/rest_request.go.tpl
pkg/codegen/assets/rest_request.go.tpl
+25
-15
pkg/codegen/rest.go
pkg/codegen/rest.go
+10
-0
No files found.
pkg/codegen/assets/rest_request.go.tpl
View file @
24285e8d
...
...
@@ -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 }}
...
...
This diff is collapsed.
Click to expand it.
pkg/codegen/rest.go
View file @
24285e8d
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment