Home > Backend Development > Golang > How to Customize Marshaling/Unmarshaling for MongoDB Types in Go?

How to Customize Marshaling/Unmarshaling for MongoDB Types in Go?

DDD
Release: 2024-11-15 02:05:03
Original
513 people have browsed it

How to Customize Marshaling/Unmarshaling for MongoDB Types in Go?

Customizing Marshaling/Unmarshaling for MongoDB Types in Go

When accessing MongoDB from Go, you may encounter situations where values need to be transformed during marshaling/unmarshaling. Consider the case of accessing the EndDate field stored as a string in MongoDB, but you require it as a Go time.Time type.

To address such scenarios, you can implement custom marshaling/unmarshaling logic using the bson.Getter and bson.Setter interfaces. Here's a step-by-step guide:

  1. Extend the Data Structure: Extend the clientConfigData type by adding a new field of type time.Time, denoted with a bson tag of "-".
type clientConfigData struct {
    SMTPAssoc  int       `bson:"smtp_assoc"`
    PlanType   string    `bson:"plan_type"`
    EndDateStr string    `bson:"end_date"`
    EndDate    time.Time `bson:"-"`
}
Copy after login
  1. Implement Custom Marshaling/Unmarshaling: Create SetBSON() and GetBSON() methods to handle custom marshaling/unmarshaling.
const endDateLayout = "2006-01-02 15:04:05" // Specify your preferred date layout

func (c *clientConfigData) SetBSON(raw bson.Raw) (err error) {
    type my clientConfigData
    if err = raw.Unmarshal((*my)(c)); err != nil {
        return
    }
    c.EndDate, err = time.Parse(endDateLayout, c.EndDateStr)
    return
}

func (c *clientConfigData) GetBSON() (interface{}, error) {
    c.EndDateStr = c.EndDate.Format(endDateLayout)
    type my *clientConfigData
    return my(c), nil
}
Copy after login

In SetBSON(), populate the struct with the raw MongoDB value and parse the EndDateStr into a time.Time. In GetBSON(), set the EndDateStr and return.

  1. Use Custom Marshaling/Unmarshaling:

When finding or inserting documents, remember to use the custom marshaling/unmarshaling logic by specifying the clientConfigData type.

var configRes = new(clientConfigData)
err := clientDB.
    C(clientConfigCollection).
    Find(bson.M{}).
    One(&configRes)
if err != nil {
    return nil, errors.Wrap(err, "finding config collection")
}
Copy after login

By implementing custom marshaling/unmarshaling, you can seamlessly work with MongoDB values that require specific type conversions. Remember to handle both SetBSON() and GetBSON() to cover both marshalling and unmarshalling.

The above is the detailed content of How to Customize Marshaling/Unmarshaling for MongoDB Types in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template