Documentation
¶
Index ¶
- func BoolToYesNo(b *bool) string
- func CopyPointer[T any](original *T) (copied *T)
- func CopySlice[T any](original []T) (copied []T)
- func DefaultComparable[T comparable](existing, defaultValue T) (result T)
- func DefaultPointer[T any](existing *T, defaultValue T) (result *T)
- func DefaultSlice[T any](existing, defaultValue []T) (result []T)
- func DefaultValidator[T SelfValidator](existing, defaultValue T) (result T)
- func ObfuscateKey(key string) (obfuscatedKey string)
- func OverrideWithComparable[T comparable](existing, other T) (result T)
- func OverrideWithPointer[T any](existing, other *T) (result *T)
- func OverrideWithSlice[T any](existing, other []T) (result []T)
- func OverrideWithValidator[T SelfValidator](existing, other T) (result T)
- type SelfValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolToYesNo ¶ added in v0.3.0
BoolToYesNo returns "yes" if the given boolean is true, "no" if the given boolean is false, and an empty string if the given boolean pointer is nil.
func CopyPointer ¶
func CopyPointer[T any](original *T) (copied *T)
CopyPointer returns a new pointer to the copied value of the original argument value.
func CopySlice ¶
func CopySlice[T any](original []T) (copied []T)
CopySlice returns a new slice with each element of the original slice copied.
func DefaultComparable ¶ added in v0.4.0
func DefaultComparable[T comparable](existing, defaultValue T) (result T)
DefaultComparable returns the existing argument if it is not the zero value, otherwise it returns the defaultValue argument. If used with an interface and an implementation of the interface, it must be instantiated with the interface type, for example: variable := DefaultComparable[Interface](variable, &implementation{}) Avoid using this function for non-interface pointers, use DefaultPointer instead to create a new pointer.
func DefaultPointer ¶
func DefaultPointer[T any](existing *T, defaultValue T) (result *T)
DefaultPointer returns the existing argument if it is not nil. Otherwise it returns a new pointer to the defaultValue argument. To default an interface to an implementation, use DefaultComparable.
func DefaultSlice ¶
func DefaultSlice[T any](existing, defaultValue []T) (result []T)
DefaultSlice returns the existing slice argument if is not nil. Otherwise it returns a new slice with the copied values of the defaultValue slice argument. Note it is preferrable to use this function for added mutation safety on the result, but one can use DefaultSliceRaw if performance matters.
func DefaultValidator ¶
func DefaultValidator[T SelfValidator](existing, defaultValue T) ( result T)
DefaultValidator returns the existing argument if it is valid, otherwise it returns the defaultValue argument.
func ObfuscateKey ¶ added in v0.3.0
ObfuscateKey returns an obfuscated key for logging purposes. If the key is empty, `[not set]` is returned. If the key has up to 128 bits of security with 2 characters removed, it will be obfuscated as `[set]`. If the key has at least 128 bits of security if at least 2 characters are removed from it, it will be obfuscated as `start_of_key...end_of_key`, where the start and end parts are each at least 1 character long, and up to 3 characters long. Note the security bits are calculated by assuming each unique character in the given key is a symbol in the alphabet, which gives a worst case scenario regarding the alphabet size. This will likely produce lower security bits estimated compared to the actual security bits of the key, but it's better this way to avoid divulging information about the key when it should not be. Finally, the 128 bits security is chosen because this function is to be used for logging purposes, which should not be exposed publicly either.
func OverrideWithComparable ¶ added in v0.4.0
func OverrideWithComparable[T comparable](existing, other T) (result T)
OverrideWithComparable returns the other argument if it is not the zero value, otherwise it returns the existing argument. If used with an interface and an implementation of the interface, it must be instantiated with the interface type, for example: variable := OverrideWithComparable[Interface](variable, &implementation{}) Avoid using this function for non-interface pointers, use OverrideWithPointer instead to create a new pointer.
func OverrideWithPointer ¶
func OverrideWithPointer[T any](existing, other *T) (result *T)
OverrideWithPointer returns the existing argument if the other argument is nil. Otherwise it returns a new pointer to the copied value of the other argument value, for added mutation safety. To override an interface and an implementation, use OverrideWithComparable.
func OverrideWithSlice ¶
func OverrideWithSlice[T any](existing, other []T) (result []T)
OverrideWithSlice returns the existing slice argument if the other slice argument is nil. Otherwise it returns a new slice with the copied values of the other slice argument. Note it is preferrable to use this function for added mutation safety on the result, but one can use OverrideWithSliceRaw if performance matters.
func OverrideWithValidator ¶
func OverrideWithValidator[T SelfValidator](existing, other T) ( result T)
OverrideWithValidator returns the existing argument if other is not valid, otherwise it returns the other argument.
Types ¶
type SelfValidator ¶
type SelfValidator interface { // IsValid returns true if the value is valid, false otherwise. IsValid() bool }
SelfValidator is an interface for a type that can validate itself. This is notably the case of netip.IP and netip.Prefix, and can be implemented by the user of this library as well.