I am having a strange (to me) an issue with golang mongodb connector. The current problem is that I have a db retrieval function that actually returns no errors, but the struct expected is just not there (null). Here is part of the code that will probably give some hints:
[library]
func (db *NoSqlConnection[T]) CreateRecoveryTable(p *player.Player[T]) error {
updt := bson.M{"id": p.Id}
_, err := db.db.Collection(recoveryTableName).InsertOne(context.TODO(), updt)
if err != nil {
return fmt.Errorf("failed to update recovery state ")
}
return nil
}
func (db *NoSqlConnection[T]) AddRecoveryRecord(p *player.Player[T], record *recovery.Recovery[T]) (int64, error) {
opts := options.UpdateOne().SetUpsert(true)
updt := bson.M{"$set": bson.M{"game_id": p.GID, "record": record}}
_, err := db.db.Collection(recoveryTableName).UpdateOne(context.TODO(), bson.M{"id": p.Id}, updt, opts)
if err != nil {
return -1, fmt.Errorf("failed to update recovery state")
}
return 1, nil
}
func (db *NoSqlConnection[T]) GetPlayerRecoveryById(id T) (recovery.Recovery[T], error) {
filter := bson.M{"id": id}
res := recovery.Recovery[T]{}
err := db.db.
Collection(playersTableName).
FindOne(context.TODO(), filter).
Decode(&res)
if err != nil {
if err == mongo.ErrNoDocuments {
return res, err
}
return res, err
}
return res, nil
}
The problematic function is GetPlayerRecoveryById(id T) Other functions work correctly (creating / updating the records in my atlas test db).
The recovery model is as:
type SpecializedID interface {
int64 | uint64 | uuid.UUID | string
}
...
type Recovery[T models.SpecializedID] struct {
Id T `json:"id" bson:"id"`
Record any `json:"record" bson:"record"`
}
So when I try to retrieve the record I never get an error, as I expect to have, but always get a zerofill record. I've tried almost all printing families , but can't seem to understand why there is nothing inside the that struct. What I am missing here?
T models.SpecializedID? if I'm reading your program correctly, it means you could have instances of bothRecovery { Id int64; Record any; }in the same keyspace asRecovery { Id UUID, Record: any; }- right?[T]generic type parameters and instead hard-code it to useuint64and see if that works first.anyfield, I have a feeling something withanyis messing up.