Skip to content

Commit

Permalink
fix: resolve serialization errors in .NET examples (#37)
Browse files Browse the repository at this point in the history
This ended up being a *slightly* bigger commit than I anticipated. But,
I essentially did two things:

- Annotate all `*Result` types in `Mondocks.Net/Types.fs` with
  `[<BsonIgnoreExtraElements>]`; otherwise, the .NET MongoDB Driver
  freaks out that the schema doesn't match exactly.

- Avoid serializing each `Index` object in the `IndexBuilder`.
  When I first ran the example in `samples/Indexes`, it failed with this
  exception:

  ```sh
  Unhandled exception. MongoDB.Driver.MongoCommandException: Command createIndexes failed: BSON field 'createIndexes.indexes.0' is the wrong type 'string', expected type 'object'.
  ```
  So we DON'T need the serialization step in the `IndexBuilder` anymore
  (though I kept the interface the same).

Finally, I added a Nix flake with a very simple `devShell` because
that's just what I'm used to developing with.
  • Loading branch information
moonshxne committed Sep 26, 2023
1 parent 0cea36e commit ee52d66
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"fantomas": {
"version": "5.1.3",
"version": "6.2.1",
"commands": [
"fantomas"
]
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -442,4 +442,7 @@ $RECYCLE.BIN/
!.vscode/extensions.json

*.fs.js
*.mjs
*.mjs

# Direnv
.envrc
64 changes: 64 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions flake.nix
@@ -0,0 +1,25 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
};
outputs = { self, nixpkgs, flake-parts }@inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
perSystem = { system, pkgs, ... }: {
# Basically "patches" the `pkgs` input to `perSystem`
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
config.allowUnfreePredicate = pkg: builtins.elem (nixpkgs.lib.getName pkg) [ "mongodb" "mongodb-compass" ];
};
formatter = pkgs.nixfmt;
devShells.default = pkgs.mkShell {
buildInputs = with pkgs;
[ dotnet-sdk ];
shellHook = ''
dotnet tool restore
'';
};
};
systems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" ];
};
}
2 changes: 1 addition & 1 deletion src/Mondocks.Net/Mondocks.Net.fsproj
Expand Up @@ -19,4 +19,4 @@
<ItemGroup>
<ProjectReference Include="..\Mondocks\Mondocks.fsproj" />
</ItemGroup>
</Project>
</Project>
7 changes: 7 additions & 0 deletions src/Mondocks.Net/Types.fs
@@ -1,15 +1,22 @@
namespace Mondocks.Types

open MongoDB.Bson
open MongoDB.Bson.Serialization.Attributes

[<BsonIgnoreExtraElements>]
type Cursor<'T> = { firstBatch: seq<'T>; ns: string }

[<BsonIgnoreExtraElements>]
type FindResult<'T> = { cursor: Cursor<'T>; ok: float }

[<BsonIgnoreExtraElements>]
type InsertResult = { n: int; ok: float }

[<BsonIgnoreExtraElements>]
type UpdateResult = { n: int; nModified: int; ok: float }

[<BsonIgnoreExtraElements>]
type DeleteResult = { n: int; ok: float }

[<BsonIgnoreExtraElements>]
type CreateIndexResult =
Expand Down
2 changes: 1 addition & 1 deletion src/Mondocks/Database.fs
Expand Up @@ -68,7 +68,7 @@ type IndexBuilder(name: string, serialize: SerializerFn) =
wildcardProjection = None }

member inline this.Run(state: Index<'PartialFilterExpression, 'StorageEngine, 'Weights, 'WildcardProjection>) =
this.Serialize { state with name = this.Name }
{ state with name = this.Name }

[<CustomOperation("key")>]
member inline __.Key
Expand Down

0 comments on commit ee52d66

Please sign in to comment.