Skip to content

Commit 19426ae

Browse files
committed
FFM-2514 add function to convert various types to string
1 parent 842ac1a commit 19426ae

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

analyticsservice/analytics.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strconv"
78
"sync"
89
"time"
910

@@ -116,6 +117,34 @@ func (as *AnalyticsService) listener() {
116117
}
117118
}
118119

120+
func convertInterfaceToString(i interface{}) string {
121+
if i == nil {
122+
return "nil"
123+
}
124+
125+
switch v := i.(type) {
126+
case string:
127+
return v
128+
case int:
129+
return strconv.Itoa(v)
130+
case int64:
131+
return strconv.FormatInt(v, 10)
132+
case float64:
133+
return fmt.Sprintf("%v", v)
134+
case float32:
135+
return fmt.Sprintf("%v", v)
136+
case bool:
137+
val := "false"
138+
if v {
139+
val = "true"
140+
}
141+
return val
142+
default:
143+
// As a last resort
144+
return fmt.Sprintf("%v", v)
145+
}
146+
}
147+
119148
func (as *AnalyticsService) sendDataAndResetCache(ctx context.Context) {
120149
as.mx.Lock()
121150
// copy cache to send to server
@@ -136,7 +165,7 @@ func (as *AnalyticsService) sendDataAndResetCache(ctx context.Context) {
136165
if analytic.target.Attributes != nil {
137166
targetAttributes = make([]metricsclient.KeyValue, 0, len(*analytic.target.Attributes))
138167
for key, value := range *analytic.target.Attributes {
139-
v, _ := value.(string)
168+
v := convertInterfaceToString(value)
140169
kv := metricsclient.KeyValue{
141170
Key: key,
142171
Value: v,

analyticsservice/analytics_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package analyticsservice
2+
3+
import "testing"
4+
5+
func Test_convertInterfaceToString(t *testing.T) {
6+
testCases := map[string]struct {
7+
input interface{}
8+
expected string
9+
}{
10+
"Given I input a string": {
11+
input: "123",
12+
expected: "123",
13+
},
14+
"Given I input a bool with the value false": {
15+
input: false,
16+
expected: "false",
17+
},
18+
"Given I input a bool with the value true": {
19+
input: true,
20+
expected: "true",
21+
},
22+
"Given I input an int64": {
23+
input: int64(123),
24+
expected: "123",
25+
},
26+
"Given I input an int": {
27+
input: 123,
28+
expected: "123",
29+
},
30+
"Given I input a float64": {
31+
input: float64(2.5),
32+
expected: "2.5",
33+
},
34+
"Given I input a float32": {
35+
input: float32(2.5),
36+
expected: "2.5",
37+
},
38+
"Given I input a nil value": {
39+
input: nil,
40+
expected: "nil",
41+
},
42+
}
43+
44+
for desc, tc := range testCases {
45+
tc := tc
46+
t.Run(desc, func(t *testing.T) {
47+
48+
actual := convertInterfaceToString(tc.input)
49+
if actual != tc.expected {
50+
t.Errorf("(%s): expected %s, actual %s", desc, tc.expected, actual)
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)