From 853b126c1144af8456729bc878b7f7d6411cabe5 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Tue, 12 Nov 2024 08:54:07 -0700 Subject: [PATCH 1/4] Add examples to the marshaling updates in the migration guide --- docs/migration-2.0.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index 3b0ad3b7fa..e0b7dc9c90 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -1183,8 +1183,20 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t ### ValueMarshaler -The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package. +The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package. Comparison can be achieved by type asserting with `bson.Type`. For example: + +```go +btype, _, _ := m.MarshalBSONValue() +fmt.Println("type of data: %s: ", bson.Type(btype)) +fmt.Println("type of data is an array: %v", bson.Type(btype) == bson.TypeArray) +``` ### ValueUnmarshaler -The `UnmarshalBSONValue` method of the `ValueUnmarshaler` interface is only required to take a byte type argument representing the BSON type to avoid importing the Go driver package. +The `UnmarshalBSONValue` method of the `ValueUnmarshaler` interface is only required to take a byte type argument representing the BSON type to avoid importing the Go driver package. For example: + +```go +if err := m.UnmarshalBSONValue(bson.TypeEmbeddedDocument, bytes); err != nil { + log.Fatalf("failed to decode embedded document: %v", err) +} +``` From 6a3befe288e79404aef4c7277fbe3d2387a2c66a Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Tue, 12 Nov 2024 09:37:24 -0700 Subject: [PATCH 2/4] Change asserting to converting --- docs/migration-2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index e0b7dc9c90..ba12d1a8cd 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -1183,7 +1183,7 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t ### ValueMarshaler -The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package. Comparison can be achieved by type asserting with `bson.Type`. For example: +The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package. Comparison can be achieved by type converting with `bson.Type`. For example: ```go btype, _, _ := m.MarshalBSONValue() From 5538c77b383105ceaf73a02e89f1606386c468c7 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Fri, 15 Nov 2024 12:58:47 -0700 Subject: [PATCH 3/4] Update docs/migration-2.0.md Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com> --- docs/migration-2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index ba12d1a8cd..f3fa4cd157 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -1183,7 +1183,7 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t ### ValueMarshaler -The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package. Comparison can be achieved by type converting with `bson.Type`. For example: +The `MarshalBSONValue` method of the [ValueMarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueMarshaler) interface now returns a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type). That allows external packages to implement the `ValueMarshaler` interface without having to import the `bson` package. Convert a returned `byte` value to [bson.Type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) to compare with the BSON type constants. For example: ```go btype, _, _ := m.MarshalBSONValue() From be0f046eea4957c6eca508c2a0b4fdff1e039ac6 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Fri, 15 Nov 2024 12:58:58 -0700 Subject: [PATCH 4/4] Update docs/migration-2.0.md Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com> --- docs/migration-2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration-2.0.md b/docs/migration-2.0.md index f3fa4cd157..c0c6f3a20b 100644 --- a/docs/migration-2.0.md +++ b/docs/migration-2.0.md @@ -1193,7 +1193,7 @@ fmt.Println("type of data is an array: %v", bson.Type(btype) == bson.TypeArray) ### ValueUnmarshaler -The `UnmarshalBSONValue` method of the `ValueUnmarshaler` interface is only required to take a byte type argument representing the BSON type to avoid importing the Go driver package. For example: +The `UnmarshalBSONValue` method of the [ValueUnmarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueUnmarshaler) interface now accepts a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) for the first argument. That allows packages to implement `ValueUnmarshaler` without having to import the `bson` package. For example: ```go if err := m.UnmarshalBSONValue(bson.TypeEmbeddedDocument, bytes); err != nil {