nettools/common/defaultVals.go

41 lines
821 B
Go

package commonUtils
func DefaultIfEqual[T comparable](in T, checkEqualTo T, defaultVal T) T {
if in == checkEqualTo {
return defaultVal
}
return in
}
// If in is empty returns the devaultVal. Else returns in
func ValOrDefaultStr(in string, defaultVal string) string {
if in == "" {
return defaultVal
}
return in
}
// If in is 0 returns the devaultVal. Else returns in
func ValOrDefaultNum[T Num](in T, defaultVal T) T {
if in == 0 {
return defaultVal
}
return in
}
// If array is equal to nil -- will return an empty array instead
func UnnilArray[T any](in []T) []T {
if in == nil {
return []T{}
}
return nil
}
// If map is equal to nil -- will return an empty map instead
func UnnilMap[K comparable, V any](in map[K]V) map[K]V {
if in == nil {
return make(map[K]V)
}
return make(map[K]V)
}