[GODRIVER-608] Cannot transform struct typed value if there is a map typed fields in it Created: 23/Oct/18  Updated: 27/Oct/23  Resolved: 19/Nov/18

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

Type: Bug Priority: Major - P3
Reporter: Viet An Tran Assignee: Kristofer Brandow (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 18.04, Golang v1.11.1



 Description   

As stated above, I have a struct like this
 

type User struct {
    ID string `json:"id"`
    Username string `json:"username"`
    FirstName string `json:"first_name"`
    LastName string `json:"last_name"`
    FullName string `json:"full_name"`
    Email string `json:"email"`
    Avatar string `json:"avatar"`
    Raw map[string]interface{} `json:"raw"` // Raw data
}

 

 And then i tried to push a variable of this type to my database like this

var usr User
// Some works here, populating the usr variable
 
client, err := mongo.NewClient("mongodb://localhost:27017")
err = client.Connect(context.TODO())
 
 
collection := mongo.Client.Database("foo").Collection("bar")
ctx := context.Background()
result, err := collection.InsertOne(ctx, usr)
if err != nil {
    panic(err)
}
fmt.Println(result.InsertedID)
 
 

 However, the program create an error like this 

cannot transform type structs.User to a *bson.Document

After some trial I found out that if the Raw field is populated it will panic like that, but if I set it to {{nil }}everything  will go back to normal.



 Comments   
Comment by Kristofer Brandow (Inactive) [ 19/Nov/18 ]

Closed due to inactivity.

Comment by Kristofer Brandow (Inactive) [ 29/Oct/18 ]

Hi vietanisme@gmail.com,

Can you please try with the latest alpha (v0.0.18) and see if you have the same problem? I was unable to replicate your issue.

--Kris

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