Cannot transform struct typed value if there is a map typed fields in it

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Gone away
    • Priority: Major - P3
    • None
    • Affects Version/s: 0.0.15
    • Component/s: BSON
    • None
    • Environment:
      Ubuntu 18.04, Golang v1.11.1
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

            Assignee:
            Kristofer Brandow (Inactive)
            Reporter:
            Viet An Tran
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: