Skip to content

Commit db0d557

Browse files
authored
Merge pull request #85 from eranra/fix_decode_json
JSON decode: keep value types, do not convert to string
2 parents 1a937f1 + a3fc2ba commit db0d557

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

pkg/pipeline/decode/decode_json.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package decode
1919

2020
import (
2121
"encoding/json"
22-
"fmt"
2322
"github.com/netobserv/flowlogs2metrics/pkg/config"
2423
log "github.com/sirupsen/logrus"
2524
)
@@ -45,9 +44,7 @@ func (c *decodeJson) Decode(in []interface{}) []config.GenericMap {
4544
if v == nil {
4645
continue
4746
}
48-
// ensure we have a string variable
49-
s := fmt.Sprintf("%v", v)
50-
decodedLine2[k] = s
47+
decodedLine2[k] = v
5148
}
5249
out = append(out, decodedLine2)
5350
}

pkg/pipeline/decode/decode_json_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,23 @@ func TestDecodeJson(t *testing.T) {
4646
in = append(in, inputStringErr)
4747
out = decodeJson.Decode(in)
4848
require.Equal(t, len(out), 3)
49-
// verify that all items come back as strings
50-
require.Equal(t, "12", out[0]["varInt"])
49+
require.Equal(t, float64(12), out[0]["varInt"])
5150
require.Equal(t, "testString", out[0]["varString"])
52-
require.Equal(t, "false", out[0]["varBool"])
51+
require.Equal(t, bool(false), out[0]["varBool"])
5352

5453
// TODO: Check for more complicated json structures
5554
}
55+
56+
func TestDecodeJsonTimestamps(t *testing.T) {
57+
newDecode := initNewDecodeJson(t)
58+
decodeJson := newDecode.(*decodeJson)
59+
inputString1 := "{\"unixTime\": 1645104030 }"
60+
var in []interface{}
61+
var out []config.GenericMap
62+
out = decodeJson.Decode(in)
63+
require.Equal(t, 0, len(out))
64+
in = append(in, inputString1)
65+
out = decodeJson.Decode(in)
66+
require.Equal(t, len(out), 1)
67+
require.Equal(t, float64(1645104030), out[0]["unixTime"])
68+
}

pkg/pipeline/transform/transform_generic_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func getGenericExpectedOutput() config.GenericMap {
5050
return config.GenericMap{
5151
"SrcAddr": "10.0.0.1",
5252
"srcIP": "10.0.0.1",
53-
"SrcPort": "11777",
53+
"SrcPort": 11777,
5454
"Protocol": "tcp",
5555
"DstAddr": "20.0.0.2",
56-
"DstPort": "22",
56+
"DstPort": 22,
5757
}
5858
}
5959

pkg/pipeline/transform/transform_multiple_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ pipeline:
6161
func getMultipleExpectedOutput() config.GenericMap {
6262
return config.GenericMap{
6363
"SrcAddr2": "10.0.0.1",
64-
"SrcPort2": "11777",
64+
"SrcPort2": 11777,
6565
"Protocol2": "tcp",
6666
"DstAddr2": "20.0.0.2",
67-
"DstPort2": "22",
67+
"DstPort2": 22,
6868
}
6969
}
7070

pkg/pipeline/transform/transform_network_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ func getExpectedOutput() config.GenericMap {
9696
return config.GenericMap{
9797
"level": "error",
9898
"protocol": "tcp",
99-
"protocol_num": "6",
99+
"protocol_num": 6,
100100
"message": "test message",
101101
"dstIP": "20.0.0.2",
102-
"dstPort": "22",
102+
"dstPort": 22,
103103
"srcIP": "10.0.0.1",
104104
"subnet16SrcIP": "10.0.0.0/16",
105105
"subnet24SrcIP": "10.0.0.0/24",
@@ -109,7 +109,7 @@ func getExpectedOutput() config.GenericMap {
109109
"smaller_than_10_Evaluate": true,
110110
"service": "ssh",
111111
"service_protocol_num": "ssh",
112-
"srcPort": "11777",
112+
"srcPort": 11777,
113113
"8888IP": "8.8.8.8",
114114
"8888IP_location_CountryName": "US",
115115
"8888IP_location_CountryLongName": "United States of America",

pkg/test/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ func GetIngestMockEntry(missingKey bool) config.GenericMap {
3131
"8888IP": "8.8.8.8",
3232
"emptyIP": "",
3333
"level": "error",
34-
"srcPort": "11777",
34+
"srcPort": 11777,
3535
"protocol": "tcp",
36-
"protocol_num": "6",
36+
"protocol_num": 6,
3737
"value": "7",
3838
"message": "test message",
3939
}
4040

4141
if !missingKey {
4242
entry["dstIP"] = "20.0.0.2"
43-
entry["dstPort"] = "22"
43+
entry["dstPort"] = 22
4444
}
4545

4646
return entry

0 commit comments

Comments
 (0)