https://github.com/vallahaye/protobson
BSON codecs for Google's protocol buffers
https://github.com/vallahaye/protobson
go golang mongodb protobuf protocol-buffers
Last synced: 11 months ago
JSON representation
BSON codecs for Google's protocol buffers
- Host: GitHub
- URL: https://github.com/vallahaye/protobson
- Owner: vallahaye
- License: mit
- Created: 2022-03-25T22:09:38.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-12T10:53:06.000Z (over 1 year ago)
- Last Synced: 2024-10-13T22:48:45.591Z (over 1 year ago)
- Topics: go, golang, mongodb, protobuf, protocol-buffers
- Language: Go
- Homepage: https://go.vallahaye.net/protobson
- Size: 66.4 KB
- Stars: 16
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protobson
[](https://pkg.go.dev/go.vallahaye.net/protobson)  [](https://goreportcard.com/badge/github.com/vallahaye/protobson) 
BSON codecs for Google's protocol buffers.
This library provides add-ons to [go.mongodb.org/mongo-driver](https://pkg.go.dev/go.mongodb.org/mongo-driver) for first-class protobuf support with similar API design, extensive testing and [high reuse of already existing codecs](https://github.com/vallahaye/protobson/blob/main/protobsoncodec/message_codec.go). The following types are currently mapped:
| Protobuf | MongoDB |
|-----------|------------|
| [`message`](https://pkg.go.dev/google.golang.org/protobuf/proto#Message) | [Document](https://www.mongodb.com/docs/manual/core/document/) |
| [`google.protobuf.Timestamp`](https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb#Timestamp) | [Date](https://www.mongodb.com/docs/manual/reference/bson-types/#date) |
| [`google.protobuf.Duration`](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb#Duration) | [64-bit integer](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.BoolValue`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#BoolValue) | [Boolean](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.BytesValue`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#BytesValue) | [Binary](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.DoubleValue`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#DoubleValue) | [Double](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.FloatValue`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#FloatValue) | [Double](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.Int32Value`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#Int32Value) | [32-bit integer](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.Int64Value`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#Int64Value) | [64-bit integer](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.StringValue`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#StringValue) | [String](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.UInt32Value`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#UInt32Value) | [32-bit integer](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.protobuf.UInt64Value`](https://pkg.go.dev/google.golang.org/protobuf/types/known/wrapperspb#UInt64Value) | [64-bit integer](https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types) |
| [`google.type.DateTime`](https://pkg.go.dev/google.golang.org/genproto/googleapis/type/datetime#DateTime) | [Date](https://www.mongodb.com/docs/manual/reference/bson-types/#date) |
This list will grow as we add support for most [Well-Known Types](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf) as well as [Google APIs Common Types](https://github.com/googleapis/api-common-protos).
## Usage
```
$ go get go.vallahaye.net/protobson
```
```go
// Set client options
clientOptions := options.Client().
SetRegistry(protobson.DefaultRegistry).
ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
```