Documentation
¶
Overview ¶
Package values provides generic any-to-typed conversion and MapAny utilities.
Index ¶
- func Bool(v any) bool
- func BoolToBytes(v bool) []byte
- func CanonicalizeJSON(input []byte) ([]byte, error)
- 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 IntToBytes(v int) []byte
- 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 MarshalCanonicalJSON(v any) ([]byte, error)
- func NumbersCoalesce[T ~int | ~int32 | ~uint | ~uint32 | ~int64 | ~uint64](items ...T) T
- 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
- func Uint32ToBytes(v uint32) []byte
- func Uint64ToBytes(v uint64) []byte
- func WriteCanonicalJSON(buf *bytes.Buffer, v any) error
- func XXH3Hash64(data []byte) uint64
- func XXH3Hash128(data []byte) []byte
- func XXH3Hash128Hex(data []byte) string
- func XXH3HashArgs64(data ...any) uint64
- func XXH3HashArgs128(data ...any) []byte
- func XXH3HashArgs128Hex(data ...any) string
- func XXH3HashString64(data string) uint64
- type HasDisplayName
- type HasDisplayNames
- type HasDisplayNamesMap
- type HasName
- type HasNames
- type HasNamesMap
- type HasValuesMap
- type MapAny
- func (c MapAny) Add(key string, value any) MapAny
- func (c MapAny) Bool(k string) bool
- func (c MapAny) CanonicalJSON() ([]byte, error)
- 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) Keys() []string
- func (c MapAny) Map(k string) MapAny
- func (c *MapAny) Merge(m MapAny) *MapAny
- func (c MapAny) OrderedKeys() []string
- func (c MapAny) OrderedRange(f func(k string, v any) bool)
- func (c MapAny) Range(f func(k string, v any) bool)
- func (c MapAny) RenameKeys(keyName func(key string) string) 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 BoolToBytes ¶ added in v0.16.88
BoolToBytes returns []byte{1} or []byte{0}. It is used for deterministic hashing of booleans.
func CanonicalizeJSON ¶ added in v0.15.83
CanonicalizeJSON returns the canonical JSON representation of the input, with all object keys sorted alphabetically.
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 IntToBytes ¶ added in v0.16.88
func JSONIndent ¶ added in v0.12.54
JSONIndent returns the value as a JSON string with indentation
func MarshalCanonicalJSON ¶ added in v0.15.84
MarshalCanonicalJSON returns the canonical JSON representation of the value, with all object keys sorted alphabetically.
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 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
func Uint32ToBytes ¶ added in v0.16.88
Uint32ToBytes returns v in big-endian order (4 bytes). It is used for deterministic hashing of integers.
func Uint64ToBytes ¶ added in v0.16.88
Uint64ToBytes returns v in big-endian order (8 bytes). It is used for deterministic hashing of integers.
func WriteCanonicalJSON ¶ added in v0.15.84
WriteCanonicalJSON writes the canonical JSON representation of the input to the buffer. All keys are sorted alphabetically, at any nesting level.
Values that are not handled natively, such as structs, typed maps and slices, json.RawMessage or custom json.Marshaler implementations, are encoded with encoding/json and then re-encoded from the resulting JSON, as encoding/json preserves the declaration order of struct fields and the original order of raw JSON.
func XXH3Hash64 ¶ added in v0.16.88
XXH3Hash64 returns the 64-bit XXH3 digest of data. It is fast and non-cryptographic.
func XXH3Hash128 ¶ added in v0.16.88
XXH3Hash128 returns the 128-bit XXH3 digest of data as 16 bytes in canonical big-endian form (see xxh3.Uint128.Bytes). It is fast and non-cryptographic.
func XXH3Hash128Hex ¶ added in v0.16.88
XXH3Hash128Hex returns the lowercase hexadecimal encoding of Hash128(data) (32 characters).
func XXH3HashArgs64 ¶ added in v0.16.90
XXH3HashArgs64 returns a 64-bit XXH3 digest over variadic arguments.
Supported types: string, []string, numeric scalars and slices (uint64, int64, uint32, int32, int), bool and []bool, []byte, enum.ProtoEnum and []enum.ProtoEnum. Other values are serialized with String(v) (see package values). String and []string elements are followed by a zero byte delimiter so adjacent fields do not merge ambiguously. Slices of fixed-width types are written without per-element delimiters; argument order and Go types distinguish fields.
func XXH3HashArgs128 ¶ added in v0.16.90
XXH3HashArgs128 returns a 128-bit XXH3 digest over variadic arguments.
Supported types: string, []string, numeric scalars and slices (uint64, int64, uint32, int32, int), bool and []bool, []byte, enum.ProtoEnum and []enum.ProtoEnum. Other values are serialized with String(v) (see package values). String and []string elements are followed by a zero byte delimiter so adjacent fields do not merge ambiguously. Slices of fixed-width types are written without per-element delimiters; argument order and Go types distinguish fields.
func XXH3HashArgs128Hex ¶ added in v0.16.88
XXH3HashArgs128Hex returns a 128-bit XXH3 digest over variadic arguments, encoded as lowercase hex (32 characters).
Supported types: string, []string, numeric scalars and slices (uint64, int64, uint32, int32, int), bool and []bool, []byte, enum.ProtoEnum and []enum.ProtoEnum. Other values are serialized with String(v) (see package values). String and []string elements are followed by a zero byte delimiter so adjacent fields do not merge ambiguously. Slices of fixed-width types are written without per-element delimiters; argument order and Go types distinguish fields.
func XXH3HashString64 ¶ added in v0.16.88
XXH3HashString64 returns the 64-bit XXH3 digest of the string's UTF-8 bytes. It is fast and non-cryptographic.
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 CastMapAny ¶ added in v0.15.85
CastMapAny casts any to MapAny, returns nil if the value is not a map[string]any or MapAny
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) CanonicalJSON ¶ added in v0.15.83
func (MapAny) JSONIndent ¶ added in v0.12.54
func (MapAny) OrderedKeys ¶ added in v0.16.91
func (MapAny) OrderedRange ¶ added in v0.16.91
OrderedRange range over map keys in order
func (MapAny) RenameKeys ¶ added in v0.15.85
RenameKeys renames the keys of the map recursively using the function f.
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
TraverseSubMaps traverses the map and its sub-maps, calling the function f for each sub-map.
type Measurable ¶
Measurable interface