Skip to content

Commit 70f2be5

Browse files
committed
code refactoring
1 parent df62bbc commit 70f2be5

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

src/FSharp.MongoDB.Bson/Serialization/Conventions/FSharpRecordConvention.fs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ open System.Reflection
2727
type FSharpRecordConvention() =
2828
inherit ConventionBase()
2929

30-
static let nrtContext = NullabilityInfoContext()
31-
3230
interface IClassMapConvention with
3331
member _.Apply classMap =
3432
match classMap.ClassType with
@@ -41,9 +39,5 @@ type FSharpRecordConvention() =
4139
classMap.MapConstructor(ctor, names) |> ignore
4240

4341
// Map each field of the record type.
44-
fields |> Array.iter (fun pi ->
45-
let memberMap = classMap.MapMember(pi)
46-
let nrtInfo = nrtContext.Create(pi)
47-
if nrtInfo.WriteState = NullabilityState.Nullable then
48-
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore)
42+
fields |> Array.iter (mkMemberNullable classMap)
4943
| _ -> ()

src/FSharp.MongoDB.Bson/Serialization/Conventions/UnionCaseConvention.fs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ open MongoDB.Bson.Serialization.Helpers
3030
type UnionCaseConvention() =
3131
inherit ConventionBase()
3232

33-
static let nrtContext = NullabilityInfoContext()
34-
3533
let tryGetUnionCase (typ:System.Type) =
3634
// 8.5.4. Compiled Form of Union Types for Use from Other CLI Languages
3735
// A compiled union type U has [o]ne CLI nested type U.C for each non-null union case C.
@@ -78,12 +76,7 @@ type UnionCaseConvention() =
7876
classMap.MapCreator(del, names) |> ignore
7977

8078
// Map each field of the union case.
81-
// Map each field of the record type.
82-
fields |> Array.iter (fun pi ->
83-
let memberMap = classMap.MapMember(pi)
84-
let nrtInfo = nrtContext.Create(pi)
85-
if nrtInfo.WriteState = NullabilityState.Nullable then
86-
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore)
79+
fields |> Array.iter (mkMemberNullable classMap)
8780

8881
interface IClassMapConvention with
8982
member _.Apply classMap =

src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module private Helpers =
2323

2424
let bindingFlags = BindingFlags.Public ||| BindingFlags.NonPublic
2525

26+
let nrtContext = NullabilityInfoContext()
27+
2628
/// <summary>
2729
/// Returns <c>Some typ</c> when <c>pred typ</c> returns true, and <c>None</c> when
2830
/// <c>pred typ</c> returns false.
@@ -90,3 +92,9 @@ module private Helpers =
9092
/// Creates a generic type <c>'T</c> using the generic arguments of <c>typ</c>.
9193
/// </summary>
9294
let mkGenericUsingDef<'T> (typ:System.Type) = typ.GetGenericArguments() |> mkGeneric<'T>
95+
96+
let mkMemberNullable (memberMap: BsonClassMap) (propertyInfo: PropertyInfo) =
97+
let memberMap = memberMap.MapMember(propertyInfo)
98+
let nrtInfo = nrtContext.Create(propertyInfo)
99+
if nrtInfo.WriteState = NullabilityState.Nullable then
100+
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore

0 commit comments

Comments
 (0)