Skip to content

Commit 4412a21

Browse files
author
Kate Osborn
committed
Use MustcastObject helper method
1 parent d37c2da commit 4412a21

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

internal/mode/static/policies/clientsettings/generator.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package clientsettings
22

33
import (
4-
"fmt"
54
"text/template"
65

76
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
@@ -39,10 +38,7 @@ keepalive_timeout {{ .KeepAlive.Timeout.Server }};
3938

4039
// Generate generates configuration as []byte for a ClientSettingsPolicy.
4140
func Generate(policy policies.Policy) []byte {
42-
csp, ok := policy.(*ngfAPI.ClientSettingsPolicy)
43-
if !ok {
44-
panic(fmt.Sprintf("expected ClientSettingsPolicy, got: %T", policy))
45-
}
41+
csp := helpers.MustCastObject[*ngfAPI.ClientSettingsPolicy](policy)
4642

4743
return helpers.MustExecuteTemplate(tmpl, csp.Spec)
4844
}

internal/mode/static/policies/clientsettings/validator.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sigs.k8s.io/gateway-api/apis/v1alpha2"
1010

1111
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
12+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
1213
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
1314
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
1415
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
@@ -27,10 +28,7 @@ func NewValidator(genericValidator validation.GenericValidator) *Validator {
2728

2829
// Validate validates the spec of a ClientSettingsPolicy.
2930
func (v *Validator) Validate(policy policies.Policy) error {
30-
csp, ok := policy.(*ngfAPI.ClientSettingsPolicy)
31-
if !ok {
32-
panic(fmt.Sprintf("expected ClientSettingsPolicy, got: %T", policy))
33-
}
31+
csp := helpers.MustCastObject[*ngfAPI.ClientSettingsPolicy](policy)
3432

3533
if err := validateTargetRef(csp.Spec.TargetRef); err != nil {
3634
return err
@@ -41,14 +39,10 @@ func (v *Validator) Validate(policy policies.Policy) error {
4139

4240
// Conflicts returns true if the two ClientSettingsPolicies conflict.
4341
func (v *Validator) Conflicts(polA, polB policies.Policy) bool {
44-
a, okA := polA.(*ngfAPI.ClientSettingsPolicy)
45-
b, okB := polB.(*ngfAPI.ClientSettingsPolicy)
46-
47-
if !okA || !okB {
48-
panic(fmt.Sprintf("expected ClientSettingsPolicies, got: %T, %T", polA, polB))
49-
}
42+
cspA := helpers.MustCastObject[*ngfAPI.ClientSettingsPolicy](polA)
43+
cspB := helpers.MustCastObject[*ngfAPI.ClientSettingsPolicy](polB)
5044

51-
return conflicts(a.Spec, b.Spec)
45+
return conflicts(cspA.Spec, cspB.Spec)
5246
}
5347

5448
func conflicts(a, b ngfAPI.ClientSettingsPolicySpec) bool {

0 commit comments

Comments
 (0)