Skip to content
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

warnings as error + remove warnings #7

Merged
merged 1 commit into from
Dec 28, 2024
Merged
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
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
Loading