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

Sets can be written to the DB, but not read #65

Open baioc opened 3 years ago

baioc commented 3 years ago

Description

Trying to read an object which contains a Set as a field causes a runtime error. Writes don't cause such a crash, but it is not clear whether the data was correctly stored.

Repro code

#r "nuget: LiteDB.FSharp"

open LiteDB
open LiteDB.FSharp
open LiteDB.FSharp.Extensions

let database =
    let mapper = FSharpBsonMapper()
    let connStr = "Filename=Lite.db;mode=Exclusive"
    new LiteDatabase(connStr, mapper)

type Foo =
    { Id: string
      Field: Set<int> }

let data = database.GetCollection<Foo> "data"

let select id =
    data.findOne <@ fun x -> x.Id = id @>

let upsert datum =
    match data.tryFindOne <@ fun x -> x.Id = datum.Id @> with
    | None -> data.Insert(datum) |> ignore
    | Some _ -> data.Update(datum) |> ignore

upsert { Id = "test"; Field = Set.empty }
select "test" // fails here

Error information

When running the previous snippet, this is the stack trace I get:

System.Exception: Could not extract element type from collection of type Microsoft.FSharp.Collections.FSharpSet`1[[System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
   at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1433.Invoke(String message) in /build/dotnet-core/src/dotnet-source-build/artifacts/src/fsharp.7ce7132f1459095e635194d09d6f73265352029a/src/fsharp/FSharp.Core/printf.fs:line 1433
   at LiteDB.FSharp.Bson.rewriteKey@100(FSharpList`1 keys, BsonDocument entity, Type entityType, String key)
   at LiteDB.FSharp.Bson.rewriteIdentityKeys@98(Type entityType, BsonDocument entity)
   at LiteDB.FSharp.Bson.deserializeByType(BsonDocument entity, Type entityType)
   at LiteDB.FSharp.Bson.deserialize[t](BsonDocument entity)
   at LiteDB.FSharp.FSharpBsonMapper.ToObject[t](BsonDocument entity)
   at LiteDB.LiteCollection`1.Find(Query query, Int32 skip, Int32 limit)+MoveNext()
   at Microsoft.FSharp.Collections.SeqModule.TryHead[T](IEnumerable`1 source) in /build/dotnet-core/src/dotnet-source-build/artifacts/src/fsharp.7ce7132f1459095e635194d09d6f73265352029a/src/fsharp/FSharp.Core/seq.fs:line 1370
   at FSI_0002.insert(Foo datum)
   at <StartupCode$FSI_0002>.$FSI_0002.main@()
Stopped due to error