Skip to content

NRT Support #25

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

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/on-push-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET Core
uses: actions/[email protected]
with:
dotnet-version: 9.0.100
dotnet-version: 9.0.203

- name: Build & Test
run: make test config=Release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-push-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Setup .NET Core
uses: actions/[email protected]
with:
dotnet-version: 9.0.100
dotnet-version: 9.0.203

- name: Extract Version Suffix
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-release-published.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup .NET Core
uses: actions/[email protected]
with:
dotnet-version: 9.0.100
dotnet-version: 9.0.203

- name: Download Release artifacts
uses: robinraju/[email protected]
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ test:

nuget:
dotnet pack -c $(config) -p:Version=$(version) -o .out

upgrade:
dotnet restore --force-evaluate
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Repository origins are:

## Supported platforms

This project targets `netstandard2.1` ([compatible runtimes](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1#select-net-standard-version)).
This project targets `.net 8`, `.net 9` and `netstandard2.1` ([compatible runtimes](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1#select-net-standard-version))

:warning: NRT support starts with `.net sdk 9.0.200`. F# compiler in .net sdk 9.0.10x does not set correctly nullable attributes on F# types. NRT are not supported on `netstandard2.1`.

## Contributing
* If you have a question about the library, then create an [issue][issues] with the `question` label.
Expand Down Expand Up @@ -89,7 +91,9 @@ NRT are serialized as:
* `null` if `Null`
* `object` if `NonNull` object

:warning: As of now, NRT can't be considered `null` when deserializing if value is missing.
On deserialization, missing value is mapped to `null`.

:warning: NRT support starts with .net sdk 9.0.200. F# compiler in .net sdk 9.0.10x does not set correctly nullable attributes on F# types.

# License
The contents of this library are made available under the [Apache License, Version 2.0][license].
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "9.0.203"
}
}
6 changes: 3 additions & 3 deletions src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0;netstandard2.1</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="3.1.0" />
<PackageReference Include="MongoDB.Bson" Version="3.4.0" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type FSharpRecordConvention() =
match classMap.ClassType with
| IsRecord typ ->
let fields = FSharpType.GetRecordFields(typ, bindingFlags)
let names = fields |> Array.map (fun x -> x.Name)
let names = fields |> Array.map _.Name

// Map the constructor of the record type.
let ctor = FSharpValue.PreComputeRecordConstructorInfo(typ, bindingFlags)
classMap.MapConstructor(ctor, names) |> ignore

// Map each field of the record type.
fields |> Array.iter (classMap.MapMember >> ignore)
fields |> Array.iter (mapMemberNullable classMap)
| _ -> ()
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type UnionCaseConvention() =

let mapUnionCase (classMap:BsonClassMap) (unionCase:UnionCaseInfo) =
let fields = unionCase.GetFields()
let names = fields |> Array.map (fun x -> x.Name)
let names = fields |> Array.map _.Name

classMap.SetDiscriminator unionCase.Name
classMap.SetDiscriminatorIsRequired true
Expand All @@ -76,7 +76,7 @@ type UnionCaseConvention() =
classMap.MapCreator(del, names) |> ignore

// Map each field of the union case.
fields |> Array.iter (classMap.MapMember >> ignore)
fields |> Array.iter (mapMemberNullable classMap)

interface IClassMapConvention with
member _.Apply classMap =
Expand Down
17 changes: 17 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,10 @@ module private Helpers =

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

#if !NETSTANDARD2_1
let nrtContext = NullabilityInfoContext()
#endif

/// <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 +94,16 @@ 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>

/// <summary>
/// Maps a member of a <c>BsonClassMap</c> to a nullable value if possible.
/// </summary>
let mapMemberNullable (classMap: BsonClassMap) (propertyInfo: PropertyInfo) =
let memberMap = classMap.MapMember(propertyInfo)
#if !NETSTANDARD2_1
let nrtInfo = nrtContext.Create(propertyInfo)
if nrtInfo.ReadState = NullabilityState.Nullable then
memberMap.SetDefaultValue(objnull) |> ignore
#else
memberMap.SetDefaultValue(objnull) |> ignore
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module FSharp =
member _.GetSerializer typ =
let mkSerializer typ =
typ
|> Option.map (fun typ -> System.Activator.CreateInstance typ :?> IBsonSerializer)
|> Option.map (fun typ -> System.Activator.CreateInstance typ |> nonNull :?> IBsonSerializer)
|> Option.toObj

match typ with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ type FSharpOptionSerializer<'T when 'T: not null>() =

match reader.GetCurrentBsonType() with
| BsonType.Null -> reader.ReadNull(); None
| _ -> Some (serializer.Value.Deserialize(context, args))
| t -> Some (serializer.Value.Deserialize(context, args))
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type FSharpUnionSerializer<'T>() =
let mkClassMapSerializer (caseType:System.Type) =
let classMap = BsonClassMap.LookupClassMap caseType
let serializerType = typedefof<BsonClassMapSerializer<_>>.MakeGenericType [| caseType |]
System.Activator.CreateInstance(serializerType, classMap) :?> IBsonSerializer
System.Activator.CreateInstance(serializerType, classMap) |> nonNull :?> IBsonSerializer

// 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
4 changes: 2 additions & 2 deletions tests/CSharpDataModels/CSharpDataModels.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
Expand All @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="3.1.0" />
<PackageReference Include="MongoDB.Bson" Version="3.4.0" />
</ItemGroup>

</Project>
38 changes: 19 additions & 19 deletions tests/CSharpDataModels/DataModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ public record PairValue(Pair Value) : Value;

public record RecordDataModel
{
public ObjectId Id { get; init; }

public required int Int { get; init; }
public int? IntOpt { get; init; }

public required string String { get; init; }
public string? StringOpt { get; init; }

public required int[] Array { get; init; }
public int[]? ArrayOpt { get; init; }

public required Value Value { get; init; }
public Value? ValueOpt { get; init; }

public required Value[] ValueArray { get; init; }
public Value[]? ValueArrayOpt { get; init; }

public required Pair Record { get; init; }
// public ObjectId Id { get; init; }
//
// public required int Int { get; init; }
// public int? IntOpt { get; init; }
//
// public required string String { get; init; }
// public string? StringOpt { get; init; }
//
// public required int[] Array { get; init; }
// public int[]? ArrayOpt { get; init; }

// public required Value Value { get; init; }
// public Value? ValueOpt { get; init; }

// public required Value[] ValueArray { get; init; }
// public Value[]? ValueArrayOpt { get; init; }

// public required Pair Record { get; init; }
public Pair? RecordOpt { get; init; }

public required Dictionary<string, int> Map { get; init; }
// public required Dictionary<string, int> Map { get; init; }
}
15 changes: 6 additions & 9 deletions tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
Expand All @@ -20,20 +20,17 @@
<Compile Include="Isomorphic\IsomorphicTests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\FSharp.MongoDB.Bson\FSharp.MongoDB.Bson.fsproj" />
<ProjectReference Include="..\CSharpDataModels\CSharpDataModels.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsUnit" Version="6.0.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="FsUnit" Version="7.0.1" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
<PackageReference Include="MongoDB.Bson" Version="3.4.0" />
</ItemGroup>

</Project>
Loading
Loading