mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
MIT License
7.93k stars 677 forks source link

Lower case problem #322

Open alpcanm opened 1 year ago

alpcanm commented 1 year ago

In my case , ı'm using mongodb driver. In this model "SecondId" decoded correctly but "ID" doesn't decode it would be 000000000000 because "ID" bson starts with lower case letter or underline. When i make bson:"_id" tobson:"Id" it works correctly but i can't do this because mongodriver doesn't work that way correctly. Id parameter should be "_id".

type MyModel struct {
    ID        primitive.ObjectID `bson:"_id" json:"id" `
    SecondId  primitive.ObjectID `bson:"SecondId,omitempty" json:"secondId"`
    CreatedAt int64              `bson:"CreatedAt,omitempty" json:"createdAt"`
    Text      string             `bson:"Text,omitempty" json:"text"`
}

output:

{
  id: '000000000000000000000000',
  secondId: '641c35cb74774ce4845114ba',
  createdAt: 1679768385584,
  text: 'test text'
}