Skip to content

Commit 563588a

Browse files
Add examples to the marshaling updates in the migration guide (#1889)
Co-authored-by: Matt Dale <[email protected]>
1 parent 27ee659 commit 563588a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

docs/migration-2.0.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,20 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t
11831183

11841184
### ValueMarshaler
11851185

1186-
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.
1186+
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:
1187+
1188+
```go
1189+
btype, _, _ := m.MarshalBSONValue()
1190+
fmt.Println("type of data: %s: ", bson.Type(btype))
1191+
fmt.Println("type of data is an array: %v", bson.Type(btype) == bson.TypeArray)
1192+
```
11871193

11881194
### ValueUnmarshaler
11891195

1190-
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.
1196+
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:
1197+
1198+
```go
1199+
if err := m.UnmarshalBSONValue(bson.TypeEmbeddedDocument, bytes); err != nil {
1200+
log.Fatalf("failed to decode embedded document: %v", err)
1201+
}
1202+
```

0 commit comments

Comments
 (0)