Skip to content

Commit fd0fb1c

Browse files
authored
If there are multiple query parameters, save the call to Query() in a variable. (#3506)
Prevents r.URL.Query() from being called multiple times for a handling a single request. Prevents unneeded churn on generated code by only applying that change to code that generates mutliple query parameters.
1 parent b7d9e61 commit fd0fb1c

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

http/codegen/templates/partial/request_elements.go.tpl

+13-8
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@
4242
{{- end }}
4343
{{- end }}
4444

45+
{{- $qpVar := "r.URL.Query()" }}
46+
{{- if gt (len .QueryParams) 1 }}
47+
{{- $qpVar = "qp" }}
48+
qp := r.URL.Query()
49+
{{- end }}
4550
{{- range .QueryParams }}
4651
{{- if and (or (eq .Type.Name "string") (eq .Type.Name "any")) .Required }}
47-
{{ .VarName }} = r.URL.Query().Get("{{ .HTTPName }}")
52+
{{ .VarName }} = {{$qpVar}}.Get("{{ .HTTPName }}")
4853
if {{ .VarName }} == "" {
4954
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
5055
}
5156

5257
{{- else if (or (eq .Type.Name "string") (eq .Type.Name "any")) }}
53-
{{ .VarName }}Raw := r.URL.Query().Get("{{ .HTTPName }}")
58+
{{ .VarName }}Raw := {{$qpVar}}.Get("{{ .HTTPName }}")
5459
if {{ .VarName }}Raw != "" {
5560
{{ .VarName }} = {{ if and (eq .Type.Name "string") .Pointer }}&{{ end }}{{ .VarName }}Raw
5661
}
@@ -60,7 +65,7 @@
6065
{{- end }}
6166

6267
{{- else if .StringSlice }}
63-
{{ .VarName }} = r.URL.Query()["{{ .HTTPName }}"]
68+
{{ .VarName }} = {{$qpVar}}["{{ .HTTPName }}"]
6469
{{- if .Required }}
6570
if {{ .VarName }} == nil {
6671
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
@@ -77,7 +82,7 @@
7782

7883
{{- else if .Slice }}
7984
{
80-
{{ .VarName }}Raw := r.URL.Query()["{{ .HTTPName }}"]
85+
{{ .VarName }}Raw := {{$qpVar}}["{{ .HTTPName }}"]
8186
{{- if .Required }}
8287
if {{ .VarName }}Raw == nil {
8388
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
@@ -100,7 +105,7 @@
100105

101106
{{- else if .Map }}
102107
{
103-
{{ .VarName }}Raw := r.URL.Query()
108+
{{ .VarName }}Raw := {{$qpVar}}
104109
{{- if .Required }}
105110
if len({{ .VarName }}Raw) == 0 {
106111
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
@@ -127,7 +132,7 @@
127132

128133
{{- else if .MapQueryParams }}
129134
{
130-
{{ .VarName }}Raw := r.URL.Query()
135+
{{ .VarName }}Raw := {{$qpVar}}
131136
{{- if .Required }}
132137
if len({{ .VarName }}Raw) == 0 {
133138
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
@@ -182,7 +187,7 @@
182187

183188
{{- else }}{{/* not string, not any, not slice and not map */}}
184189
{
185-
{{ .VarName }}Raw := r.URL.Query().Get("{{ .HTTPName }}")
190+
{{ .VarName }}Raw := {{$qpVar}}.Get("{{ .HTTPName }}")
186191
{{- if .Required }}
187192
if {{ .VarName }}Raw == "" {
188193
err = goa.MergeErrors(err, goa.MissingFieldError("{{ .Name }}", "query string"))
@@ -340,4 +345,4 @@
340345
{{ .Validate }}
341346
{{- end }}
342347
{{- end }}
343-
{{- end }}
348+
{{- end -}}

http/codegen/testdata/payload_decode_functions.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,7 @@ func DecodeMethodCookieStringDefaultValidateRequest(mux goahttp.Muxer, decoder f
36883688
}
36893689
}
36903690
`
3691+
36913692
var PayloadCookiePrimitiveStringDefaultDecodeCode = `// DecodeMethodCookiePrimitiveStringDefaultRequest returns a decoder for
36923693
// requests sent to the ServiceCookiePrimitiveStringDefault
36933694
// MethodCookiePrimitiveStringDefault endpoint.
@@ -5125,8 +5126,9 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
51255126
}
51265127
path = uint(v)
51275128
}
5129+
qp := r.URL.Query()
51285130
{
5129-
optionalRaw := r.URL.Query().Get("optional")
5131+
optionalRaw := qp.Get("optional")
51305132
if optionalRaw != "" {
51315133
v, err2 := strconv.ParseInt(optionalRaw, 10, strconv.IntSize)
51325134
if err2 != nil {
@@ -5137,7 +5139,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
51375139
}
51385140
}
51395141
{
5140-
optionalButRequiredParamRaw := r.URL.Query().Get("optional_but_required_param")
5142+
optionalButRequiredParamRaw := qp.Get("optional_but_required_param")
51415143
if optionalButRequiredParamRaw == "" {
51425144
err = goa.MergeErrors(err, goa.MissingFieldError("optional_but_required_param", "query string"))
51435145
}
@@ -5182,8 +5184,9 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
51825184
int64_ *int64
51835185
err error
51845186
)
5187+
qp := r.URL.Query()
51855188
{
5186-
int_Raw := r.URL.Query().Get("int")
5189+
int_Raw := qp.Get("int")
51875190
if int_Raw != "" {
51885191
v, err2 := strconv.ParseInt(int_Raw, 10, strconv.IntSize)
51895192
if err2 != nil {
@@ -5194,7 +5197,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
51945197
}
51955198
}
51965199
{
5197-
int32_Raw := r.URL.Query().Get("int32")
5200+
int32_Raw := qp.Get("int32")
51985201
if int32_Raw != "" {
51995202
v, err2 := strconv.ParseInt(int32_Raw, 10, 32)
52005203
if err2 != nil {
@@ -5205,7 +5208,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
52055208
}
52065209
}
52075210
{
5208-
int64_Raw := r.URL.Query().Get("int64")
5211+
int64_Raw := qp.Get("int64")
52095212
if int64_Raw != "" {
52105213
v, err2 := strconv.ParseInt(int64_Raw, 10, 64)
52115214
if err2 != nil {
@@ -5234,8 +5237,9 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
52345237
int64_ *int64
52355238
err error
52365239
)
5240+
qp := r.URL.Query()
52375241
{
5238-
int_Raw := r.URL.Query().Get("int")
5242+
int_Raw := qp.Get("int")
52395243
if int_Raw != "" {
52405244
v, err2 := strconv.ParseInt(int_Raw, 10, strconv.IntSize)
52415245
if err2 != nil {
@@ -5251,7 +5255,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
52515255
}
52525256
}
52535257
{
5254-
int32_Raw := r.URL.Query().Get("int32")
5258+
int32_Raw := qp.Get("int32")
52555259
if int32_Raw != "" {
52565260
v, err2 := strconv.ParseInt(int32_Raw, 10, 32)
52575261
if err2 != nil {
@@ -5267,7 +5271,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
52675271
}
52685272
}
52695273
{
5270-
int64_Raw := r.URL.Query().Get("int64")
5274+
int64_Raw := qp.Get("int64")
52715275
if int64_Raw != "" {
52725276
v, err2 := strconv.ParseInt(int64_Raw, 10, 64)
52735277
if err2 != nil {
@@ -5360,6 +5364,7 @@ func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp
53605364
}
53615365
}
53625366
`
5367+
53635368
var QueryMapAliasDecodeCode = `// DecodeMethodARequest returns a decoder for requests sent to the
53645369
// ServiceQueryMapAlias MethodA endpoint.
53655370
func DecodeMethodARequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (any, error) {
@@ -5718,6 +5723,7 @@ func DecodeMethodPathCustomInt32Request(mux goahttp.Muxer, decoder func(*http.Re
57185723
}
57195724
}
57205725
`
5726+
57215727
var PayloadPathCustomInt64DecodeCode = `// DecodeMethodPathCustomInt64Request returns a decoder for requests sent to
57225728
// the ServicePathCustomInt64 MethodPathCustomInt64 endpoint.
57235729
func DecodeMethodPathCustomInt64Request(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (any, error) {
@@ -5745,6 +5751,7 @@ func DecodeMethodPathCustomInt64Request(mux goahttp.Muxer, decoder func(*http.Re
57455751
}
57465752
}
57475753
`
5754+
57485755
var PayloadPathCustomUIntDecodeCode = `// DecodeMethodPathCustomUIntRequest returns a decoder for requests sent to the
57495756
// ServicePathCustomUInt MethodPathCustomUInt endpoint.
57505757
func DecodeMethodPathCustomUIntRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (any, error) {
@@ -5772,6 +5779,7 @@ func DecodeMethodPathCustomUIntRequest(mux goahttp.Muxer, decoder func(*http.Req
57725779
}
57735780
}
57745781
`
5782+
57755783
var PayloadPathCustomUInt32DecodeCode = `// DecodeMethodPathCustomUInt32Request returns a decoder for requests sent to
57765784
// the ServicePathCustomUInt32 MethodPathCustomUInt32 endpoint.
57775785
func DecodeMethodPathCustomUInt32Request(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (any, error) {
@@ -5799,6 +5807,7 @@ func DecodeMethodPathCustomUInt32Request(mux goahttp.Muxer, decoder func(*http.R
57995807
}
58005808
}
58015809
`
5810+
58025811
var PayloadPathCustomUInt64DecodeCode = `// DecodeMethodPathCustomUInt64Request returns a decoder for requests sent to
58035812
// the ServicePathCustomUInt64 MethodPathCustomUInt64 endpoint.
58045813
func DecodeMethodPathCustomUInt64Request(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (any, error) {

0 commit comments

Comments
 (0)