Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/github.com/spf13/vip…
Browse files Browse the repository at this point in the history
…er-1.8.1
  • Loading branch information
alfrunes committed Jul 5, 2021
2 parents 752dc46 + ba9fb4b commit 82206b0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
19 changes: 14 additions & 5 deletions mongo/doc/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

// DocumentFromStruct creates a bson document from a struct in the order of the
// underlying data structure. Additional fields can be appended to the struct
// with the appendElements, these fields will be added at the end of the
// document.
func DocumentFromStruct(
// MarshallBSONOrDocumentFromStruct marshals a structure to BSON if it implements
// the bson.Marshaler interface, otherwise invokes DocumentFromStruct on it.
func MarshallBSONOrDocumentFromStruct(
sct interface{},
appendElements ...bson.E,
) (doc bson.D) {
Expand All @@ -37,6 +35,17 @@ func DocumentFromStruct(
}
return doc
}
return DocumentFromStruct(sct, appendElements...)
}

// DocumentFromStruct creates a bson document from a struct in the order of the
// underlying data structure. Additional fields can be appended to the struct
// with the appendElements, these fields will be added at the end of the
// document.
func DocumentFromStruct(
sct interface{},
appendElements ...bson.E,
) (doc bson.D) {
s := reflect.ValueOf(sct)
defer func() {
if r := recover(); r != nil {
Expand Down
63 changes: 62 additions & 1 deletion mongo/doc/bson_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// Copyright 2021 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,67 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

type SampleBSONMarshalerStruct struct {
Value bson.D
}

func (r *SampleBSONMarshalerStruct) MarshalBSON() ([]byte, error) {
return bson.Marshal(r.Value)
}

func TestMarshallBSONOrDocumentFromStruct(t *testing.T) {
testCases := []struct {
Name string

Input interface{}
AppendElements []bson.E
Expected bson.D
}{
{
Name: "Simple success",

Input: struct {
Field1 string
Field2 int
}{
Field1: "foo",
Field2: 321,
},
Expected: bson.D{
{Key: "field1", Value: "foo"},
{Key: "field2", Value: 321},
},
},
{
Name: "bson.Marshaler interface",

Input: &SampleBSONMarshalerStruct{
Value: bson.D{
{Key: "field", Value: "value"},
},
},
Expected: bson.D{
{Key: "field", Value: "value"},
},
},
{
Name: "Not a struct",

Input: "Panic attack!",
Expected: nil,
},
}

t.Parallel()
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
doc := MarshallBSONOrDocumentFromStruct(tc.Input, tc.AppendElements...)
assert.Equal(t, tc.Expected, doc)
})
}

}

func TestDocumentFromStruct(t *testing.T) {
testCases := []struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion store/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func WithTenantID(ctx context.Context, doc interface{}) bson.D {
case bson.D:
res = append(res, v...)
default:
if bsonData := mdoc.DocumentFromStruct(v); bsonData != nil {
if bsonData := mdoc.MarshallBSONOrDocumentFromStruct(v); bsonData != nil {
res = append(res, bsonData...)
}
}
Expand Down

0 comments on commit 82206b0

Please sign in to comment.