bring back yahoo pipes!
1package transforms
2
3import "strings"
4
5// getNestedValue retrieves a value from a nested map using dot notation
6func getNestedValue(obj map[string]interface{}, path string) interface{} {
7 parts := strings.Split(path, ".")
8 var current interface{} = obj
9
10 for _, part := range parts {
11 if m, ok := current.(map[string]interface{}); ok {
12 current = m[part]
13 } else {
14 return nil
15 }
16 }
17
18 return current
19}
20
21// toFloat attempts to convert various numeric types to float64
22func toFloat(v interface{}) (float64, bool) {
23 switch val := v.(type) {
24 case float64:
25 return val, true
26 case float32:
27 return float64(val), true
28 case int:
29 return float64(val), true
30 case int64:
31 return float64(val), true
32 case int32:
33 return float64(val), true
34 default:
35 return 0, false
36 }
37}