Skip to content

Support for Nullable DateTime #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LiteDB.FSharp.Tests/Tests.LiteDatabase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ type RecOptGuid = {
OtherId: Option<Guid>
}

type RecNullableDateTime = {
Id: int
Date: Nullable<DateTime>
}

let pass() = Expect.isTrue true "passed"
let fail() = Expect.isTrue false "failed"

Expand Down Expand Up @@ -658,4 +663,32 @@ let liteDatabaseUsage mapper =
|> function
| 1 -> pass()
| n -> fail()

testCase "Documents with Nullable DateTime != null can be used" <| fun _ ->
useDatabase mapper<| fun db ->
let docs = db.GetCollection<RecNullableDateTime>()
let date = Nullable DateTime.MinValue
docs.Insert ({ Id = 1; Date = date }) |> ignore
docs.FindAll()
|> Seq.tryHead
|> function
| None -> fail()
| Some doc ->
match doc.Id, doc.Date with
| 1, date -> pass()
| _ -> fail()

testCase "Documents with Nullable DateTime = null can be used" <| fun _ ->
useDatabase mapper<| fun db ->
let docs = db.GetCollection<RecNullableDateTime>()
let date = Nullable()
docs.Insert ({ Id = 1; Date = date }) |> ignore
docs.FindAll()
|> Seq.tryHead
|> function
| None -> fail()
| Some doc ->
match doc.Id, doc.Date with
| 1, date -> pass()
| _ -> fail()
]
5 changes: 5 additions & 0 deletions LiteDB.FSharp.Tests/Tests.Runner.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Runner

open System
open Expecto
open Expecto.Logging
open LiteDB.FSharp
Expand Down Expand Up @@ -27,6 +28,10 @@ let defaultValueTests =
testCase "Default of string is an empty string" <| fun _ ->
let value = DefaultValue.fromType (typeof<string>) |> unbox<string>
Expect.equal "" value "An empty string is the default string"

testCase "Works with Nullable DateTimes" <| fun _ ->
let value = DefaultValue.fromType (typeof<Nullable<DateTime>>) |> unbox<Nullable<DateTime>>
Expect.equal (Nullable()) value ""
]

let liteDbTests mapper name =
Expand Down
15 changes: 14 additions & 1 deletion LiteDB.FSharp/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Kind =
| ObjectId = 13
| Double = 14
| Record = 15
| NullableDateTime = 16

/// Helper for serializing map/dict with non-primitive, non-string keys such as unions and records.
/// Performs additional serialization/deserialization of the key object and uses the resulting JSON
Expand Down Expand Up @@ -204,6 +205,8 @@ type FSharpJsonConverter() =
&& t.GetGenericArguments().[0] <> typeof<string>
then
Kind.MapOrDictWithNonStringKey
elif t = typeof<Nullable<DateTime>>
then Kind.NullableDateTime
else Kind.Other)

match kind with
Expand Down Expand Up @@ -297,7 +300,8 @@ type FSharpJsonConverter() =
writer.WritePropertyName(fieldType.Name)
serializer.Serialize(writer, fieldValue)
writer.WriteEndObject()

| true, Kind.NullableDateTime ->
serializer.Serialize(writer, value)
| true, _ ->
serializer.Serialize(writer, value)

Expand Down Expand Up @@ -457,6 +461,15 @@ type FSharpJsonConverter() =
| false, _ -> DefaultValue.fromType fieldType

FSharpValue.MakeRecord(t, recordValues)
| true, Kind.NullableDateTime ->
match reader.TokenType with
| JsonToken.Null ->
null
| _ ->
let jsonObject = JObject.Load(reader)
let dateValue = jsonObject.["$date"].Value<string>()
let date = DateTime.Parse(dateValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
upcast date

| true, _ ->
serializer.Deserialize(reader, t)
Expand Down