Skip to content

Commit

Permalink
add voption support
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Dec 28, 2024
1 parent 0635dfe commit 4c25eca
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module FSharpValueSerializer =
match typ with
| IsList typ -> Some (mkGenericUsingDef<FSharpListSerializer<_>> typ)
| IsMap typ -> Some (mkGenericUsingDef<FSharpMapSerializer<_, _>> typ)
| IsValueOption typ -> Some (mkGenericUsingDef<FSharpValueOptionSerializer<_>> typ)

Check failure on line 48 in src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs

View workflow job for this annotation

GitHub Actions / build

The pattern discriminator 'IsValueOption' is not defined.

Check failure on line 48 in src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs

View workflow job for this annotation

GitHub Actions / build

The type 'FSharpValueOptionSerializer' is not defined. Maybe you want one of the following:� FSharpValueSerializationProvider� FSharpOptionSerializer� FSharpUnionSerializer� FSharpSetSerializer� FSharpListSerializer
| IsOption typ -> Some (mkGenericUsingDef<FSharpOptionSerializer<_>> typ)
| IsSet typ -> Some (mkGenericUsingDef<FSharpSetSerializer<_>> typ)
| IsUnion typ -> Some (mkGeneric<FSharpUnionSerializer<_>> [| typ |])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(* Copyright (c) 2013 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

namespace FSharp.MongoDB.Bson.Serialization.Serializers

open System.Diagnostics
open MongoDB.Bson
open MongoDB.Bson.Serialization
open MongoDB.Bson.Serialization.Serializers

/// <summary>
/// Serializer for F# voption types that writes the value in the <c>ValueSome</c> case and <c>null</c> in
/// the <c>None</c> case.
/// </summary>
type FSharpValueOptionSerializer<'T>() =
inherit SerializerBase<'T voption>()

let serializer = lazy (BsonSerializer.LookupSerializer<'T>())

override _.Serialize (context, args, value) =
let writer = context.Writer

match value with
| ValueSome x -> serializer.Value.Serialize(context, args, x :> obj)
| ValueNone -> writer.WriteNull()

override _.Deserialize (context, args) =
let reader = context.Reader

match reader.GetCurrentBsonType() with
| BsonType.Null -> reader.ReadNull(); ValueNone
| _ -> ValueSome (serializer.Value.Deserialize(context, args))
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(* Copyright (c) 2015 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)

namespace FSharp.MongoDB.Bson.Tests.Serialization

open MongoDB.Bson

open FsUnit
open NUnit.Framework

module FSharpOptionSerialization =

type Primitive =
{ Bool : bool option
Int : int option
String : string option
Float : float option }

[<Test>]
let ``test serialize optional primitives (none) in a record type``() =
let value = { Bool = None
Int = None
String = None
Float = None }

let result = serialize value
let expected = BsonDocument()

result |> should equal expected

[<Test>]
let ``test deserialize optional primitives (none) in a record type)``() =
let doc = BsonDocument()

let result = deserialize doc typeof<Primitive>
let expected = { Bool = None
Int = None
String = None
Float = None }

result |> should equal expected

[<Test>]
let ``test serialize optional primitives (some) in a record type``() =
let value = { Bool = Some false
Int = Some 0
String = Some "0.0"
Float = Some 0.0 }

let result = serialize value
let expected = BsonDocument([ BsonElement("Bool", BsonBoolean false)
BsonElement("Int", BsonInt32 0)
BsonElement("String", BsonString "0.0")
BsonElement("Float", BsonDouble 0.0) ])

result |> should equal expected

[<Test>]
let ``test deserialize optional primitives (some) in a record type``() =
let doc = BsonDocument([ BsonElement("Bool", BsonBoolean true)
BsonElement("Int", BsonInt32 1)
BsonElement("String", BsonString "1.0")
BsonElement("Float", BsonDouble 1.0) ])

let result = deserialize doc typeof<Primitive>
let expected = { Bool = Some true
Int = Some 1
String = Some "1.0"
Float = Some 1.0 }

result |> should equal expected

0 comments on commit 4c25eca

Please sign in to comment.