1

I'm storing attachment in mongodb as Attachment object:

type Attachment struct {
  ID   string `bson:"_id" json:"id"`
  Name string `bson:"name" json:"name"`
  URL  string `bson:"url" json:"url"`
}

The URL stored is the presigned URL for PUT request, retrieved using AWS session. In Ruby on Rails, I can use virtual attribute to change the URL to presigned URL for GET request:

// models/attachment.rb
def url
  if super.present?
    // get the presigned URL for get request using the URL from super
  else
    super
  end
end

How can I accomplish this in Go? I have my configs in config.yaml and need to convert yaml to struct. Meanwhile, marshal and unmarshal of BSON only receive data []byte as parameter. I'm not sure how to initiate the AWS session in the marshal and unmarshal of BSON.

I prefer to modify the URL after I query from mongodb, but I want to do it in 1 place

5
  • So you want to modify the URL before / after saving to / from MongoDB?
    – icza
    Jan 18, 2021 at 10:13
  • @icza anything, as long as I can do it in one place, dont want to do it in many places
    – nanakondor
    Jan 18, 2021 at 10:15
  • Not describing what you actually want to achieve kind of limits this question to be answered by people who are familiar with Ruby on Rails and it's virtual attributes. Achieve what?
    – super
    Jan 18, 2021 at 10:16
  • ok i think i prefer to modify the URL after I do the query from mongodb
    – nanakondor
    Jan 18, 2021 at 10:18
  • Please don't put tags in the title. Also, the language is called "Go", not "golang" Jan 18, 2021 at 10:38

1 Answer 1

1

The mongo-go and mgo drivers check for and call certain implemented interfaces when converting Go values to / from BSON values. Implement bson.Marshaler and bson.Unmarshaler on your type and you can do anything before saving it / after loading it.

Call the default bson.Marhsal() and bson.Unmarshal() functions to do the regular marshaling / unmarshaling process, and if that succeeds, then do what you want before returning.

For example:

// Called when an Attachment is saved.
func (a *Attachment) MarshalBSON() (data []byte, err error) {
    data, err = bson.Marshal(a)
    if err != nil {
        return
    }

    // Do your additional thing here

    return
}

// Called when an Attachment is loaded.
func (a *Attachment) UnmarshalBSON(data []byte) error {
    if err := bson.Unmarshal(data, &a); err != nil {
        return err
    }

    // Do your additional thing here

    return nil
}

Also see related: How to ignore nulls while unmarshalling a MongoDB document?

4
  • sorry if i wasnt clear. i mean, if i hardcode my aws config i think its possible but I retrieve it using config.yaml so im not sure how to get them inside the unmarshal function
    – nanakondor
    Jan 18, 2021 at 10:37
  • Your custom marshaling logic is "plain" Go code, it can access your config. You don't need to hard-code anything into it.
    – icza
    Jan 18, 2021 at 10:39
  • ohh ok i got it ty. also, if i implement unmarshalbson, is it compulsory i implement marshalbson as well eventhough i only need unmarshal?
    – nanakondor
    Jan 18, 2021 at 10:42
  • @nanakondor Implement whichever you need. If you only need custom save, implement only MarshalBSON(). You don't have to implement both.
    – icza
    Jan 18, 2021 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.