Skip to content

Commit

Permalink
refactored and added NewObjectId func
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Sokolovskii committed Feb 25, 2019
1 parent 73e23c3 commit b82e874
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 70 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"mongodb",
"objectid",
"officional",
"pmongo",
"proto",
"protobuf",
"protoc",
Expand Down
8 changes: 4 additions & 4 deletions codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/amsokol/mongo-go-driver-protobuf/mongodb"
"github.com/amsokol/mongo-go-driver-protobuf/pmongo"
)

var (
Expand All @@ -33,7 +33,7 @@ var (
timeType = reflect.TypeOf(time.Time{})

// ObjectId type
objectIDType = reflect.TypeOf(mongodb.ObjectId{})
objectIDType = reflect.TypeOf(pmongo.ObjectId{})
objectIDPrimitiveType = reflect.TypeOf(primitive.ObjectID{})

// Codecs
Expand Down Expand Up @@ -108,7 +108,7 @@ type objectIDCodec struct {

// EncodeValue encodes Protobuf ObjectId value to BSON value
func (e *objectIDCodec) EncodeValue(ectx bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
v := val.Interface().(mongodb.ObjectId)
v := val.Interface().(pmongo.ObjectId)
// Create primitive.ObjectId from string
id, err := primitive.ObjectIDFromHex(v.Value)
if err != nil {
Expand All @@ -131,7 +131,7 @@ func (e *objectIDCodec) DecodeValue(ectx bsoncodec.DecodeContext, vr bsonrw.Valu
if err = enc.DecodeValue(ectx, vr, reflect.ValueOf(&id).Elem()); err != nil {
return err
}
oid := mongodb.ObjectId{Value: id.Hex()}
oid := *pmongo.NewObjectId(id)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions codecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/amsokol/mongo-go-driver-protobuf/mongodb"
"github.com/amsokol/mongo-go-driver-protobuf/pmongo"
"github.com/amsokol/mongo-go-driver-protobuf/test"
)

Expand All @@ -32,7 +32,7 @@ func TestCodecs(t *testing.T) {
}

objectID := primitive.NewObjectID()
id := mongodb.ObjectId{Value: objectID.Hex()}
id := pmongo.NewObjectId(objectID)

t.Run("primitive object id", func(t *testing.T) {
resultID, err := id.GetObjectID()
Expand All @@ -58,7 +58,7 @@ func TestCodecs(t *testing.T) {
Uint32Value: &wrappers.UInt32Value{Value: 12345},
Uint64Value: &wrappers.UInt64Value{Value: 123456789},
Timestamp: ts,
Id: &id,
Id: id,
}

t.Run("marshal/unmarshal", func(t *testing.T) {
Expand Down
54 changes: 27 additions & 27 deletions examples/mongodb-rw/data.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/mongodb-rw/data.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "protoc-gen-gotag/tagger/tagger.proto";

import "mongodb/objectid.proto";
import "pmongo/objectid.proto";

message Data{
mongodb.ObjectId id = 1 [(tagger.tags) = "bson:\"_id,omitempty\"" ];
Expand Down
2 changes: 1 addition & 1 deletion mongodb/jsonpb.go → pmongo/jsonpb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mongodb
package pmongo

import (
"bytes"
Expand Down
7 changes: 6 additions & 1 deletion mongodb/mongodb.go → pmongo/objectid.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package mongodb
package pmongo

import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

// NewObjectId creates proto ObjectId from MongoDB ObjectID
func NewObjectId(id primitive.ObjectID) *ObjectId {
return &ObjectId{Value: id.Hex()}
}

// GetObjectID returns MongoDB object ID
func (o *ObjectId) GetObjectID() (primitive.ObjectID, error) {
return primitive.ObjectIDFromHex(o.Value)
Expand Down
12 changes: 6 additions & 6 deletions mongodb/objectid.pb.go → pmongo/objectid.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax="proto3";
package mongodb;

option go_package = "github.com/amsokol/mongo-go-driver-protobuf/mongodb";
option go_package = "github.com/amsokol/mongo-go-driver-protobuf/pmongo";

message ObjectId{
string value = 1;
Expand Down
2 changes: 1 addition & 1 deletion proto_build.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@protoc --proto_path=proto/mongodb --go_out=../../../ objectid.proto
@protoc --proto_path=proto/pmongo --go_out=../../../ objectid.proto

@protoc --proto_path=test --proto_path=proto --proto_path=proto/third_party --go_out=test codecs_test.proto
2 changes: 1 addition & 1 deletion proto_build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#bash

protoc --proto_path=proto/mongodb --go_out=../../../ objectid.proto
protoc --proto_path=proto/pmongo --go_out=../../../ objectid.proto

protoc --proto_path=test --proto_path=proto --proto_path=proto/third_party --go_out=test codecs_test.proto
46 changes: 23 additions & 23 deletions test/codecs_test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/codecs_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package test;
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";

import "mongodb/objectid.proto";
import "pmongo/objectid.proto";

message Data{
google.protobuf.BoolValue boolValue = 1;
Expand Down

0 comments on commit b82e874

Please sign in to comment.