Skip to content

Commit 2dd6ed3

Browse files
authored
Remove existence check from templates before range (#8181)
Closes #8095 Per testing and documentation the if guard to check if a slice is not empty or not nil is not necessary. Documentation: > {{range pipeline}} T1 {{end}} > The value of the pipeline must be an array, slice, map, iter.Seq, > iter.Seq2, integer or channel. > If the value of the pipeline has length zero, nothing is output; > otherwise, dot is set to the successive elements of the array, > slice, or map and T1 is executed. If the value is a map and the > keys are of basic type with a defined order, the elements will be > visited in sorted key order. https://pkg.go.dev/text/template#hdr-Actions Code testing: ``` <p>begin</p> {{ range $value := .Elements }} <p>{{ $value }}</p> {{ end }} <p>end</p> ``` struct that gets populated with data: ``` type Example struct { Elements []string } ``` executed with the following data: ``` // slice is not empty e := Example{ Elements: []string{"one", "two", "three"} } // slice is empty, but initialised eEmpty := Example { Elements: []string{} } // slice is nil eNil := Example { Elements: nil } ``` The output for these three in order: ``` <p>begin</p> <p>Element 1</p> <p>Element 2</p> <p>Element 3</p> <p>end</p> <p>begin</p> <p>end</p> <p>begin</p> <p>end</p> ```
1 parent 7801be3 commit 2dd6ed3

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

internal/configs/version1/nginx-plus.ingress.tmpl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,8 @@ server {
213213
error_page 504 @grpcerror504;
214214
{{- end}}
215215

216-
{{- if $location.LocationSnippets}}
217216
{{- range $value := $location.LocationSnippets}}
218217
{{$value}}{{end}}
219-
{{- end}}
220218

221219
{{- with $jwt := $location.JWTAuth}}
222220
auth_jwt_key_file {{$jwt.Key}};
@@ -264,10 +262,8 @@ server {
264262
{{- if $.Keepalive}}
265263
proxy_set_header Connection "";{{end}}
266264
{{- end}}
267-
{{- if $location.LocationSnippets}}
268-
{{range $value := $location.LocationSnippets}}
269-
{{$value}}{{end}}
270-
{{- end}}
265+
{{- range $value := $location.LocationSnippets}}
266+
{{$value}}{{- end}}
271267

272268
{{- with $jwt := $location.JWTAuth }}
273269
auth_jwt_key_file {{$jwt.Key}};

internal/configs/version1/nginx-plus.tmpl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ load_module modules/ngx_http_app_protect_module.so;
2222
load_module modules/ngx_http_app_protect_dos_module.so;
2323
{{- end}}
2424
load_module modules/ngx_fips_check_module.so;
25-
{{- if .MainSnippets}}
26-
{{range $value := .MainSnippets}}
27-
{{$value}}{{end}}
28-
{{- end}}
25+
{{- range $value := .MainSnippets}}
26+
{{$value}}{{- end}}
2927

3028
load_module modules/ngx_http_js_module.so;
3129

@@ -42,10 +40,8 @@ http {
4240
js_import /etc/nginx/njs/apikey_auth.js;
4341
js_set $apikey_auth_hash apikey_auth.hash;
4442

45-
{{- if .HTTPSnippets}}
46-
{{range $value := .HTTPSnippets}}
47-
{{$value}}{{end}}
48-
{{- end}}
43+
{{- range $value := .HTTPSnippets}}
44+
{{$value}}{{- end}}
4945

5046
{{if .LogFormat -}}
5147
log_format main {{if .LogFormatEscaping}}escape={{ .LogFormatEscaping }} {{end}}

internal/configs/version1/nginx.ingress.tmpl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ server {
107107
auth_basic_user_file {{ .Secret }};
108108
{{- end }}
109109

110-
{{- if $server.ServerSnippets}}
111110
{{- range $value := $server.ServerSnippets}}
112-
{{$value}}{{end}}
113-
{{- end}}
111+
{{$value}}{{- end}}
114112

115113
{{- range $location := $server.Locations}}
116114
location {{ makeLocationPath $location $.Ingress.Annotations | printf }} {
@@ -137,10 +135,8 @@ server {
137135
error_page 504 @grpcerror504;
138136
{{- end}}
139137

140-
{{- if $location.LocationSnippets}}
141138
{{- range $value := $location.LocationSnippets}}
142-
{{$value}}{{end}}
143-
{{- end}}
139+
{{$value}}{{- end}}
144140

145141
{{- with $location.BasicAuth }}
146142
auth_basic {{ printf "%q" .Realm }};
@@ -183,10 +179,8 @@ server {
183179
{{- if $.Keepalive}}
184180
proxy_set_header Connection "";{{end}}
185181
{{- end}}
186-
{{- if $location.LocationSnippets}}
187-
{{range $value := $location.LocationSnippets}}
188-
{{$value}}{{end}}
189-
{{- end}}
182+
{{- range $value := $location.LocationSnippets}}
183+
{{$value}}{{- end}}
190184
{{- with $location.BasicAuth }}
191185
auth_basic {{ printf "%q" .Realm }};
192186
auth_basic_user_file {{ .Secret }};

internal/configs/version1/nginx.tmpl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ pid /var/lib/nginx/nginx.pid;
1515
load_module modules/ngx_otel_module.so;
1616
{{- end}}
1717

18-
{{- if .MainSnippets}}
19-
{{range $value := .MainSnippets}}
20-
{{$value}}{{end}}
21-
{{- end}}
18+
{{- range $value := .MainSnippets}}
19+
{{$value}}{{- end}}
2220

2321
load_module modules/ngx_http_js_module.so;
2422

@@ -36,10 +34,8 @@ http {
3634
js_import /etc/nginx/njs/apikey_auth.js;
3735
js_set $apikey_auth_hash apikey_auth.hash;
3836

39-
{{- if .HTTPSnippets}}
40-
{{range $value := .HTTPSnippets}}
41-
{{$value}}{{end}}
42-
{{- end}}
37+
{{- range $value := .HTTPSnippets}}
38+
{{$value}}{{- end}}
4339

4440
{{if .LogFormat -}}
4541
log_format main {{if .LogFormatEscaping}}escape={{ .LogFormatEscaping }} {{end}}

0 commit comments

Comments
 (0)