File size: 892 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package models
import (
"fmt"
"maps"
"reflect"
)
type Attributes map[any]any
func (a Attributes) Clone() Attributes {
return maps.Clone(a)
}
// AsStringMap converts Attributes into a map[string]any by:
// - keeping string keys as-is
// - stringifying comparable non-string keys as "<type>:<value>"
func (a Attributes) AsStringMap() map[string]any {
if len(a) == 0 {
return nil
}
out := make(map[string]any, len(a))
for k, v := range a {
if sk, ok := k.(string); ok {
out[sk] = v
continue
}
t := reflect.TypeOf(k)
if t == nil {
continue
}
if t.Comparable() {
key := fmt.Sprintf("%T:%v", k, k)
out[key] = v
}
}
return out
}
func (a Attributes) Merge(m Attributes) Attributes {
if len(m) == 0 {
return a.Clone()
}
r := make(Attributes, len(a)+len(m))
for k, v := range a {
r[k] = v
}
for k, v := range m {
r[k] = v
}
return r
}
|