Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1777

BSON Key conflict in unmarshaling

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.4.4
    • Component/s: BSON
    • Security Level: Public
    • Labels:
      None

      Description

      Hello, I am trying to do the following

      ``` go
      package main

      import (
          "fmt"

          "github.com/gofrs/uuid"
          "gitlab.tryvium.io/booking-platform/booking-platform-backend/mongoutil"
          "go.mongodb.org/mongo-driver/mongo"
          "go.mongodb.org/mongo-driver/mongo/options"
          "golang.org/x/net/context"
      )

      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)}
          _, err := coll.InsertOne(context.Background(), bb)

          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?

        Attachments

        1. image-2020-10-25-20-36-37-061.png
          image-2020-10-25-20-36-37-061.png
          4 kB
        2. test.go
          0.7 kB

          Activity

            People

            Assignee:
            isabella.siu Isabella Siu
            Reporter:
            a.sanino@tryvium.io Alessandro Sanino
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: