Skip to content

Commit 0c3d74a

Browse files
authored
Fix function calls with inconsistent argument breaking (#932)
Problem: - The code doesn't follow the guideline: when breaking up a call, choose to break after each parameter argument. See https://github.com/nginxinc/nginx-kubernetes-gateway/blob/0ff13226b16cfec44f509542b25bc3f2fcf610cc/docs/developer/go-style-guide.md#consistent-line-breaks - THe guidelines don't have an example of a function call Solution: - Fix the problems in the code, except for ginkgo helpers in tests, because of their readability. - Add an example of a functional call to the guidelines
1 parent 29fdf4a commit 0c3d74a

File tree

11 files changed

+241
-92
lines changed

11 files changed

+241
-92
lines changed

cmd/gateway/commands.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func createStaticModeCommand() *cobra.Command {
124124
Short: "Configure NGINX in the scope of a single Gateway resource",
125125
RunE: func(cmd *cobra.Command, args []string) error {
126126
logger := zap.New()
127-
logger.Info("Starting NGINX Kubernetes Gateway in static mode",
127+
logger.Info(
128+
"Starting NGINX Kubernetes Gateway in static mode",
128129
"version", version,
129130
"commit", commit,
130131
"date", date,
@@ -184,7 +185,8 @@ func createProvisionerModeCommand() *cobra.Command {
184185
Hidden: true,
185186
RunE: func(cmd *cobra.Command, args []string) error {
186187
logger := zap.New()
187-
logger.Info("Starting NGINX Kubernetes Gateway Provisioner",
188+
logger.Info(
189+
"Starting NGINX Kubernetes Gateway Provisioner",
188190
"version", version,
189191
"commit", commit,
190192
"date", date,

docs/developer/go-style-guide.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,27 @@ signaller <- false // is this a signal? is this an error?
4545

4646
### Consistent Line Breaks
4747

48-
When breaking up a long function definition, call, or struct initialization, choose to break after each parameter,
48+
When breaking up a (1) long function definition, (2) call, or (3) struct initialization, choose to break after each parameter,
4949
argument, or field.
5050

5151
DO:
5252

5353
```go
54+
// 1
5455
func longFunctionDefinition(
5556
paramX int,
5657
paramY string,
5758
paramZ bool,
5859
) (string, error){}
5960

60-
// and
61+
// 2
62+
callWithManyArguments(
63+
arg1,
64+
arg2,
65+
arg3,
66+
)
6167

68+
// 3
6269
s := myStruct{
6370
field1: 1,
6471
field2: 2,
@@ -69,6 +76,7 @@ s := myStruct{
6976
DO NOT:
7077

7178
```go
79+
// 1
7280
func longFunctionDefinition(paramX int, paramY string,
7381
paramZ bool,
7482
) (string, error){}
@@ -80,12 +88,25 @@ func longFunctionDefinition(
8088
paramZ bool,
8189
) (string, error){}
8290

83-
// or
91+
// 2
92+
callWithManyArguments(arg1, arg2,
93+
arg3)
94+
95+
// 3
8496
s := myStruct{field1: 1, field2: 2,
8597
field3: 3}
8698

8799
```
88100

101+
> **Exception**: Calls to ginkgo helper functions like below in tests are OK (because of the readability):
102+
>
103+
>```go
104+
>DescribeTable("Edge cases for events",
105+
> func(e interface{}) {
106+
> . . .
107+
>)
108+
>```
109+
89110
When constructing structs pass members during initialization.
90111
91112
Example:

internal/framework/status/updater.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ func (upd *updaterImpl) update(
133133
err := upd.cfg.Client.Get(ctx, nsname, obj)
134134
if err != nil {
135135
if !apierrors.IsNotFound(err) {
136-
upd.cfg.Logger.Error(err, "Failed to get the recent version the resource when updating status",
136+
upd.cfg.Logger.Error(
137+
err,
138+
"Failed to get the recent version the resource when updating status",
137139
"namespace", nsname.Namespace,
138140
"name", nsname.Name,
139141
"kind", obj.GetObjectKind().GroupVersionKind().Kind)
@@ -145,7 +147,9 @@ func (upd *updaterImpl) update(
145147

146148
err = upd.cfg.Client.Status().Update(ctx, obj)
147149
if err != nil {
148-
upd.cfg.Logger.Error(err, "Failed to update status",
150+
upd.cfg.Logger.Error(
151+
err,
152+
"Failed to update status",
149153
"namespace", nsname.Namespace,
150154
"name", nsname.Name,
151155
"kind", obj.GetObjectKind().GroupVersionKind().Kind)

internal/mode/provisioner/handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ func (h *eventHandler) ensureDeploymentsMatchGateways(ctx context.Context) {
117117

118118
h.provisions[nsname] = deployment
119119

120-
h.logger.Info("Created deployment",
120+
h.logger.Info(
121+
"Created deployment",
121122
"deployment", client.ObjectKeyFromObject(deployment),
122123
"gateway", nsname,
123124
)
@@ -135,7 +136,8 @@ func (h *eventHandler) ensureDeploymentsMatchGateways(ctx context.Context) {
135136

136137
delete(h.provisions, nsname)
137138

138-
h.logger.Info("Deleted deployment",
139+
h.logger.Info(
140+
"Deleted deployment",
139141
"deployment", client.ObjectKeyFromObject(deployment),
140142
"gateway", nsname,
141143
)

internal/mode/static/nginx/config/validation/common.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ var escapedStringsNoVarExpansionFmtRegexp = regexp.MustCompile("^" + escapedStri
4141
// If the value is invalid, the function returns an error that includes the specified examples of valid values.
4242
func validateEscapedStringNoVarExpansion(value string, examples []string) error {
4343
if !escapedStringsNoVarExpansionFmtRegexp.MatchString(value) {
44-
msg := k8svalidation.RegexError(escapedStringsNoVarExpansionErrMsg, escapedStringsNoVarExpansionFmt,
45-
examples...)
44+
msg := k8svalidation.RegexError(
45+
escapedStringsNoVarExpansionErrMsg,
46+
escapedStringsNoVarExpansionFmt,
47+
examples...,
48+
)
4649
return errors.New(msg)
4750
}
4851
return nil

internal/mode/static/nginx/config/validation/common_test.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,56 @@ import (
88
func TestValidateEscapedString(t *testing.T) {
99
validator := func(value string) error { return validateEscapedString(value, []string{"example"}) }
1010

11-
testValidValuesForSimpleValidator(t, validator,
11+
testValidValuesForSimpleValidator(
12+
t,
13+
validator,
1214
`test`,
1315
`test test`,
1416
`\"`,
15-
`\\`)
16-
testInvalidValuesForSimpleValidator(t, validator,
17+
`\\`,
18+
)
19+
testInvalidValuesForSimpleValidator(
20+
t,
21+
validator,
1722
`\`,
18-
`test"test`)
23+
`test"test`,
24+
)
1925
}
2026

2127
func TestValidateEscapedStringNoVarExpansion(t *testing.T) {
2228
validator := func(value string) error { return validateEscapedStringNoVarExpansion(value, []string{"example"}) }
2329

24-
testValidValuesForSimpleValidator(t, validator,
30+
testValidValuesForSimpleValidator(
31+
t,
32+
validator,
2533
`test`,
2634
`test test`,
2735
`\"`,
28-
`\\`)
29-
testInvalidValuesForSimpleValidator(t, validator,
36+
`\\`,
37+
)
38+
testInvalidValuesForSimpleValidator(
39+
t,
40+
validator,
3041
`\`,
3142
`test"test`,
32-
`$test`)
43+
`$test`,
44+
)
3345
}
3446

3547
func TestValidateValidHeaderName(t *testing.T) {
3648
validator := func(value string) error { return validateHeaderName(value) }
3749

38-
testValidValuesForSimpleValidator(t, validator,
50+
testValidValuesForSimpleValidator(
51+
t,
52+
validator,
3953
`Content-Encoding`,
4054
`X-Forwarded-For`,
4155
// max supported length is 256, generate string with 16*16 chars (256)
42-
strings.Repeat("very-long-header", 16))
43-
testInvalidValuesForSimpleValidator(t, validator,
56+
strings.Repeat("very-long-header", 16),
57+
)
58+
testInvalidValuesForSimpleValidator(
59+
t,
60+
validator,
4461
`\`,
4562
`test test`,
4663
`test"test`,
@@ -49,5 +66,6 @@ func TestValidateValidHeaderName(t *testing.T) {
4966
"host",
5067
"my-header[]",
5168
"my-header&",
52-
strings.Repeat("very-long-header", 16)+"1")
69+
strings.Repeat("very-long-header", 16)+"1",
70+
)
5371
}

internal/mode/static/nginx/config/validation/framework_test.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ func testValidValuesForSupportedValuesValidator[T configValue](
4343
f supportedValuesValidatorFunc[T],
4444
values ...T,
4545
) {
46-
runValidatorTests(t, func(g *WithT, v T) {
47-
valid, supportedValues := f(v)
48-
g.Expect(valid).To(BeTrue(), createFailureMessage(v))
49-
g.Expect(supportedValues).To(BeNil(), createFailureMessage(v))
50-
}, "valid_value", values...)
46+
runValidatorTests(
47+
t,
48+
func(g *WithT, v T) {
49+
valid, supportedValues := f(v)
50+
g.Expect(valid).To(BeTrue(), createFailureMessage(v))
51+
g.Expect(supportedValues).To(BeNil(), createFailureMessage(v))
52+
},
53+
"valid_value",
54+
values...,
55+
)
5156
}
5257

5358
func testInvalidValuesForSupportedValuesValidator[T configValue](
@@ -56,11 +61,16 @@ func testInvalidValuesForSupportedValuesValidator[T configValue](
5661
supportedValuesMap map[T]struct{},
5762
values ...T,
5863
) {
59-
runValidatorTests(t, func(g *WithT, v T) {
60-
valid, supportedValues := f(v)
61-
g.Expect(valid).To(BeFalse(), createFailureMessage(v))
62-
g.Expect(supportedValues).To(Equal(getSortedKeysAsString(supportedValuesMap)), createFailureMessage(v))
63-
}, "invalid_value", values...)
64+
runValidatorTests(
65+
t,
66+
func(g *WithT, v T) {
67+
valid, supportedValues := f(v)
68+
g.Expect(valid).To(BeFalse(), createFailureMessage(v))
69+
g.Expect(supportedValues).To(Equal(getSortedKeysAsString(supportedValuesMap)), createFailureMessage(v))
70+
},
71+
"invalid_value",
72+
values...,
73+
)
6474
}
6575

6676
func TestValidateInSupportedValues(t *testing.T) {
@@ -74,12 +84,19 @@ func TestValidateInSupportedValues(t *testing.T) {
7484
return validateInSupportedValues(value, supportedValues)
7585
}
7686

77-
testValidValuesForSupportedValuesValidator(t, validator,
87+
testValidValuesForSupportedValuesValidator(
88+
t,
89+
validator,
7890
"value1",
7991
"value2",
80-
"value3")
81-
testInvalidValuesForSupportedValuesValidator(t, validator, supportedValues,
82-
"value4")
92+
"value3",
93+
)
94+
testInvalidValuesForSupportedValuesValidator(
95+
t,
96+
validator,
97+
supportedValues,
98+
"value4",
99+
)
83100
}
84101

85102
func TestGetSortedKeysAsString(t *testing.T) {

0 commit comments

Comments
 (0)