protobson

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 10 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.

Main difference from original library is ability to set custom field naming - all you need is to make two functions to get document field name from proto field descriptor and versa.

Overview

Usage

Complete example can be seen in example directory

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

package main

import (
	"log"
    "reflect"

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

    "github.com/custom-app/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")
	client, err := mongo.Connect(context.Background(), opts)
	if err != nil {
		log.Panicln(err)
	}
	...
}

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

View Source
var (
	// ErrNotImplementingProto occurs when value for decoding is not implementing proto.Message interface
	ErrNotImplementingProto = errors.New("value not implementing proto interface")
)

Functions

func NewCodec

func NewCodec(opts ...Option) 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.

If no FieldNamer option passed, FieldNamerByNumber will be used as default

Types

type FieldNamer

type FieldNamer interface {
	// FieldDescriptorToFieldName is function to get document field name from proto field descriptor
	FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string
	// FieldNameToFieldDescriptor is function to get proto field descriptor from document field name
	FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors, name string) (protoreflect.FieldDescriptor, error)
}

FieldNamer is used to build field name in document from its field descriptor

type FieldNamerByJsonName

type FieldNamerByJsonName struct {
}

FieldNamerByJsonName makes field name based on json name of field

func (*FieldNamerByJsonName) FieldDescriptorToFieldName

func (f *FieldNamerByJsonName) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string

FieldDescriptorToFieldName is implementation of FieldNamer method

func (*FieldNamerByJsonName) FieldNameToFieldDescriptor

func (f *FieldNamerByJsonName) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
	name string) (protoreflect.FieldDescriptor, error)

FieldNameToFieldDescriptor is implementation of FieldNamer method

type FieldNamerByName

type FieldNamerByName struct {
}

FieldNamerByName makes field name based on field name in proto spec

func (*FieldNamerByName) FieldDescriptorToFieldName

func (f *FieldNamerByName) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string

FieldDescriptorToFieldName is implementation of FieldNamer method

func (*FieldNamerByName) FieldNameToFieldDescriptor

func (f *FieldNamerByName) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
	name string) (protoreflect.FieldDescriptor, error)

FieldNameToFieldDescriptor is implementation of FieldNamer method

type FieldNamerByNumber

type FieldNamerByNumber struct {
}

FieldNamerByNumber makes field names based on field tag number. With this implementation fields in proto spec can be renamed and documents still can be decoded without loss of any data

func (*FieldNamerByNumber) FieldDescriptorToFieldName

func (f *FieldNamerByNumber) FieldDescriptorToFieldName(fd protoreflect.FieldDescriptor) string

FieldDescriptorToFieldName is implementation of FieldNamer method

func (*FieldNamerByNumber) FieldNameToFieldDescriptor

func (f *FieldNamerByNumber) FieldNameToFieldDescriptor(fd protoreflect.FieldDescriptors,
	name string) (protoreflect.FieldDescriptor, error)

FieldNameToFieldDescriptor is implementation of FieldNamer method

type Option

type Option func(*protobufCodec)

Option - functional option for protobufCodec

func WithFieldNamer

func WithFieldNamer(namer FieldNamer) Option

WithFieldNamer - returns options with FieldNamer instance

func WithFieldNamerByJsonName

func WithFieldNamerByJsonName() Option

WithFieldNamerByJsonName makes FieldNamer Option with FieldNamerByJsonName instance

func WithFieldNamerByName

func WithFieldNamerByName() Option

WithFieldNamerByName makes FieldNamer Option with FieldNamerByName instance

func WithFieldNamerByNumber

func WithFieldNamerByNumber() Option

WithFieldNamerByNumber makes FieldNamer Option with FieldNamerByNumber instance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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