Skip to content

Commit

Permalink
warnings as error + remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Dec 28, 2024
1 parent 9b063a2 commit 44d5066
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 10 additions & 6 deletions src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ module private Helpers =
/// <summary>
/// Returns <c>Some typ</c> when <c>typ</c> is a record type, and <c>None</c> otherwise.
/// </summary>
let (|IsRecord|_|) typ =
let isRecord typ = typ <> null && FSharpType.IsRecord(typ, bindingFlags)
whenType isRecord typ
let (|IsRecord|_|) = function
| Null -> None
| NonNull typ ->
let isRecord typ = FSharpType.IsRecord(typ, bindingFlags)
whenType isRecord typ

/// <summary>
/// Returns <c>Some typ</c> when <c>typ</c> is a top-level union type or when it represents a
/// particular union case, and <c>None</c> otherwise.
/// </summary>
let (|IsUnion|_|) (typ:System.Type) =
let isUnion typ = typ <> null && FSharpType.IsUnion(typ, bindingFlags)
whenType isUnion typ
let (|IsUnion|_|) = function
| Null -> None
| NonNull typ ->
let isUnion typ = FSharpType.IsUnion(typ, bindingFlags)
whenType isUnion typ

/// <summary>
/// Returns true if <c>typ</c> is a generic type with defintion <c>'GenericType</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ module FSharpValueSerializer =
interface IBsonSerializationProvider with

member __.GetSerializer typ =
let mkSerializer = function
| Some (typ:System.Type) -> System.Activator.CreateInstance typ :?> IBsonSerializer
| None -> null
let mkSerializer typ =
typ
|> Option.map (fun typ -> System.Activator.CreateInstance typ :?> IBsonSerializer)
|> Option.toObj

match typ with
| IsList typ -> Some (mkGenericUsingDef<FSharpListSerializer<_>> typ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ module FSharpListSerialization =
let value = { MaybeStrings = [ Some "a"; None; Some "z" ] }

let result = serialize value
let expected = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ])
let expected =
let values: (string|null) array = [| "a"; null; "z" |]
BsonDocument("MaybeStrings", BsonArray values)

result |> should equal expected

[<Test>]
let ``test deserialize a list of optional strings``() =
let doc = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ])
let doc =
let values: (string | null) array = [| "a"; null; "z" |]
BsonDocument("MaybeStrings", BsonArray values)

let result = deserialize doc typeof<Record>
let expected = { MaybeStrings = [ Some "a"; None; Some "z" ] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ module FSharpSetSerialization =
let value = { MaybeStrings = Set.ofList [ Some "a"; None; Some "z" ] }

let result = serialize value
let expected = BsonDocument("MaybeStrings", BsonArray (Set.ofList ["a"; null; "z"]))
let expected =
let values: (string|null) list = [ "a"; null; "z" ]
BsonDocument("MaybeStrings", BsonArray (Set.ofList values))

result |> should equal expected

[<Test>]
let ``test deserialize a set of optional strings``() =
let doc = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ])
let doc =
let values: (string|null) list = [ "a"; null; "z" ]
BsonDocument("MaybeStrings", BsonArray values)

let result = deserialize doc typeof<Record>
let expected = { MaybeStrings = Set.ofList [ Some "a"; None; Some "z" ] }
Expand Down

0 comments on commit 44d5066

Please sign in to comment.