utils

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package utils provides utility functions for key mapping and conversions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackendObjectReferenceToKey

func BackendObjectReferenceToKey(backendRef gatewayv1.BackendObjectReference) string

BackendObjectReferenceToKey converts a BackendObjectReferenceToKey to a unique string key. It handles nil pointers by using a consistent placeholder.

func ClearMap

func ClearMap[K comparable, V any](m map[K]V)

Generic function to clear a map in place

func ComparePointers

func ComparePointers[T cmp.Ordered](a, b *T) int

ComparePointers compares two pointers to any ordered type. It establishes a consistent sort order where nil values come before non-nil values.

It returns:

  • -1 if a < b
  • 0 if a == b (or both are nil)
  • +1 if a > b

func ConvertSliceWithFunc added in v0.8.0

func ConvertSliceWithFunc[U, V any](arg []U, f func(U) V) []V

func ConvertV1Alpha2HostnameToString added in v0.8.0

func ConvertV1Alpha2HostnameToString(hostname v1alpha2.Hostname) string

func GetHostnamesForRouteWithListener added in v0.8.0

func GetHostnamesForRouteWithListener(listenerHostname *string, routeHostnames []string) []string

GetHostnamesForRouteWithListener returns the hostnames that match a listener and a route. The rules are based on the Gateway API specification. If the listener hostname is not set, it matches any route hostname. If the listener hostname is a wildcard, it checks if it matches any route hostname. If the route hostnames are empty, the listener hostname matches the route hostnames.

func GetNamespacedName added in v0.8.0

func GetNamespacedName(name gatewayv1.ObjectName, namespace *gatewayv1.Namespace, defaultNamespace string) types.NamespacedName

func KeyToBackendObjectReference

func KeyToBackendObjectReference(key string) (gatewayv1.BackendObjectReference, error)

KeyToBackendObjectReference converts a string key back to a gatewayv1.BackendObjectReference. It is the inverse of BackendObjectReferenceToKey.

func KeyToParentRef

func KeyToParentRef(key string) (gatewayv1.ParentReference, error)

KeyToParentRef converts a string key back to a gatewayv1.ParentReference. It is the inverse of ParentRefToKey.

func Keys

func Keys[K comparable, V any](src map[K]V) map[K]struct{}

func MapToSortedListByCreationTimestamp

func MapToSortedListByCreationTimestamp[T ObjectWithTimestamp](objects map[types.NamespacedName]T) []T

func NamespaceAsString

func NamespaceAsString(ns *gatewayv1.Namespace) string

func ObjectKeyFromNamespacedName

func ObjectKeyFromNamespacedName(s string) (client.ObjectKey, error)

func ParentRefToKey

func ParentRefToKey(parentRef gatewayv1.ParentReference) string

ParentRefToKey converts a ParentReference to a unique string key. It handles nil pointers by using a consistent placeholder.

func ParseNamespacedName

func ParseNamespacedName(s string) (types.NamespacedName, error)

ParseNamespacedName parses a "namespace/name" string into a NamespacedName.

func PointerDefaultValueIfNil added in v0.8.0

func PointerDefaultValueIfNil[T any](arg *T) T

PointerDefaultValueIfNil dereferences a pointer and returns its value. If the pointer is nil, the zero value of T is returned instead.

Example:

x := 42
v := PointerDefaultValueIfNil(&x)  // returns 42
v := PointerDefaultValueIfNil(nil) // returns 0 (zero value of int)

func RouteGroupKindsToString

func RouteGroupKindsToString(routesGK []gatewayv1.RouteGroupKind) string

func SetDifference

func SetDifference[K comparable, V any](mapA, mapB map[K]V) map[K]struct{}

SetDifference finds the keys that are in mapA but not in mapB. It works for any map with a comparable key type K and any value type V.

func SetIntersection

func SetIntersection[K comparable, V any](mapA, mapB map[K]V) map[K]struct{}

func SortByCreationTimestamp

func SortByCreationTimestamp[T ObjectWithTimestamp](objects []T)

func StrPtrToString

func StrPtrToString[T stringer](p *T) string

func StringToPtr

func StringToPtr[T stringer](s string) *T

StringToPtr converts a string to a pointer of a string-based type. If the string is the placeholder "_", it returns nil.

Types

type Errors

type Errors []error

func (*Errors) Add

func (e *Errors) Add(errs ...error)

func (*Errors) AddErrors

func (e *Errors) AddErrors(errs Errors)

func (*Errors) Result

func (e *Errors) Result() error

type KeyMap

type KeyMap[KEY any, VALUE any] struct {
	// contains filtered or unexported fields
}

func NewKeyMap

func NewKeyMap[KEY any, VALUE any](keygen func(KEY) string) KeyMap[KEY, VALUE]

func (*KeyMap[KEY, VALUE]) Clear

func (m *KeyMap[KEY, VALUE]) Clear()

func (*KeyMap[KEY, VALUE]) Delete

func (m *KeyMap[KEY, VALUE]) Delete(key KEY)

func (*KeyMap[KEY, VALUE]) Exists

func (m *KeyMap[KEY, VALUE]) Exists(key KEY) bool

func (*KeyMap[KEY, VALUE]) Get

func (m *KeyMap[KEY, VALUE]) Get(key KEY) (VALUE, bool)

func (*KeyMap[KEY, VALUE]) Iterate

func (m *KeyMap[KEY, VALUE]) Iterate(callback func(string, VALUE) bool)

func (*KeyMap[KEY, VALUE]) Len

func (m *KeyMap[KEY, VALUE]) Len() int

func (*KeyMap[KEY, VALUE]) Set

func (m *KeyMap[KEY, VALUE]) Set(key KEY, value VALUE)

type ObjectWithTimestamp

type ObjectWithTimestamp interface {
	GetCreationTimestamp() metav1.Time
	GetName() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL