[GODRIVER-719] Map Decoder should support non-primitive string types Created: 18/Dec/18  Updated: 28/Oct/23  Resolved: 07/Jan/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.1.0
Fix Version/s: 0.2.0

Type: Bug Priority: Major - P3
Reporter: Aleix Ventayol Assignee: Kristofer Brandow (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu via WSL on Windows


Attachments: PNG File Capture.PNG     File device.go     File device_type.go    

 Description   

The map decoder attempts to set the key of a map value as a string. When the key type is instead a non-primitive string type, e.g. type mystring string, the map decoder panics because it does not first convert the string value of the BSON to the appropriate type. To fix this, convert the BSON key string to the map's key type before setting.

(original description below)


I've a function that finds an item on the database, but I get a reflection error when trying to decode it.

This is the error I get:

reflect.Value.SetMapIndex: value of type string is not assignable to type models.DataType

 

And here you have the code I use to find the element on the database:

 

//Find finds a device
func (r *Device) Find(manufacturer string, model string, sn string) (d *models.Device) {    
    var dev models.Device    
    ctx, cancel := context.WithTimeout(context.Background(), db.Timeout*time.Second)    
    defer cancel()     
 
    err := r.col.FindOne(ctx, bson.M{
        "manufacturer": manufacturer,        
        "model": model,        
        "serialNumber": sn,        
        "active": true,    }).Decode(&dev)    
    if err != nil {        
        log.Fatal(err)        
        d = nil    
    }    
    d = &dev     
    return
}

I attach the two models related to the problem, the device has a map where the map index is not a string but a custom type defined as string.

I was using mgo driver without any problems before, but after migration to this official driver when I try to run my code I get a runtime exception.



 Comments   
Comment by Githook User [ 07/Jan/19 ]

Author:

{'username': 'skriptble', 'email': 'kris@mongodb.com', 'name': 'Kris Brandow'}

Message: Ensure map decoder handles non-primitive strings

GODRIVER-719

Change-Id: Id3d57e5bb8a5eb09bb18684bfe962735ef597ee6
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/0e8ecc2d856e5caaeb7cf7e581624c25a5fb1f65

Comment by Kristofer Brandow (Inactive) [ 04/Jan/19 ]

Code Review: https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/439194.

Comment by Aleix Ventayol [ 04/Jan/19 ]

Let me know if you need me to test anything, I happy to help.

Comment by Kristofer Brandow (Inactive) [ 04/Jan/19 ]

Hi ventayol,

Thanks for the bug report, this is an issue. I'll work on a fix for this.

--Kris

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