safetypes icon indicating copy to clipboard operation
safetypes copied to clipboard

MongoDB feature for marshalling and unmarshalling Option struct

Open davidbayo10 opened this issue 2 years ago • 3 comments

Hi,

Just a little question. Could be possible to marshal and unmarshal MongoDB structs with Option properties?

If you try to Decode a Cursor result, It not fits the Option struct because of value property

e.g.

type Record struct {
	Date1 safetypes.Option[time.Time] `bson:"date1,omitempty"`
	Date2  safetypes.Option[time.Time] `bson:"date2,omitempty"`
}

Input:

[
  {
    date1: "2022-08-30T00:00:00.000Z"
  },
  {
    date2: "2022-09-30T00:00:00.000Z"
  }
]

Marshalled records have this struct:

Record { Date1 { value: nil } Date2 { value: nil } }

I would like to decode this array into

Desire:

records[0].Date1.IsSome() == true
records[0].Date2.IsNone() == true

records[1].Date1.IsNone() == true
records[1].Date2.IsSome() == true

I like this lib for allow go structs be "Rust Like". I hope this could be possible Thank you all in advance

davidbayo10 avatar Jul 08 '22 11:07 davidbayo10