protobson

package module
v0.0.0-...-31ff71f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2022 License: MIT Imports: 9 Imported by: 0

README

protobson

GoDev Go Report Card

Description

protobson is a Go library consisting of a BSON codec for Protobuf messages that can be used with mongo-go-driver.

This library uses the second major version of the Go Protobuf API.

Overview

Usage

Below is a snippet making use of this codec by registering it with the MongoDB Go library:

package main

import (
    "reflect"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo/options"
    "google.golang.org/protobuf/proto"

    "github.com/tesspib/protobson"
)

func main() {
    regBuilder := bson.NewRegistryBuilder()
    codec := protobson.NewCodec()

    msgType := reflect.TypeOf((*proto.Message)(nil)).Elem()
    registry := regBuilder.RegisterHookDecoder(msgType, codec).RegisterHookEncoder(msgType, codec).Build()

    opts := options.Client().SetRegistry(registry)
    // opts.ApplyURI("mongodb://localhost:27017")
    // ...
}

Note the use of RegisterHookDecoder and RegisterHookEncoder methods. Those ensure that given codec will be used to encode and decode values which type implement the interface. Since every Protobuf message implements the proto.Message interface, the codec will work with any message value.

Credits

This library is originally based on protomongo, part of the MIT-licensed dataform project by Tada Science, Inc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldNumberToElementName

func FieldNumberToElementName(num protoreflect.FieldNumber) string

FieldNumberToElementName returns the BSON-encoded field name corresponding to Protobuf message field number.

func NewCodec

func NewCodec() bsoncodec.ValueCodec

NewCodec returns a new instance of a BSON codec for Protobuf messages. Messages are encoded using field numbers as document keys, so that stored messages can survive field renames.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL