Documentation
¶
Overview ¶
Package values provides generic any-to-typed conversion and MapAny utilities.
Index ¶
- func Bool(v any) bool
- func Coalesce[M Measurable[any]](args ...M) M
- func Float32(v any) float32
- func Float64(v any) float64
- func IndentJSON(data string) string
- func Int(v any) int
- func Int64(v any) int64
- func IntSlice(v any) []int
- func IsCollection(value any) bool
- func IsEmpty(value any) bool
- func IsMap(value any) bool
- func IsSlice(value any) bool
- func JSON(value any) string
- func JSONIndent(value any) string
- func NumbersCoalesce[T ~int | ~int32 | ~uint | ~uint32 | ~int64 | ~uint64](items ...T) T
- func OrderedMapKeys[K constraints.Ordered, V any](m map[K]V) []K
- func RangeOrderedMap[K constraints.Ordered, V any](c map[K]V, f func(k K, v V) bool)
- func Select[T any](cond bool, a, b T) T
- func Shrink(value any) any
- func String(v any) string
- func StringSlice(v any) []string
- func StringsCoalesce(str ...string) string
- func Time(v any) *time.Time
- func UInt64(v any) uint64
- type HasDisplayName
- type HasDisplayNames
- type HasDisplayNamesMap
- type HasName
- type HasNames
- type HasNamesMap
- type HasValuesMap
- type MapAny
- func (c MapAny) Bool(k string) bool
- func (c MapAny) Extract(path ...string) MapAny
- func (c MapAny) Float32(k string) float32
- func (c MapAny) Float64(k string) float64
- func (c MapAny) GetOrSet(key string, getter func(key string) any) any
- func (c MapAny) Has(k string) bool
- func (c MapAny) Int(k string) int
- func (c MapAny) Int64(k string) int64
- func (c MapAny) IsMap(k string) bool
- func (c MapAny) IsSlice(k string) bool
- func (c MapAny) JSON() string
- func (c MapAny) JSONIndent() string
- func (c MapAny) Map(k string) MapAny
- func (c *MapAny) Merge(m MapAny) *MapAny
- func (c *MapAny) Scan(value any) error
- func (c MapAny) Shrink() MapAny
- func (c MapAny) Slice(k string) []any
- func (c MapAny) String(k string) string
- func (c MapAny) StringSlice(k string) []string
- func (c MapAny) Time(k string) *time.Time
- func (c MapAny) To(val any) error
- func (c MapAny) TraverseSubMaps(f func(k string, v MapAny) (bool, error)) error
- func (c MapAny) UInt64(k string) uint64
- func (c MapAny) Value() (driver.Value, error)
- func (c MapAny) YAML() string
- type Measurable
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Coalesce ¶
func Coalesce[M Measurable[any]](args ...M) M
Coalesce returns the first non-empty value
func IndentJSON ¶ added in v0.12.54
IndentJSON indents a JSON string It will return the original string if it fails to indent This is useful for pretty printing JSON strings
func JSONIndent ¶ added in v0.12.54
JSONIndent returns the value as a JSON string with indentation
func NumbersCoalesce ¶
NumbersCoalesce returns the first value from the supplied list that is not 0, or 0 if there are no values that are not zero
func OrderedMapKeys ¶ added in v0.5.36
func OrderedMapKeys[K constraints.Ordered, V any](m map[K]V) []K
OrderedMapKeys returns ordered keys
func RangeOrderedMap ¶ added in v0.5.36
func RangeOrderedMap[K constraints.Ordered, V any](c map[K]V, f func(k K, v V) bool)
RangeOrderedMap range over ordered map
func Shrink ¶ added in v0.15.81
Shrink removes empty values recursively: - returns nil if value is empty - if it's a Map, then only non empty values are returned in a map, or nil - if it's a Slice, then only non empty values are returned.
Example ¶
// Example with nested structures
input := map[string]any{
"users": []any{
map[string]any{"name": "", "age": 0, "active": false},
map[string]any{"name": "John", "age": 30, "active": true, "tags": []any{"", "admin"}},
map[string]any{"name": "Jane", "age": 25, "tags": []any{"", "", ""}},
},
"config": map[string]any{
"debug": false,
"timeout": 0,
"host": "localhost",
},
"empty_section": map[string]any{
"a": "",
"b": 0,
"c": []any{},
},
}
result := Shrink(input)
fmt.Printf("Result: %+v\n", result)
// Example with simple values
fmt.Printf("Empty string: %v\n", Shrink(""))
fmt.Printf("Non-empty string: %v\n", Shrink("hello"))
fmt.Printf("Zero int: %v\n", Shrink(0))
fmt.Printf("Non-zero int: %v\n", Shrink(42))
fmt.Printf("Empty slice: %v\n", Shrink([]any{}))
fmt.Printf("Slice with empty values: %v\n", Shrink([]any{"", 0, false}))
fmt.Printf("Slice with mixed values: %v\n", Shrink([]any{"", "hello", 0, 42}))
Output: Result: map[config:map[host:localhost] users:[map[active:true age:30 name:John tags:[admin]] map[age:25 name:Jane]]] Empty string: <nil> Non-empty string: hello Zero int: <nil> Non-zero int: 42 Empty slice: <nil> Slice with empty values: <nil> Slice with mixed values: [hello 42]
func StringsCoalesce ¶
StringsCoalesce returns the first non-empty string value
Types ¶
type HasDisplayName ¶ added in v0.15.77
type HasDisplayName interface {
DisplayName() string
}
type HasDisplayNames ¶ added in v0.15.77
type HasDisplayNames interface {
DisplayNames() []string
}
type HasDisplayNamesMap ¶ added in v0.15.77
type HasNamesMap ¶ added in v0.15.77
type HasValuesMap ¶ added in v0.15.77
type MapAny ¶
MapAny provides map of values
func FromJSON ¶
FromJSON returns map from json encoded string, this method does not return value on error, as the value is expected a valid JSON string.
func FromStruct ¶ added in v0.15.81
func FromYAML ¶
FromYAML returns map from yaml encoded string, this method does not return value on error, as the value is expected a valid YAML string.
func (MapAny) JSONIndent ¶ added in v0.12.54
func (MapAny) Shrink ¶ added in v0.15.81
Shrink shrinks the map by removing empty keys
Example ¶
// Example with MapAny
m := MapAny{
"empty": "",
"text": "hello",
"zero": 0,
"num": 42,
"false": false,
"true": true,
"nil": nil,
"empty_map": MapAny{"a": "", "b": 0},
"valid_map": MapAny{"x": "world", "y": 100},
}
result := m.Shrink()
fmt.Printf("Result: %+v\n", result)
// Example with all empty values
empty := MapAny{"a": "", "b": 0, "c": false, "d": nil}
emptyResult := empty.Shrink()
if emptyResult == nil {
fmt.Printf("Empty result: <nil>\n")
} else {
fmt.Printf("Empty result: %v\n", emptyResult)
}
Output: Result: map[num:42 text:hello true:true valid_map:map[x:world y:100]] Empty result: <nil>
func (MapAny) String ¶
String will return the value as a string, if the underlying type is not a string, it will try and co-oerce it to a string.
func (MapAny) StringSlice ¶
StringSlice returns slice of string values
func (MapAny) TraverseSubMaps ¶ added in v0.13.59
type Measurable ¶
Measurable interface