Skip to content

Implement BSON Marshaler support #142

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/deckarep/golang-set/v2

go 1.18

require go.mongodb.org/mongo-driver v1.16.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4=
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
11 changes: 10 additions & 1 deletion set.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ SOFTWARE.
// that can enforce mutual exclusion through other means.
package mapset

import "go.mongodb.org/mongo-driver/bson/bsontype"

// Set is the primary interface provided by the mapset package. It
// represents an unordered set of data and a large number of
// operations that can be applied to that set.
Expand Down Expand Up @@ -192,8 +194,15 @@ type Set[T comparable] interface {
MarshalJSON() ([]byte, error)

// UnmarshalJSON will unmarshal a JSON-based byte slice into a full Set datastructure.
// For this to work, set subtypes must implemented the Marshal/Unmarshal interface.
// For this to work, set subtypes must implement the Marshal/Unmarshal interface.
UnmarshalJSON(b []byte) error

// MarshalBSONValue will marshal the set into a BSON-based representation.
MarshalBSONValue() (bsontype.Type, []byte, error)

// UnmarshalBSONValue will unmarshal a BSON-based byte slice into a full Set datastructure.
// For this to work, set subtypes must implement the Marshal/Unmarshal interface.
UnmarshalBSONValue(bt bsontype.Type, b []byte) error
}

// NewSet creates and returns a new set with the given elements.
Expand Down
24 changes: 22 additions & 2 deletions threadsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ SOFTWARE.

package mapset

import "sync"
import (
"sync"

"go.mongodb.org/mongo-driver/bson/bsontype"
)

type threadSafeSet[T comparable] struct {
sync.RWMutex
Expand Down Expand Up @@ -291,9 +295,25 @@ func (t *threadSafeSet[T]) MarshalJSON() ([]byte, error) {
}

func (t *threadSafeSet[T]) UnmarshalJSON(p []byte) error {
t.RLock()
t.Lock()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnmarshalJSON should be a write lock - unlikely to cause issues in practice (with a newly-initialized Set), but an inconsistency nonetheless.

err := t.uss.UnmarshalJSON(p)
t.Unlock()

return err
}

func (t *threadSafeSet[T]) MarshalBSONValue() (bsontype.Type, []byte, error) {
t.RLock()
bt, b, err := t.uss.MarshalBSONValue()
t.RUnlock()

return bt, b, err
}

func (t *threadSafeSet[T]) UnmarshalBSONValue(bt bsontype.Type, p []byte) error {
t.Lock()
err := t.uss.UnmarshalBSONValue(bt, p)
t.Unlock()

return err
}
78 changes: 78 additions & 0 deletions threadsafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"sync"
"sync/atomic"
"testing"

"go.mongodb.org/mongo-driver/bson"
)

const N = 1000
Expand Down Expand Up @@ -625,3 +627,79 @@ func Test_MarshalJSON(t *testing.T) {
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
}
}

func Test_UnmarshalBSONValue(t *testing.T) {
tp, s, initErr := bson.MarshalValue(
bson.A{"1", "2", "3", "test"},
)

if initErr != nil {
t.Errorf("Init Error should be nil: %v", initErr)

return
}

if tp != bson.TypeArray {
t.Errorf("Encoded Type should be bson.Array, got: %v", tp)

return
}

expected := NewSet("1", "2", "3", "test")
actual := NewSet[string]()
err := bson.UnmarshalValue(bson.TypeArray, s, actual)
if err != nil {
t.Errorf("Error should be nil: %v", err)
}

if !expected.Equal(actual) {
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
}
}
func TestThreadUnsafeSet_UnmarshalBSONValue(t *testing.T) {
tp, s, initErr := bson.MarshalValue(
bson.A{int64(1), int64(2), int64(3)},
)

if initErr != nil {
t.Errorf("Init Error should be nil: %v", initErr)

return
}

if tp != bson.TypeArray {
t.Errorf("Encoded Type should be bson.Array, got: %v", tp)

return
}

expected := NewThreadUnsafeSet[int64](1, 2, 3)
actual := NewThreadUnsafeSet[int64]()
err := actual.UnmarshalBSONValue(bson.TypeArray, []byte(s))
if err != nil {
t.Errorf("Error should be nil: %v", err)
}
if !expected.Equal(actual) {
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
}
}
func Test_MarshalBSONValue(t *testing.T) {
expected := NewSet("1", "test")

_, b, err := bson.MarshalValue(
NewSet("1", "test"),
)
if err != nil {
t.Errorf("Error should be nil: %v", err)
}

actual := NewSet[string]()
err = bson.UnmarshalValue(bson.TypeArray, b, actual)
if err != nil {
t.Errorf("Error should be nil: %v", err)
}

if !expected.Equal(actual) {
t.Errorf("Expected no difference, got: %v", expected.Difference(actual))
}
}
24 changes: 24 additions & 0 deletions threadunsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
"encoding/json"
"fmt"
"strings"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)

type threadUnsafeSet[T comparable] map[T]struct{}
Expand Down Expand Up @@ -328,3 +331,24 @@ func (s threadUnsafeSet[T]) UnmarshalJSON(b []byte) error {

return nil
}

// MarshalBSON creates a BSON array from the set.
func (s threadUnsafeSet[T]) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(s.ToSlice())
}

// UnmarshalBSON recreates a set from a BSON array.
func (s threadUnsafeSet[T]) UnmarshalBSONValue(bt bsontype.Type, b []byte) error {
if bt != bson.TypeArray {
return fmt.Errorf("must use BSON Array to unmarshal Set")
}

var i []T
err := bson.UnmarshalValue(bt, b, &i)
if err != nil {
return err
}
s.Append(i...)

return nil
}
Loading