Zaid-Ajaj / LiteDB.FSharp

Advanced F# Support for LiteDB, an embedded NoSql database for .NET with type-safe query expression through F# quotations
MIT License
180 stars 22 forks source link

Adds support for records with optional arrays #44

Closed aspnetde closed 4 years ago

aspnetde commented 4 years ago

We ran into an issue where we wouldn't be able to deserialize a record with an optional array:

open System
open LiteDB
open LiteDB.FSharp

type ArrayOption =
    { Id : string
      Data: string [] option }

let arrayOptionNone: ArrayOption =
    { Id = Guid.NewGuid().ToString()
      Data = None }

let arrayOptionSome: ArrayOption =
    { Id = Guid.NewGuid().ToString()
      Data = Some [| "string" |] }

[<EntryPoint>]
let main argv =
    use db = new LiteDatabase("filename=arrays.db;mode=exclusive", FSharpBsonMapper())

    let arrayOptionCollection = db.GetCollection<ArrayOption>()

    arrayOptionCollection.Insert arrayOptionNone |> ignore
    arrayOptionCollection.Insert arrayOptionSome |> ignore

    let arrayOptions = arrayOptionCollection.FindAll() |> Seq.toArray
    arrayOptions |> Seq.iter (fun x -> (printf "Id: %s" x.Id))
    0

This would fail.

The changes made in this PR fix this. I assume it's working not only for arrays but also lists etc., but I tried to keep the tests as close as possible to the existing ones and decided to test it for arrays only.

Zaid-Ajaj commented 4 years ago

Thanks a lot for the filing the issue and fixing it right away. These are the best kinds of contributions :heart: :pray: I have published a new version 2.15.0 that includes the fix. Should be available as soon as Nuget does the magic :smile:

aspnetde commented 4 years ago

You're welcome 😄 Thx for updating the Nuget package so fast 👍