Skip to content

Commit 5b05297

Browse files
committed
Events, generalizar para cualquier tipo de map
Permitirmos que se agrupen eventos siempre que el tipo de dato almacenado en Metadata.Key sea un map.
1 parent 223fc57 commit 5b05297

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

gremlin/traversal/events.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package traversal
22

33
import (
4+
"fmt"
45
"reflect"
56
"time"
67

@@ -18,7 +19,7 @@ type EventsTraversalExtension struct {
1819
// EventsGremlinTraversalStep step aggregates events from different revisions of the nodes into a new metadata key.
1920
// This step should be used with a presistant backend, so it can access previous revisions of the nodes.
2021
// To use this step we should select a metadata key (first parameter), where the events will be read from.
21-
// Inside this Metadata.Key events should have the format map[string]interface{}.
22+
// Inside this Metadata.Key events should have the format map[interface{}]interface{} (could be a type based on that).
2223
// The second parameter is the metadata key where all the events will be aggregated.
2324
// The aggregation will with the format: map[string][]interface{}.
2425
// All events with the same key in the map will be joined in an slice.
@@ -202,21 +203,40 @@ func (s *EventsGremlinTraversalStep) InterfaceEvents(tv *traversal.GraphTraversa
202203
// mergeEvents return the merge of node.Key events with the ones already stored in nodeEvents
203204
// Eg.:
204205
// node: Metadata.key: {"a":{x}, "b":{y}}
205-
// nodeEvents: {"a":{z}}
206+
// nodeEvents: {"a":[{z}]}
206207
// return: Metadata.key: {"a":[{x},{z}], "b":[{y}]}
207208
//
208-
// Ignore if Metadata.key has an invalid format.
209+
// Ignore if Metadata.key has an invalid format (not a map).
210+
// Reflect is used to be able to access map's defined in different types.
211+
// Event aggregation data type should be map[string]interface{} to be able to be encoded with JSON
209212
func mergeEvents(node *graph.Node, key string, nodeEvents map[string][]interface{}) map[string][]interface{} {
210213
if nodeEvents == nil {
211214
nodeEvents = map[string][]interface{}{}
212215
}
213216

214217
n1EventsIface, n1Err := node.GetField(key)
218+
215219
if n1Err == nil {
216-
// Ignore Metadata.key values with not a valid format
217-
n1Events, _ := n1EventsIface.(map[string]interface{})
220+
// Ignore Metadata.key values which are not a map
221+
n1EventsValue := reflect.ValueOf(n1EventsIface)
222+
223+
// If the metadata value is a pointer, resolve it
224+
if n1EventsValue.Kind() == reflect.Ptr {
225+
n1EventsValue = n1EventsValue.Elem()
226+
}
227+
228+
// Events step only accepts a map as data origin
229+
if n1EventsValue.Kind() != reflect.Map {
230+
logging.GetLogger().Errorf("Invalid type for events, expecting a map, but it is %v", n1EventsValue.Kind())
231+
return nodeEvents
232+
}
233+
234+
iter := n1EventsValue.MapRange()
218235
NODE_EVENTS:
219-
for k, v := range n1Events {
236+
for iter.Next() {
237+
k := fmt.Sprintf("%v", iter.Key().Interface())
238+
v := iter.Value().Interface()
239+
220240
// Do not append if the same event already exists
221241
for _, storedEvent := range nodeEvents[k] {
222242
if reflect.DeepEqual(storedEvent, v) {

gremlin/traversal/events_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,36 @@ func TestMergeEventsNilNodeEvents(t *testing.T) {
4545
key := "Events"
4646

4747
metadataNode1 := graph.Metadata{key: map[string]interface{}{
48-
"abc": map[string]string{"descr": "foo"},
48+
"abc": map[interface{}]string{"descr": "foo"},
4949
}}
5050
node := CreateNode("nodeA", metadataNode1, graph.TimeUTC(), 1)
5151

5252
nodeEventsAgg := mergeEvents(node, key, nil)
5353

54-
expected := map[string][]interface{}{
54+
expected := map[interface{}][]interface{}{
5555
"abc": {
56-
map[string]string{"descr": "foo"},
56+
map[interface{}]string{"descr": "foo"},
57+
},
58+
}
59+
60+
assert.Equal(t, expected, nodeEventsAgg)
61+
}
62+
63+
func TestMergeEventsPointerValue(t *testing.T) {
64+
key := "Events"
65+
66+
value := map[string]interface{}{
67+
"abc": map[interface{}]string{"descr": "foo"},
68+
}
69+
70+
metadataNode1 := graph.Metadata{key: &value}
71+
node := CreateNode("nodeA", metadataNode1, graph.TimeUTC(), 1)
72+
73+
nodeEventsAgg := mergeEvents(node, key, nil)
74+
75+
expected := map[interface{}][]interface{}{
76+
"abc": {
77+
map[interface{}]string{"descr": "foo"},
5778
},
5879
}
5980

@@ -64,19 +85,19 @@ func TestMergeEvents(t *testing.T) {
6485
tests := []struct {
6586
name string
6687
nodesEvents []interface{}
67-
expected map[string][]interface{}
88+
expected map[interface{}][]interface{}
6889
}{
6990
{
7091
name: "no nodes",
71-
expected: map[string][]interface{}{},
92+
expected: map[interface{}][]interface{}{},
7293
},
7394
{
7495
name: "one node",
7596
nodesEvents: []interface{}{
7697
map[string]interface{}{
7798
"abc": map[string]string{"descr": "foo"},
7899
}},
79-
expected: map[string][]interface{}{
100+
expected: map[interface{}][]interface{}{
80101
"abc": {
81102
map[string]string{"descr": "foo"},
82103
},
@@ -91,7 +112,7 @@ func TestMergeEvents(t *testing.T) {
91112
map[string]interface{}{
92113
"xyz": map[string]string{"descr": "bar"},
93114
}},
94-
expected: map[string][]interface{}{
115+
expected: map[interface{}][]interface{}{
95116
"abc": {
96117
map[string]string{"descr": "foo"},
97118
},
@@ -109,7 +130,7 @@ func TestMergeEvents(t *testing.T) {
109130
map[string]interface{}{
110131
"abc": map[string]string{"descr": "bar"},
111132
}},
112-
expected: map[string][]interface{}{
133+
expected: map[interface{}][]interface{}{
113134
"abc": {
114135
map[string]string{"descr": "foo"},
115136
map[string]string{"descr": "bar"},
@@ -126,7 +147,7 @@ func TestMergeEvents(t *testing.T) {
126147
"abc": map[string]string{"descr": "foo"},
127148
"xxx": map[string]string{"descr": "bar"},
128149
}},
129-
expected: map[string][]interface{}{
150+
expected: map[interface{}][]interface{}{
130151
"abc": {
131152
map[string]string{"descr": "foo"},
132153
},
@@ -141,7 +162,7 @@ func TestMergeEvents(t *testing.T) {
141162

142163
for _, test := range tests {
143164
t.Run(test.name, func(t *testing.T) {
144-
nodeEventsAgg := map[string][]interface{}{}
165+
nodeEventsAgg := map[interface{}][]interface{}{}
145166

146167
for _, nodeEvents := range test.nodesEvents {
147168
metadataNode1 := graph.Metadata{key: nodeEvents}

0 commit comments

Comments
 (0)