Skip to content

GODRIVER-2119 implement methods required to use as a map key #715

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

Merged
merged 3 commits into from
Aug 18, 2021
Merged
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
17 changes: 17 additions & 0 deletions bson/primitive/objectid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package primitive
import (
"bytes"
"crypto/rand"
"encoding"
"encoding/binary"
"encoding/hex"
"encoding/json"
Expand All @@ -34,6 +35,9 @@ var NilObjectID ObjectID
var objectIDCounter = readRandomUint32()
var processUnique = processUniqueBytes()

var _ encoding.TextMarshaler = ObjectID{}
var _ encoding.TextUnmarshaler = ObjectID{}

// NewObjectID generates a new ObjectID.
func NewObjectID() ObjectID {
return NewObjectIDFromTimestamp(time.Now())
Expand Down Expand Up @@ -94,6 +98,19 @@ func IsValidObjectID(s string) bool {
return err == nil
}

// MarshalText returns the ObjectID as UTF-8-encoded text. Implementing this allows us to use ObjectID
// as a map key when marshalling JSON. See https://pkg.go.dev/encoding#TextMarshaler
func (id ObjectID) MarshalText() ([]byte, error) {
return []byte(id.Hex()), nil
}

// UnmarshalText populates the byte slice with the ObjectID. Implementing this allows us to use ObjectID
// as a map key when unmarshalling JSON. See https://pkg.go.dev/encoding#TextUnmarshaler
func (id ObjectID) UnmarshalText(b []byte) error {
id, err := ObjectIDFromHex(string(b))
return err
}

// MarshalJSON returns the ObjectID as a string
func (id ObjectID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.Hex())
Expand Down
32 changes: 32 additions & 0 deletions bson/primitive/objectid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,38 @@ func TestCounterOverflow(t *testing.T) {
require.Equal(t, uint32(0), objectIDCounter)
}

func TestObjectID_MarshalJSONMap(t *testing.T) {
type mapOID struct {
Map map[ObjectID]string
}

oid := NewObjectID()
expectedJSON := []byte(fmt.Sprintf(`{"Map":{%q:"foo"}}`, oid.Hex()))
data := mapOID{
Map: map[ObjectID]string{oid: "foo"},
}

out, err := json.Marshal(&data)
require.NoError(t, err)
require.Equal(t, expectedJSON, out)
}

func TestObjectID_UnmarshalJSONMap(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider incorporating this test function as a test case in TestObjectID_UnmarshalJSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not add it in TestObjectID_UnmarshalJSON because i did not see a clean way of slotting it into the table tests.

type mapOID struct {
Map map[ObjectID]string
}
oid := NewObjectID()
mapOIDJSON := []byte(fmt.Sprintf(`{"Map":{%q:"foo"}}`, oid.Hex()))
expectedData := mapOID{
Map: map[ObjectID]string{oid: "foo"},
}

data := mapOID{}
err := json.Unmarshal(mapOIDJSON, &data)
require.NoError(t, err)
require.Equal(t, expectedData, data)
}

func TestObjectID_UnmarshalJSON(t *testing.T) {
oid := NewObjectID()

Expand Down