8000 refactor: replace if-else chains with switch statements · coder/coder@7d11352 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d11352

Browse files
committed
refactor: replace if-else chains with switch statements
Changed if-else chains to switch statements in several files to address gocritic:ifElseChain linter warnings. This makes the code more idiomatic and easier to maintain. Files updated: - coderd/audit/request.go - coderd/healthcheck/derphealth/derp.go - vpn/router.go
1 parent 527df65 commit 7d11352

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

coderd/audit/request.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ func InitRequest[T Auditable](w http.ResponseWriter, p *RequestParams) (*Request
407407

408408
var userID uuid.UUID
409409
key, ok := httpmw.APIKeyOptional(p.Request)
410-
if ok {
410+
switch {
411+
case ok:
411412
userID = key.UserID
412-
} else if req.UserID != uuid.Nil {
413+
case req.UserID != uuid.Nil:
413414
userID = req.UserID
414-
} else {
415+
default:
415416
// if we do not have a user associated with the audit action
416417
// we do not want to audit
417418
// (this pertains to logins; we don't want to capture non-user login attempts)
@@ -557,16 +558,18 @@ func BaggageFromContext(ctx context.Context) WorkspaceBuildBaggage {
557558
}
558559

559560
func either[T Auditable, R any](old, new T, fn func(T) R, auditAction database.AuditAction) R {
560-
if ResourceID(new) != uuid.Nil {
561+
switch {
562+
case ResourceID(new) != uuid.Nil:
561563
return fn(new)
562-
} else if ResourceID(old) != uuid.Nil {
564+
case ResourceID(old) != uuid.Nil:
563565
return fn(old)
564-
} else if auditAction == database.AuditActionLogin || auditAction == database.AuditActionLogout {
566+
case auditAction == database.AuditActionLogin || auditAction == database.AuditActionLogout:
565567
// If the request action is a login or logout, we always want to audit it even if
566568
// there is no diff. See the comment in audit.InitRequest for more detail.
567569
return fn(old)
570+
default:
571+
panic("both old and new are nil")
568572
}
569-
panic("both old and new are nil")
570573
}
571574

572575
func ParseIP(ipStr string) pqtype.Inet {

coderd/healthcheck/derphealth/derp.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,15 @@ func (r *RegionReport) Run(ctx context.Context) {
197197
return
198198
}
199199

200-
if len(r.Region.Nodes) == 1 {
200+
switch {
201+
case len(r.Region.Nodes) == 1:
201202
r.Healthy = r.NodeReports[0].Severity != health.SeverityError
202203
r.Severity = r.NodeReports[0].Severity
203-
} else if unhealthyNodes == 1 {
204+
case unhealthyNodes == 1:
204205
// r.Healthy = true (by default)
205206
r.Severity = health.SeverityWarning
206207
r.Warnings = append(r.Warnings, health.Messagef(health.CodeDERPOneNodeUnhealthy, oneNodeUnhealthy))
207-
} else if unhealthyNodes > 1 {
208+
case unhealthyNodes > 1:
208209
r.Healthy = false
209210

210211
// Review node reports and select the highest severity.

vpn/router.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,39 @@ func convertRouterConfig(cfg router.Config) *NetworkSettingsRequest {
4040
v6LocalAddrs := make([]string, 0)
4141
v6PrefixLengths := make([]uint32, 0)
4242
for _, addrs := range cfg.LocalAddrs {
43-
if addrs.Addr().Is4() {
43+
switch {
44+
case addrs.Addr().Is4():
4445
v4LocalAddrs = append(v4LocalAddrs, addrs.Addr().String())
4546
v4SubnetMasks = append(v4SubnetMasks, prefixToSubnetMask(addrs))
46-
} else if addrs.Addr().Is6() {
47+
case addrs.Addr().Is6():
4748
v6LocalAddrs = append(v6LocalAddrs, addrs.Addr().String())
4849
// #nosec G115 - Safe conversion as IPv6 prefix lengths are always within uint32 range (0-128)
4950
v6PrefixLengths = append(v6PrefixLengths, uint32(addrs.Bits()))
50-
} else {
51+
default:
5152
continue
5253
}
5354
}
5455
v4Routes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
5556
v6Routes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
5657
for _, route := range cfg.Routes {
57-
if route.Addr().Is4() {
58+
switch {
59+
case route.Addr().Is4():
5860
v4Routes = append(v4Routes, convertToIPV4Route(route))
59-
} else if route.Addr().Is6() {
61+
case route.Addr().Is6():
6062
v6Routes = append(v6Routes, convertToIPV6Route(route))
61-
} else {
63+
default:
6264
continue
6365
}
6466
}
6567
v4ExcludedRoutes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
6668
v6ExcludedRoutes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
6769
for _, route := range cfg.LocalRoutes {
68-
if route.Addr().Is4() {
70+
switch {
71+
case route.Addr().Is4():
6972
v4ExcludedRoutes = append(v4ExcludedRoutes, convertToIPV4Route(route))
70-
} else if route.Addr().Is6() {
73+
case route.Addr().Is6():
7174
v6ExcludedRoutes = append(v6ExcludedRoutes, convertToIPV6Route(route))
72-
} else {
75+
default:
7376
continue
7477
}
7578
}
@@ -125,4 +128,4 @@ func convertToIPV6Route(route netip.Prefix) *NetworkSettingsRequest_IPv6Settings
125128
func prefixToSubnetMask(prefix netip.Prefix) string {
126129
maskBytes := net.CIDRMask(prefix.Masked().Bits(), net.IPv4len*8)
127130
return net.IP(maskBytes).String()
128-
}
131+
}

0 commit comments

Comments
 (0)
0