Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a custom type for Mongo's ID's #442

Closed
EwanValentine opened this issue Oct 16, 2017 · 10 comments
Closed

Creating a custom type for Mongo's ID's #442

EwanValentine opened this issue Oct 16, 2017 · 10 comments

Comments

@EwanValentine
Copy link

Apologies, I raised a similar ticket a while back but I just thought I'd raise a more specific question than my previous closed ticket. #393

For some context, when you generate protobuf code, say...

message Hello {
  string id = 1;
}

You would get

type Hello struct {
  Id string
}

Which means I have to do a bunch of conversion in order to use these structs as Mongodb models. As Mongodb's mgo driver requires a bson.ObjectId custom type for the ID field. Also the ID field is all uppercase as per Golang's own linting/style guide. Whereas protobuf generates them as Id.

So given that, is there a way I can create a custom type which would generate mgo compatible ID fields? Such as...

message Hello {
  BsonID id = 1;
}

generates...

type Hello struct {
    ID bson.ObjectId
}

I just wondered how to go about creating a custom type for this use-case?

I noticed someone had a PR of a similar nature a while back which wasn't approved/merged: #320

Thanks in advance!

@pbdeuchler
Copy link

We use bytes in our proto definitions and it works very well

@EwanValentine
Copy link
Author

EwanValentine commented Nov 1, 2017

@pbdeuchler Do you need to manually convert between the two at runtime still? Did you need to limit the bytes to 12 in your protobuf def?

@EwanValentine
Copy link
Author

Oh yeah it is just bytes under the hood! https://godoc.org/github.com/pkg/bson#ObjectId

@dsnet
Copy link
Member

dsnet commented Dec 6, 2017

Closing this in favor of #52.

@dsnet dsnet closed this as completed Dec 6, 2017
@bazaglia
Copy link

@EwanValentine @pbdeuchler have you tried the official mongodb go driver? I couldn't get it working: could not greet: rpc error: code = Unknown desc = cannot decode objectID into a *[]byte exit status 1

@amsokol
Copy link

amsokol commented Feb 23, 2019

Hi, look at this one:
https://github.com/amsokol/mongo-go-driver-protobuf
It supports ObjectId also.

@mihai1voicescu
Copy link

ObjectId can be replaced with a simple string in proto now

@Tobi696
Copy link

Tobi696 commented May 6, 2020

@mihai1voicescu Could you show the example code? Either it doesn't work for me or I didn't get you right.

@mihai1voicescu
Copy link

Unfortunately I put that project on hold before I could finish but this is what I used:

message Person {
    string  id = 1 [(tagger.tags) = "bson:\"_id,omitempty\""];

Which generates:

type Person struct {
	Id                   string           `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" bson:"_id,omitempty"`

As far as I remember (so I might be wrong) you are then free to insert the structs in Mongo and it will generate an ID if the struct.id == "" which you can get with:

query, err := itemCol.InsertOne(ctx, &req.Item)
...
oid, ok := query.InsertedID.(primitive.ObjectID)
	if !ok {
		panic("Unable to convert")
	}

	res := mn.CreateResponse{Id: oid.Hex()}

Note that I had one more problem with the ID, but I can't remember what it was or if I solved it.

Hope it helps! If not maybe you can post the exact exception/st/problem.

Please note that I am not an expert in go so the above code might not be production ready.

@pravchuk
Copy link

Has this been solved or not yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants