generics

package
v0.19.934 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

README

go-generics

go package for shared generic functions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyTrue

func AnyTrue(vals ...bool) bool

func DefaultName

func DefaultName() string

func DeleteSliceKey

func DeleteSliceKey(obj interface{}, key string)

func DiffMaps

func DiffMaps[K comparable, T any](newVals, oldVals map[K]T) (map[K]T, map[K]T)

DiffMaps returns two additions, the additions that need to be added, and the ones that need to be deleted

func DisplayName

func DisplayName(val string) string

func EncodeNestedForHstore added in v0.19.934

func EncodeNestedForHstore(data map[string]any) map[string]any

EncodeNestedForHstore returns a copy of data where any map-valued entry has been replaced with its JSON encoding. Scalars and slices pass through unchanged.

Hstore stores map[string]string, so nested map values would otherwise be flattened by ToStringMap via fmt.Sprintf("%v", v) into "map[k:v]" text — a format StringToMapDecodeHook reads back by splitting on whitespace, which silently truncates any value containing a space, colon, or bracket. JSON encoding the nested level once at write time round-trips losslessly via the JSON branch of StringToMapDecodeHook.

Only top-level map values are encoded. Slices are left alone because the composed decoder pairs this hook with mapstructure.StringToSliceHookFunc(","), which expects comma-separated strings, not JSON arrays.

func ErrsToStrings

func ErrsToStrings[T Error](val []T) []string

func FindMap

func FindMap[K comparable, T comparable](k K, inps ...map[K]T) T

func First

func First[T comparable](vals ...T) T

First returns the first non-empty value in the provided slice.

If the type of T is a pointer, the first non-nil value is returned, even if it is a zero value.

func FirstNonEmptyString

func FirstNonEmptyString(vs ...string) string

func FromPtrStr

func FromPtrStr(s *string) string

FromPtrString safely returns an object from a pointer

func GetFakeObj

func GetFakeObj[T any]() T

GetFakeObj returns a faked instance of type T will panic on error meant exclusively for testing

func GetTimeDuration

func GetTimeDuration(startedAt time.Time, finishedAt time.Time) time.Duration

func HasAnyPrefix

func HasAnyPrefix(val string, prefixes ...string) bool

func IntSliceToStringSlice

func IntSliceToStringSlice(vals []interface{}) []string

func IsJSON

func IsJSON(s []byte) bool

func IsJSONStr

func IsJSONStr(str string) bool

func MapToKeys

func MapToKeys[T comparable, V any](in map[T]V) []T

func MapValuesToSlice

func MapValuesToSlice(inp map[string]string) []string

func MergeMap

func MergeMap[K comparable, T any](src map[K]T, vals ...map[K]T) map[K]T

func MergeMaps

func MergeMaps(a, b map[string]interface{}) map[string]interface{}

Merges source and destination map, preferring values from the source map Taken from github.com/helm/pkg/cli/values/options.go

func MergeSlice

func MergeSlice[T comparable](a, b []T) []T

func Name

func Name(separator string, wordsPerName int) string

func NewNullBool

func NewNullBool(val bool) sql.NullBool

func NewNullBoolFromPtr added in v0.19.850

func NewNullBoolFromPtr(val *bool) sql.NullBool

func SliceAfterValue

func SliceAfterValue[T comparable](vals []T, val T) []T

func SliceContains

func SliceContains[T comparable](val T, vals []T) bool

func SliceToGroups

func SliceToGroups[T any](vals []T, grpSize int) [][]T

SliceToGroups turns a slice into a set of groups, with the group size. If the group size is less than 1, we set it to one.

func SliceToMap

func SliceToMap[T comparable, V any](vals []T) map[T]V

func SliceToMapDefault

func SliceToMapDefault[T comparable, V any](vals []T, deflt V) map[T]V

func SliceToStrings

func SliceToStrings[T stringer](val []T) []string

func StringOneOf

func StringOneOf(val string, vals ...string) bool

func StringToMapDecodeHook

func StringToMapDecodeHook() mapstructure.DecodeHookFunc

StringToMapDecodeHook decodes hstore values back into maps.

Two on-disk formats are accepted:

  1. JSON object — the format written by EncodeNestedForHstore for new rows. Whitespace, colons, and brackets in values round-trip losslessly.
  2. Legacy fmt.Sprintf("%v", map) text shaped like "map[k1:v1 k2:v2]". Pre-existing rows still use this format. The parser is lossy on any value containing whitespace and is preserved only for backward compatibility — new writes must go through EncodeNestedForHstore.

func StringToNestedMapDecodeHook

func StringToNestedMapDecodeHook() mapstructure.DecodeHookFunc

StringToNestedMapDecodeHook decodes input interface into nested map if needed Beta: use with caution, not used anywhere as of now, bugs expected

func StructToMap

func StructToMap(obj any) (map[string]string, error)

func SubMap

func SubMap[K comparable, T any](newVals, oldVals map[K]T) map[K]T

func SystemName

func SystemName(val string) string

func ToIntMap

func ToIntMap[T any](inp map[string]T) map[string]interface{}

func ToIntSlice

func ToIntSlice[T any](vals []T) []interface{}

func ToMapstructure

func ToMapstructure(inp interface{}) (map[string]interface{}, error)

func ToMapstructureWithJSONTag

func ToMapstructureWithJSONTag(inp interface{}) (map[string]interface{}, error)

func ToPtr

func ToPtr[T any](v T) *T

ToPtr generically returns a reference to the value v of type T

func ToStringMap

func ToStringMap(inp map[string]interface{}) map[string]string

func ToStringSlice

func ToStringSlice[T any](vals []T) []string

func UniqueSlice

func UniqueSlice[T comparable](items []T) []T

UniqueSlice takes a slice and returns a deduplicated slice of elements

func ValOrDefault

func ValOrDefault[T comparable](val, deflt T) T

Types

type Error

type Error interface {
	Error() string
}

Error interface defines the Error() string method.

type NullString

type NullString struct {
	sql.NullString
}

func NewNullString

func NewNullString(val string) NullString

func (NullString) Empty

func (s NullString) Empty() bool

func (NullString) MarshalJSON

func (s NullString) MarshalJSON() ([]byte, error)

func (*NullString) UnmarshalJSON

func (s *NullString) UnmarshalJSON(data []byte) error

func (NullString) ValueOrDefault

func (s NullString) ValueOrDefault(def string) string

func (NullString) ValueString

func (s NullString) ValueString() string

type NullTime

type NullTime struct {
	sql.NullTime
}

func NewNullTime

func NewNullTime(val time.Time) NullTime

func (NullTime) Empty

func (dt NullTime) Empty() bool

func (NullTime) MarshalJSON

func (dt NullTime) MarshalJSON() ([]byte, error)

func (*NullTime) UnmarshalJSON

func (dt *NullTime) UnmarshalJSON(data []byte) error

func (NullTime) ValueOrDefault

func (dt NullTime) ValueOrDefault(def time.Time) time.Time

func (NullTime) ValueTime

func (dt NullTime) ValueTime() time.Time

Jump to

Keyboard shortcuts

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