[GODRIVER-894] How to make a decodable costum object id Created: 21/Mar/19  Updated: 11/Sep/19  Resolved: 25/Mar/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.0.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Karl Wolffgang Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

go1.12 linux/amd64



 Description   

Is there a way to make ObjectIDs with own methods that can be decoded from a BSON?
I made me a simple custom ObjectID with a valid function:

package primitive
 
import (
	primitive "go.mongodb.org/mongo-driver/bson/primitive"
)
 
type ObjectID struct {
	primitive.ObjectID
}
 
func (o ObjectID) Validate() error {
	return nil
}

But when I use that type instead of the original in a struct and try to decode a BSON I get the following error message:

cannot decode objectID into a primitive.ObjectID



 Comments   
Comment by Karl Wolffgang [ 27/Mar/19 ]

Hey @Ian Whalen, thank you for the fast answer. Can you give me a small hint on how to implement such a custom codec? I am still relatively new to Go and often have difficulties finding the right start.

Comment by Ian Whalen (Inactive) [ 25/Mar/19 ]

karltga indeed you can write a custom codec to do this.

Generated at Thu Feb 08 08:35:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.