Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add converter pkg to house type conversion helpers #1337

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2022 The Kubernetes Authors.
robscott marked this conversation as resolved.
Show resolved Hide resolved

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,20 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package utils
package converter
robscott marked this conversation as resolved.
Show resolved Hide resolved

import (
gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// PathMatchTypePtr translates a string to *PathMatchType
func PathMatchTypePtr(s string) *gatewayv1a2.PathMatchType {
result := gatewayv1a2.PathMatchType(s)
// HeaderMatchTypePtr translates a string to *PathMatchType
func HeaderMatchTypePtr(s string) *gatewayv1a2.HeaderMatchType {
result := gatewayv1a2.HeaderMatchType(s)
return &result
}

// PortNumberPtr translates an int to a *PortNumber
func PortNumberPtr(p int) *gatewayv1a2.PortNumber {
result := gatewayv1a2.PortNumber(p)
// PathMatchTypePtr translates a string to *PathMatchType
func PathMatchTypePtr(s string) *gatewayv1a2.PathMatchType {
result := gatewayv1a2.PathMatchType(s)
return &result
}
61 changes: 61 additions & 0 deletions apis/v1alpha2/converter/httproute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package converter

import (
"testing"

gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

func Test_PathMatchTypePtr(t *testing.T) {
pathMatchTypePtrTests := []struct {
name string
pathType string
expectedPath gatewayv1a2.PathMatchType
}{
{
name: "valid path exact match",
pathType: "Exact",
expectedPath: gatewayv1a2.PathMatchExact,
},
{
name: "valid path exact match using constant",
pathType: string(gatewayv1a2.PathMatchExact),
expectedPath: gatewayv1a2.PathMatchExact,
},
{
name: "valid path prefix match",
pathType: "PathPrefix",
expectedPath: gatewayv1a2.PathMatchPathPrefix,
},
{
name: "valid path regular expression match",
pathType: "RegularExpression",
expectedPath: gatewayv1a2.PathMatchRegularExpression,
},
}

for _, tc := range pathMatchTypePtrTests {
t.Run(tc.name, func(t *testing.T) {
path := PathMatchTypePtr(tc.pathType)
if *path != tc.expectedPath {
t.Errorf("Expected path %s, got %s", tc.expectedPath, *path)
}
})
}
}
27 changes: 27 additions & 0 deletions apis/v1alpha2/converter/shared_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package converter

import (
gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// PortNumberPtr translates an int to a *PortNumber
func PortNumberPtr(p int) *gatewayv1a2.PortNumber {
result := gatewayv1a2.PortNumber(p)
return &result
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package utils
package converter

import (
"testing"
Expand Down Expand Up @@ -68,37 +68,3 @@ func Test_PortNumberPtr(t *testing.T) {
})
}
}

func Test_PathMatchTypePtr(t *testing.T) {
pathMatchTypePtrTests := []struct {
name string
pathType string
expectedPath gatewayv1a2.PathMatchType
}{
{
name: "valid path exact match",
pathType: "Exact",
expectedPath: gatewayv1a2.PathMatchExact,
},

{
name: "valid path prefix match",
pathType: "PathPrefix",
expectedPath: gatewayv1a2.PathMatchPathPrefix,
},
{
name: "valid path regular expression match",
pathType: "RegularExpression",
expectedPath: gatewayv1a2.PathMatchRegularExpression,
},
}

for _, tc := range pathMatchTypePtrTests {
t.Run(tc.name, func(t *testing.T) {
path := PathMatchTypePtr(tc.pathType)
if *path != tc.expectedPath {
t.Errorf("Expected path %s, got %s", tc.expectedPath, *path)
}
})
}
}
Loading