-
Notifications
You must be signed in to change notification settings - Fork 1
/
props.go
74 lines (64 loc) · 1.58 KB
/
props.go
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
package databeat
import (
"encoding/json"
"fmt"
)
// Props is a sugar type to make it easier to track props
type Props map[string]interface{}
var _ ToEventProps = Props{}
func (p Props) ToEventProps() (map[string]string, map[string]float64, map[string]interface{}) {
strProps := map[string]string{}
numProps := map[string]float64{}
etcProps := map[string]interface{}{}
for k, v := range (map[string]interface{})(p) {
switch t := v.(type) {
case string:
strProps[k] = t
case fmt.Stringer:
strProps[k] = t.String()
case []byte:
strProps[k] = string(t)
case int:
numProps[k] = float64(t)
case int16:
numProps[k] = float64(t)
case int32:
numProps[k] = float64(t)
case int64:
numProps[k] = float64(t)
case uint:
numProps[k] = float64(t)
case uint16:
numProps[k] = float64(t)
case uint32:
numProps[k] = float64(t)
case uint64:
numProps[k] = float64(t)
case float32:
numProps[k] = float64(t)
case float64:
numProps[k] = t
case bool:
strProps[k] = fmt.Sprintf("%v", t)
default:
etcProps[k] = v
}
}
return strProps, numProps, etcProps
}
type ToEventProps interface {
ToEventProps() (map[string]string, map[string]float64, map[string]interface{})
}
func StructToProps(v any) (map[string]string, map[string]float64, map[string]interface{}, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, nil, nil, err
}
var values Props
err = json.Unmarshal(data, &values)
if err != nil {
return nil, nil, nil, err
}
strProps, numProps, etcProps := values.ToEventProps()
return strProps, numProps, etcProps, nil
}