Giter VIP home page Giter VIP logo

mongo-go-driver-protobuf's Introduction

mongo-go-driver-protobuf

This is extension for officional MongoDB Go driver adds support for Google protocol buffers types.

Description

It contains set of BSON marshal/unmarshal codecs for Google protocol buffers type wrappers, Timestamp and MongoDB ObjectID:

  • BoolValue
  • BytesValue
  • DoubleValue
  • FloatValue
  • Int32Value
  • Int64Value
  • StringValue
  • Uint32Value
  • Uint64Value
  • Timestamp
  • ObjectID

Links

Requirements

  • Google protocol buffers version proto3
  • Official MongoDB Go Driver RC1 or higher

Installation

Installing using go get:

go get -u github.com/amsokol/mongo-go-driver-protobuf

or you don't need to do anything manually if you are using Go modules. Go modules installs necessary packages automatically.

Usage example

First install protoc-gen-gotag to make available Go language tags for proto messages

go get -u github.com/amsokol/protoc-gen-gotag

Next

  1. Create free Altas mini MongoDB instance
  2. Create experiments database
  3. Create proto collection into experiments database
  4. Run this example

mongo-go-driver-protobuf's People

Contributors

aaulm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongo-go-driver-protobuf's Issues

Not generate bson:"_id, omitempty"

Use example code to regenerate data.pb.go, but not generate bson:"_id, omitempty" or other tag "bson:xxxxx" fields.

Code fragment:

package main

import (
	fmt "fmt"
	pmongo "github.com/amsokol/mongo-go-driver-protobuf/pmongo"
	_ "github.com/amsokol/protoc-gen-gotag/tagger"
	proto "github.com/golang/protobuf/proto"
	timestamp "github.com/golang/protobuf/ptypes/timestamp"
	wrappers "github.com/golang/protobuf/ptypes/wrappers"
	math "math"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

type Data struct {
	Id                   *pmongo.ObjectId      `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	BoolValue            bool                  `protobuf:"varint,2,opt,name=boolValue,proto3" json:"boolValue,omitempty"`
	BoolProtoValue       *wrappers.BoolValue   `protobuf:"bytes,3,opt,name=boolProtoValue,proto3" json:"boolProtoValue,omitempty"`
	BytesValue           []byte                `protobuf:"bytes,4,opt,name=bytesValue,proto3" json:"bytesValue,omitempty"`
	BytesProtoValue      *wrappers.BytesValue  `protobuf:"bytes,5,opt,name=bytesProtoValue,proto3" json:"bytesProtoValue,omitempty"`
	DoubleValue          float64               `protobuf:"fixed64,6,opt,name=doubleValue,proto3" json:"doubleValue,omitempty"`
	DoubleProtoValue     *wrappers.DoubleValue `protobuf:"bytes,7,opt,name=doubleProtoValue,proto3" json:"doubleProtoValue,omitempty"`
	FloatValue           float32               `protobuf:"fixed32,8,opt,name=floatValue,proto3" json:"floatValue,omitempty"`
	FloatProtoValue      *wrappers.FloatValue  `protobuf:"bytes,9,opt,name=floatProtoValue,proto3" json:"floatProtoValue,omitempty"`
	Int32Value           int32                 `protobuf:"varint,10,opt,name=int32Value,proto3" json:"int32Value,omitempty"`
	Int32ProtoValue      *wrappers.Int32Value  `protobuf:"bytes,11,opt,name=int32ProtoValue,proto3" json:"int32ProtoValue,omitempty"`
	Int64Value           int64                 `protobuf:"varint,12,opt,name=int64Value,proto3" json:"int64Value,omitempty"`
	Int64ProtoValue      *wrappers.Int64Value  `protobuf:"bytes,13,opt,name=int64ProtoValue,proto3" json:"int64ProtoValue,omitempty"`
	StringValue          string                `protobuf:"bytes,14,opt,name=stringValue,proto3" json:"stringValue,omitempty"`
	StringProtoValue     *wrappers.StringValue `protobuf:"bytes,15,opt,name=stringProtoValue,proto3" json:"stringProtoValue,omitempty"`
	Uint32Value          uint32                `protobuf:"varint,16,opt,name=uint32Value,proto3" json:"uint32Value,omitempty"`
	Uint32ProtoValue     *wrappers.UInt32Value `protobuf:"bytes,17,opt,name=uint32ProtoValue,proto3" json:"uint32ProtoValue,omitempty"`
	Uint64Value          uint64                `protobuf:"varint,18,opt,name=uint64Value,proto3" json:"uint64Value,omitempty"`
	Uint64ProtoValue     *wrappers.UInt64Value `protobuf:"bytes,19,opt,name=uint64ProtoValue,proto3" json:"uint64ProtoValue,omitempty"`
	Timestamp            *timestamp.Timestamp  `protobuf:"bytes,20,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
	XXX_NoUnkeyedLiteral struct{}              `json:"-"`
	XXX_unrecognized     []byte                `json:"-"`
	XXX_sizecache        int32                 `json:"-"`
}

Json Unmarshal

Hey,
I'm trying to unmarshal some JSON response back into Protobuf.
but I'm getting
json: cannot unmarshal string into Go value of type map[string]json.RawMessage
and it's caused by the ID field
from

s := []byte(`{"id":"5eaacacc6149d0f8d4b6a36d"}`)
var rsp pb.IDMsg
unmarshaler = jsonpb.Unmarshaler{AllowUnknownFields: false}
if err := unmarshaler.Unmarshal(bytes.NewBuffer(s), &rsp); err != nil {
	t.Fatal(err)
}

pb.proto:

message IDMsg {
  pmongo.ObjectId id = 1;
}

pb.go:

type IDMsg struct {
	Id                   *pmongo.ObjectId `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	XXX_NoUnkeyedLiteral struct{}         `json:"-" bson:"-"`
	XXX_unrecognized     []byte           `json:"-" bson:"-"`
	XXX_sizecache        int32            `json:"-" bson:"-"`
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.