eminarican / safetypes

Rust like result and option implementation for golang
GNU General Public License v3.0
34 stars 2 forks source link

MongoDB feature for marshalling and unmarshalling Option struct #1

Open davidbayo10 opened 2 years ago

davidbayo10 commented 2 years ago

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

eminarican commented 2 years ago

Okay, I'll look into it today in my free time thanks for the issue ^^

eminarican commented 2 years ago

oopsie .d

eminarican commented 2 years ago

@davidbayo10 I've tested it using json, probably works fine but could you test using latest commit? I'm not at home currently so its a bit troublesome for me 😅