Skip to content

Commit

Permalink
Fix/random map event (#54)
Browse files Browse the repository at this point in the history
* encode random-map-interface objects

* add comment and test case
  • Loading branch information
tardunge authored Dec 7, 2020
1 parent 52b9e53 commit f397161
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions client/golang/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (e *encoder) encodeValue(v interface{}) (*pb.Value, error) {
case json.RawMessage:
valueRef := e.updateDict(string(val))
return &pb.Value{Value: &pb.Value_Json{Json: valueRef}}, nil
case map[string]interface{}: // If a map is provided, it is converted to json string before encoding.
b, err := json.Marshal(val)
if err != nil {
return nil, err
}
valueRef := e.updateDict(string(b))
return &pb.Value{Value: &pb.Value_Json{Json: valueRef}}, nil
case nil: // The field is not set.
return nil, nil
default:
Expand Down
19 changes: 17 additions & 2 deletions client/golang/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,31 @@ func TestEncoder(t *testing.T) {
"color": "yellow",
"topic": "movie",
},
{
"event": "xyz",
"nested": map[string]interface{}{
"level0": 0,
"level1": map[string]interface{}{
"test": 1,
"level2": []int{
1,
2,
3,
},
},
},
},
}

encoder := newEncoder()
res := encoder.Encode(testEvents)
assert.Len(t, res.Events, 7)
assert.Len(t, res.Strings, 13)
assert.Len(t, res.Events, 8)
assert.Len(t, res.Strings, 15)
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{1: {Value: &pb.Value_String_{String_: 2}}}}, res.Events[0])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{3: {Value: &pb.Value_Int64{Int64: 123}}}}, res.Events[1])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{4: {Value: &pb.Value_Bool{Bool: true}}}}, res.Events[2])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{5: {Value: &pb.Value_Time{Time: testTime.Unix()}}}}, res.Events[3])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{6: {Value: &pb.Value_Json{Json: 7}}}}, res.Events[4])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{6: {Value: &pb.Value_String_{String_: 8}}}}, res.Events[5])
assert.Equal(t, &pb.Event{Value: map[uint32]*pb.Value{1: {Value: &pb.Value_String_{String_: 9}}, 14: {Value: &pb.Value_Json{Json: 15}}}}, res.Events[7])
}

0 comments on commit f397161

Please sign in to comment.