protobsoncodec

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSONPBFallbackStructTagParser bsoncodec.StructTagParserFunc = func(sf reflect.StructField) (bsoncodec.StructTags, error) {
	if _, ok := sf.Tag.Lookup("bson"); ok {
		return bsoncodec.DefaultStructTagParser(sf)
	}
	tag, ok := sf.Tag.Lookup("protobuf")
	if !ok {
		return bsoncodec.DefaultStructTagParser(sf)
	}
	return parseTags(tag, false)
}

JSONPBFallbackStructTagParser is the StructTagParser used by the MessageCodec by default. It has the same behavior as bsoncodec.DefaultStructTagParser but will also fallback to parsing the protobuf tag on a field where the bson tag isn't available. In this case, the key will be taken from the json property, or from the name property if there is none.

An example:

type T struct {
  Name   string `protobuf:"bytes,1,opt,name=name,proto3"` // Key is "name"
  FooBar string `protobuf:"bytes,2,opt,name=foo_bar,json=fooBar,proto3"` // Key is "fooBar"
  BarFoo string `protobuf:"bytes,3,opt,name=bar_foo,json=barFoo,proto3" bson:"barfoo"` // Key is "barfoo"
}
View Source
var ProtoNamesFallbackStructTagParser bsoncodec.StructTagParserFunc = func(sf reflect.StructField) (bsoncodec.StructTags, error) {
	if _, ok := sf.Tag.Lookup("bson"); ok {
		return bsoncodec.DefaultStructTagParser(sf)
	}
	tag, ok := sf.Tag.Lookup("protobuf")
	if !ok {
		return bsoncodec.DefaultStructTagParser(sf)
	}
	return parseTags(tag, true)
}

ProtoNamesFallbackStructTagParser has the same behavior as JSONPBFallbackStructTagParser except it forces the use of the name property as the key when parsing protobuf tags.

View Source
var TypeMessage = reflect.TypeOf((*proto.Message)(nil)).Elem()

Message type.

Functions

This section is empty.

Types

type MessageCodec

type MessageCodec struct {
	*bsoncodec.StructCodec
}

MessageCodec is the Codec used for proto.Message values.

func NewMessageCodec

func NewMessageCodec(opts ...*protobsonoptions.MessageCodecOptions) *MessageCodec

NewMessageCodec returns a MessageCodec with options opts.

func (*MessageCodec) DecodeValue

DecodeValue is the ValueDecoderFunc for proto.Message.

func (*MessageCodec) EncodeValue

EncodeValue is the ValueEncoderFunc for proto.Message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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