I'm building a go project using mongodb as a database.
In order to make the code easily maintainable, i created a mongo provider package that is the single point of contact between my project and mongo-driver/mongo
. That way I only use my own implementations in the rest of the project.
I wrapped the whole native driver with my own implementations. The only problem I encounter is with the primitive.ObjectID
type.
Obviously, my structures are stored in an internal models
package like this :
type StoredUser struct {
Id my_mongo_driver.Id `bson:"_id,omitempty"`
Username string `bson:"username"`
}
As you see, the models
package does not have any contact with the native mongo-driver/mongo
package. This is what I want.
In the my_mongo_driver
package, I tried reimplementing the Id
type by two different ways :
type Id primitive.ObjectID
type Id struct {primitive.ObjectID}
In both cases, I get an error when parsing the database results :
- First approach :
error decoding key _id: cannot decode objectID into an array
- Second approach :
error decoding key _id: cannot decode objectID into a my_mongo_driver.Id
How can I properly isolate my packages if my models need to use the exact native type for the parsing to work ?
Aucun commentaire:
Enregistrer un commentaire