Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Jan 1, 2025
1 parent df62bbc commit 70f2be5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ open System.Reflection
type FSharpRecordConvention() =
inherit ConventionBase()

static let nrtContext = NullabilityInfoContext()

interface IClassMapConvention with
member _.Apply classMap =
match classMap.ClassType with
Expand All @@ -41,9 +39,5 @@ type FSharpRecordConvention() =
classMap.MapConstructor(ctor, names) |> ignore

// Map each field of the record type.
fields |> Array.iter (fun pi ->
let memberMap = classMap.MapMember(pi)
let nrtInfo = nrtContext.Create(pi)
if nrtInfo.WriteState = NullabilityState.Nullable then
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore)
fields |> Array.iter (mkMemberNullable classMap)
| _ -> ()
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ open MongoDB.Bson.Serialization.Helpers
type UnionCaseConvention() =
inherit ConventionBase()

static let nrtContext = NullabilityInfoContext()

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

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

interface IClassMapConvention with
member _.Apply classMap =
Expand Down
8 changes: 8 additions & 0 deletions src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module private Helpers =

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

let nrtContext = NullabilityInfoContext()

/// <summary>
/// Returns <c>Some typ</c> when <c>pred typ</c> returns true, and <c>None</c> when
/// <c>pred typ</c> returns false.
Expand Down Expand Up @@ -90,3 +92,9 @@ module private Helpers =
/// Creates a generic type <c>'T</c> using the generic arguments of <c>typ</c>.
/// </summary>
let mkGenericUsingDef<'T> (typ:System.Type) = typ.GetGenericArguments() |> mkGeneric<'T>

let mkMemberNullable (memberMap: BsonClassMap) (propertyInfo: PropertyInfo) =
let memberMap = memberMap.MapMember(propertyInfo)
let nrtInfo = nrtContext.Create(propertyInfo)
if nrtInfo.WriteState = NullabilityState.Nullable then
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore

0 comments on commit 70f2be5

Please sign in to comment.