[GODRIVER-1777] BSON Key conflict in unmarshaling Created: 25/Oct/20 Updated: 28/Oct/23 Resolved: 01/Dec/20 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | BSON |
| Affects Version/s: | None |
| Fix Version/s: | 1.4.4 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Alessandro Sanino | Assignee: | Isabella Siu (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
|
Hello, I am trying to do the following ``` go import ( "github.com/gofrs/uuid" type A struct { ID uuid.UUID `bson:"id"` Name string `bson:"name,required"` }type Alias struct { A `bson:",inline"` ID mongoutil.UUID `bson:"id,required"` }func main() { c, _ := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://admin:admin@localhost:27017")) coll := c.Database("tryvium").Collection("test") id, _ := uuid.NewV4() aa := A\{ID: id, Name: "test"} bb := Alias{A: aa, ID: mongoutil.UUID(aa.ID)} fmt.Println(err) However I get the following error on run `cannot transform type main.Alias to a BSON Document: struct main.Alias) duplicated key id` Is there any way to restructure my 2 structs to achieve what I want, which is having ID field changed in `Alias` struct? |
| Comments |
| Comment by Githook User [ 01/Dec/20 ] | ||||||||||||||||||||||||||||||||||||
|
Author: {'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}Message: | ||||||||||||||||||||||||||||||||||||
| Comment by Githook User [ 01/Dec/20 ] | ||||||||||||||||||||||||||||||||||||
|
Author: {'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}Message: | ||||||||||||||||||||||||||||||||||||
| Comment by Isabella Siu (Inactive) [ 11/Nov/20 ] | ||||||||||||||||||||||||||||||||||||
|
Hi a.sanino@tryvium.io , I'll schedule it for 1.4.4, which will be released on December 1st. | ||||||||||||||||||||||||||||||||||||
| Comment by Alessandro Sanino [ 11/Nov/20 ] | ||||||||||||||||||||||||||||||||||||
|
thanks @Isabella Siu which is the ETA for this to come out? | ||||||||||||||||||||||||||||||||||||
| Comment by Isabella Siu (Inactive) [ 11/Nov/20 ] | ||||||||||||||||||||||||||||||||||||
|
As an update on this ticket, I've looked more into how encoding/json processes fields with the same names, and we will be changing bson to allow for field replacement for embedded structs. | ||||||||||||||||||||||||||||||||||||
| Comment by Alessandro Sanino [ 02/Nov/20 ] | ||||||||||||||||||||||||||||||||||||
|
@Isabella Siu No, that's not what I want. | ||||||||||||||||||||||||||||||||||||
| Comment by Isabella Siu (Inactive) [ 02/Nov/20 ] | ||||||||||||||||||||||||||||||||||||
|
The problem here is that A.ID and Alias.ID try to go to the same key (id) when Alias is marshaled to bson. Assuming that you want the Alias.ID value to become _id, you can change Alias like this:
Hopefully this fixes your issue! | ||||||||||||||||||||||||||||||||||||
| Comment by Alessandro Sanino [ 25/Oct/20 ] | ||||||||||||||||||||||||||||||||||||
|
Added the code formatted here for a better understanding
And the error
The ideal result is represented by the following image (from Compass) NOTE: mongoutil package is a UUID package which converts the binary UUID with subtype, explicit for mongoDB |