-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start implementing isomorphic serialization between cs/fs
- Loading branch information
Showing
8 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace CsDataModel; | ||
using MongoDB.Bson; | ||
|
||
public record Pair | ||
{ | ||
public required int First { get; init; } | ||
public required string? Second { get; init; } | ||
} | ||
|
||
|
||
public record Value | ||
{ | ||
public record IntValue(int Value) : Value; | ||
public record StringValue(string Value) : Value; | ||
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 Pair? RecordOpt { get; init; } | ||
|
||
public required Dictionary<string, int> Map { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
module FSharp.MongoDB.Driver.Isomorphic.Tests | ||
open MongoDB.Driver | ||
open FsUnit | ||
open NUnit.Framework | ||
open CsDataModel; | ||
open FsDataModel; | ||
open MongoDB.Bson | ||
|
||
let mutable client: MongoClient = Unchecked.defaultof<MongoClient> | ||
let mutable db: IMongoDatabase = Unchecked.defaultof<IMongoDatabase> | ||
|
||
[<OneTimeSetUp>] | ||
let init() = | ||
let connectionString = "mongodb://localhost" | ||
let dbname = "FSharp-MongoDB-Driver" | ||
client <- new MongoClient(connectionString) | ||
db <- client.GetDatabase(dbname) | ||
db.DropCollection("IsomophicDataModel") | ||
FSharp.MongoDB.Driver.Register() | ||
|
||
[<OneTimeTearDown>] | ||
let teardown() = | ||
client.Dispose() | ||
|
||
[<Test>] | ||
let ``Isomorphic Some``() = | ||
|
||
let csModel = | ||
let map = | ||
let map = System.Collections.Generic.Dictionary<string, int>() | ||
map.Add("1", 1) | ||
map.Add("2", 2) | ||
map | ||
|
||
RecordDataModel( | ||
Int = 42, | ||
IntOpt = 666, | ||
|
||
String = "String", | ||
StringOpt = "StringOpt", | ||
|
||
Array = [| 1; 2; 3 |], | ||
ArrayOpt = [| 5; 6; 7; 8 |], | ||
|
||
Value = CsDataModel.Value.IntValue(42), | ||
ValueOpt = CsDataModel.Value.StringValue("ValueStringOpt"), | ||
|
||
ValueArray = [| CsDataModel.Value.IntValue(42) | ||
CsDataModel.Value.StringValue("String") | ||
CsDataModel.Value.PairValue(CsDataModel.Pair(First = 99, Second = "SecondPair")) |], | ||
ValueArrayOpt = [| CsDataModel.Value.IntValue(101) |], | ||
|
||
Record = CsDataModel.Pair(First = 1, Second = "Second"), | ||
RecordOpt = CsDataModel.Pair(First = -1, Second = "SecondOpt"), | ||
|
||
Map = map) | ||
|
||
|
||
let fsModel = | ||
{ Id = ObjectId() | ||
|
||
Int = 42 | ||
IntOpt = Some 666 | ||
|
||
String = "String" | ||
StringOpt = Some "StringOpt" | ||
|
||
Array = [| 1; 2; 3 |] | ||
ArrayOpt = Some [| 5; 6; 7; 8 |] | ||
|
||
Value = Value.IntValue 42 | ||
ValueOpt = Some <| Value.StringValue "ValueStringOpt" | ||
|
||
ValueArray = [| Value.IntValue 42; Value.StringValue "String"; Value.PairValue { First = 99; Second = Some "SecondPair" } |] | ||
ValueArrayOpt = Some [| Value.IntValue 101 |] | ||
|
||
Record = { First = 1; Second = Some "Second" } | ||
RecordOpt = Some { First = -1; Second = Some "SecondOpt" } | ||
|
||
Map = Map [ "1", 1; "2", 2 ] } | ||
|
||
let csCollection = db.GetCollection<CsDataModel.RecordDataModel> "IsomophicDataModel" | ||
csCollection.InsertOne(csModel) | ||
|
||
let fsCollection = db.GetCollection<FsDataModel.RecordDataModel> "IsomophicDataModel" | ||
let fromDb = fsCollection.Find(fun x -> x.Id = csModel.Id).First() | ||
fromDb |> should equal fsModel | ||
|
||
[<Test>] | ||
let ``Isomorphic None``() = | ||
|
||
let csModel = | ||
let map = | ||
let map = System.Collections.Generic.Dictionary<string, int>() | ||
map.Add("1", 1) | ||
map.Add("2", 2) | ||
map | ||
|
||
RecordDataModel( | ||
Int = 42, | ||
|
||
String = "String", | ||
|
||
Array = [| 1; 2; 3 |], | ||
|
||
Value = CsDataModel.Value.IntValue(42), | ||
|
||
ValueArray = [| CsDataModel.Value.IntValue(42) | ||
CsDataModel.Value.StringValue("String") | ||
CsDataModel.Value.PairValue(CsDataModel.Pair(First = 99, Second = "SecondPair")) |], | ||
|
||
Record = CsDataModel.Pair(First = 1, Second = "Second"), | ||
|
||
Map = map) | ||
|
||
|
||
let fsModel = | ||
{ Id = ObjectId() | ||
|
||
Int = 42 | ||
IntOpt = None | ||
|
||
String = "String" | ||
StringOpt = None | ||
|
||
Array = [| 1; 2; 3 |] | ||
ArrayOpt = None | ||
|
||
Value = Value.IntValue 42 | ||
ValueOpt = None | ||
|
||
ValueArray = [| Value.IntValue 42; Value.StringValue "String"; Value.PairValue { First = 99; Second = Some "SecondPair" } |] | ||
ValueArrayOpt = None | ||
|
||
Record = { First = 1; Second = Some "Second" } | ||
RecordOpt = None | ||
|
||
Map = Map [ "1", 1; "2", 2 ] } | ||
|
||
let csCollection = db.GetCollection<CsDataModel.RecordDataModel> "IsomophicDataModel" | ||
csCollection.InsertOne(csModel) | ||
|
||
let fsCollection = db.GetCollection<FsDataModel.RecordDataModel> "IsomophicDataModel" | ||
let fromDb = fsCollection.Find(fun x -> x.Id = csModel.Id).First() | ||
fromDb |> should equal fsModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace FsDataModel | ||
open MongoDB.Bson; | ||
|
||
type Pair = | ||
{ First: int | ||
Second: string option } | ||
|
||
[<RequireQualifiedAccess>] | ||
type Value = | ||
| IntValue of Value:int | ||
| StringValue of Value: string | ||
| PairValue of Value:Pair | ||
|
||
type RecordDataModel = | ||
{ Id: ObjectId | ||
|
||
Int: int | ||
IntOpt: int option | ||
|
||
String: string | ||
StringOpt: string option | ||
|
||
Array: int array | ||
ArrayOpt: int array option | ||
|
||
Value: Value | ||
ValueOpt: Value option | ||
|
||
ValueArray: Value array | ||
ValueArrayOpt: Value array option | ||
|
||
Record: Pair | ||
RecordOpt: Pair option | ||
|
||
Map: Map<string, int> } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="DataModel.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters