File size: 1,964 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | package models
import (
"math"
"reflect"
"github.com/brunoga/deep"
)
type Annotations map[string]interface{}
func (a Annotations) GetBool(key string) bool {
if len(a) == 0 {
return false
}
val, ok := a[key]
if !ok {
return false
}
boolVal, ok := val.(bool)
if !ok {
return false
}
return boolVal
}
func (a Annotations) GetString(key string) (string, bool) {
if len(a) == 0 {
return "", false
}
val, ok := a[key]
if !ok {
return "", false
}
strVal, ok := val.(string)
if !ok {
return "", false
}
return strVal, true
}
func (a Annotations) GetInt(key string) (int, bool) {
if len(a) == 0 {
return 0, false
}
val, ok := a[key]
if !ok {
return 0, false
}
switch t := val.(type) {
case int:
return t, true
case float32:
f := float64(t)
if f != math.Trunc(f) || float64(math.MaxInt) < f || f < float64(math.MinInt) {
return 0, false
}
return int(t), true
case float64:
if t != math.Trunc(t) || float64(math.MaxInt) < t || t < float64(math.MinInt) {
return 0, false
}
return int(t), true
default:
return 0, false
}
}
func (a Annotations) Reset() {
for k := range a {
delete(a, k)
}
}
func (a Annotations) Clone() (Annotations, error) {
if a == nil {
return nil, nil
}
return deep.Copy[Annotations](a)
}
func (a Annotations) Merge(m Annotations) (Annotations, error) {
if a == nil {
return m, nil
}
result, err := a.Clone()
if err != nil {
return nil, err
}
if len(m) == 0 {
return result, nil
}
for k, v := range m {
vv, err := deep.Copy(v)
if err != nil {
return nil, err
}
result[k] = vv
}
return result, nil
}
func (a Annotations) Equal(other Annotations) bool {
if a == nil || other == nil {
return a == nil && other == nil
}
if len(a) != len(other) {
return false
}
for k, v := range a {
otherV, ok := other[k]
if !ok {
return false
}
if !reflect.DeepEqual(v, otherV) {
return false
}
}
return true
}
|