Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bson.D conversion in GRPC #2083

Merged
merged 5 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/storage/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func TestRoundTripDataOverGRPC(t *testing.T) {
err = client.Insert(ctx, plugins.InsertOptions{
Collection: CollectionOutputs,
Documents: []bson.M{{"namespace": "dev", "installation": "test", "name": "thing1", "resultId": "111"},
{"namespace": "dev", "installation": "test", "name": "thing2", "resultId": "222"}},
{"namespace": "dev", "installation": "test", "name": "thing2", "resultId": "111"},
{"namespace": "dev", "installation": "test", "name": "thing2", "resultId": "222"},
},
})
require.NoError(t, err)

Expand All @@ -106,10 +108,12 @@ func TestRoundTripDataOverGRPC(t *testing.T) {
// Group them by output name and select the last value for each output
{{"$group", bson.D{
{"_id", "$name"},
{"lastOutput", bson.M{"$first": "$ROOT"}},
{"lastOutput", bson.M{"$first": "$$ROOT"}},
}}},
},
})
require.NoError(t, err)
require.Len(t, aggregateResults, 2)
// make sure the group function is picking the most recent output value with the same name
require.Contains(t, aggregateResults[0].Lookup("lastOutput").String(), "222")
}
26 changes: 7 additions & 19 deletions pkg/storage/pluginstore/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,18 @@ func ConvertBsonToPrimitives(src interface{}) interface{} {
// deserialize from protobuf, this walks the specified value, finds ints
// that were encoded as floats, and converts them back to ints.
func ConvertSliceToBsonD(src interface{}) interface{} {
dest := ConvertFloatToInt(src)
switch tv := dest.(type) {
switch tv := src.(type) {
case []interface{}:
toBson := make(bson.D, 0, len(tv))
for i, item := range tv {
for _, item := range tv {
converted := ConvertSliceToBsonD(item)
if m, ok := converted.(map[string]interface{}); ok {
for k, v := range m {
toBson = append(toBson, bson.E{Key: k, Value: v})
}
continue
}
tv[i] = converted
}
if len(toBson) > 0 {
return toBson
}
return tv
return toBson
case map[string]interface{}:
for k, v := range tv {
tv[k] = ConvertSliceToBsonD(v)
Expand Down Expand Up @@ -394,15 +388,12 @@ func NewStruct(src map[string]interface{}) *structpb.Struct {
// AsMap converts a protobuf struct into its original representation, bson.M.
func AsMap(src *structpb.Struct, c ...converter) bson.M {
dest := src.AsMap()
converts := []converter{ConvertFloatToInt}
if c != nil {
converts = append(converts, c...)
}
for k, v := range dest {
for _, convert := range converts {
v = convert(v)
converted := ConvertFloatToInt(v)
for _, convert := range c {
converted = convert(v)
}
dest[k] = v
dest[k] = converted
}
return dest
}
Expand All @@ -421,9 +412,6 @@ type converter func(src interface{}) interface{}
// representation, bson.D.
func AsOrderedMap(src []*structpb.Struct, c ...converter) bson.D {
dest := make(bson.D, 0, len(src))
if c == nil {
c = []converter{ConvertFloatToInt}
}
for _, item := range src {
for k, v := range AsMap(item, c...) {
dest = append(dest, bson.E{Key: k, Value: v})
Expand Down