/
mappings.go
163 lines (151 loc) · 4.17 KB
/
mappings.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2016 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package elasticsearch
import (
"encoding/json"
"fmt"
"math"
"strings"
"github.com/openconfig/gnmi/proto/gnmi"
)
// EscapeFieldName escapes field names for Elasticsearch
func EscapeFieldName(name string) string {
return strings.Replace(name, ".", "_", -1)
}
// SetKey fills a Data map's relevant key if the key is a simple type.
func SetKey(m map[string]interface{}, key interface{}) error {
// In the case of gnmi, these will always be strings
if str, ok := key.(string); ok {
m["KeyString"] = &str
return nil
}
return fmt.Errorf("unknown type %v", key)
}
// SetValue fills a Data map's relevant Value fields
func SetValue(m map[string]interface{}, val interface{}) error {
if val == nil {
return nil
}
if str := toStringPtr(val); str != nil {
m["ValueString"] = str
} else if long := toLongPtr(val); long != nil {
m["ValueLong"] = long
} else if bl := toBoolPtr(val); bl != nil {
m["ValueBool"] = bl
} else if dub := toDoublePtr(val); dub != nil {
m["ValueDouble"] = dub
} else if arr := toValueArray(val); arr != nil {
m["Value"] = arr
} else if json, err := toJSONValue(val); err == nil {
switch tv := json.(type) {
case string:
m["ValueString"] = &tv
case int, uint:
m["ValueLong"] = &tv
case bool:
m["ValueBool"] = &tv
case float32:
m["ValueDouble"] = &tv
case float64:
m["ValueDouble"] = &tv
}
} else if bytesVal, ok := val.(*gnmi.TypedValue_BytesVal); ok {
// TODO: handle byte arrays properly (BUG589248)
fmt.Printf("ignoring byte array with string value %s\n", bytesVal.BytesVal)
} else {
// this type may not be supported yet, or could not convert
return fmt.Errorf("unknown type %T for value %v", val, val)
}
return nil
}
// *TypedValue_StringVal
func toStringPtr(val interface{}) *string {
if tv, ok := val.(*gnmi.TypedValue_StringVal); ok {
return &tv.StringVal
}
return nil
}
// *TypedValue_IntVal, *TypedValue_UintVal
func toLongPtr(val interface{}) *int64 {
switch tv := val.(type) {
case *gnmi.TypedValue_IntVal:
val := int64(tv.IntVal)
return &val
case *gnmi.TypedValue_UintVal:
val := int64(tv.UintVal)
return &val
}
return nil
}
// *TypedValue_BoolVal
func toBoolPtr(val interface{}) *bool {
if tv, ok := val.(*gnmi.TypedValue_BoolVal); ok {
return &tv.BoolVal
}
return nil
}
// *TypedValue_FloatVal, *TypedValue_DecimalVal
func toDoublePtr(val interface{}) *float64 {
switch tv := val.(type) {
case *gnmi.TypedValue_FloatVal:
val := float64(tv.FloatVal)
if !math.IsInf(val, 0) && !math.IsNaN(val) {
return &val
}
case *gnmi.TypedValue_DoubleVal:
val := float64(tv.DoubleVal)
if !math.IsInf(val, 0) && !math.IsNaN(val) {
return &val
}
case *gnmi.TypedValue_DecimalVal:
// convert to float64 for now
val := float64(tv.DecimalVal.Digits)
for i := 0; i < int(tv.DecimalVal.Precision); i++ {
val /= 10
}
if !math.IsInf(val, 0) && !math.IsNaN(val) {
return &val
}
}
return nil
}
// Flatten a non-simple type into a []*field
func toValueArray(val interface{}) []*map[string]interface{} {
if tv, ok := val.(*gnmi.TypedValue_LeaflistVal); ok {
elements := tv.LeaflistVal.Element
fields := make([]*map[string]interface{}, len(elements))
// LeaflistVal should only have simple types
for i, el := range elements {
m := make(map[string]interface{})
if str := toStringPtr(el.Value); str != nil {
m["String"] = str
} else if long := toLongPtr(el.Value); long != nil {
m["Long"] = long
} else if bl := toBoolPtr(el.Value); bl != nil {
m["Bool"] = bl
} else if dub := toDoublePtr(el.Value); dub != nil {
m["Double"] = dub
} else {
// this type is not supported yet
return nil
}
fields[i] = &m
}
return fields
}
return nil
}
// *TypedValue_JsonVal, *TypedValue_JsonIetfVal
func toJSONValue(val interface{}) (interface{}, error) {
var out interface{}
if tv, ok := val.(*gnmi.TypedValue_JsonVal); ok {
err := json.Unmarshal(tv.JsonVal, &out)
return out, err
}
if tv, ok := val.(*gnmi.TypedValue_JsonIetfVal); ok {
err := json.Unmarshal(tv.JsonIetfVal, &out)
return out, err
}
return nil, nil
}