From 0d0a3dc45f1c381f24473d1466542d77144d1d56 Mon Sep 17 00:00:00 2001 From: Michael Maier Date: Thu, 23 Jan 2020 09:26:39 +0100 Subject: [PATCH] bump new version --- CHANGELOG.md | 11 +++++++++++ lib/mongo.ex | 8 +++++++- mix.exs | 2 +- test/mongo_test.exs | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a189609b..03d4ffa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +## 0.6.3 + +* Enhancements + * basic support for inserting structs + * removed duplicated code + * Cursor-API raises a `Mongo.Error` instead of a `FunctionClauseError` + +* Bugfixes + * `:appname` option (typo) #38 + * fixed index creation in `Mongo.GridFs.Bucket` + ## 0.6.2 * Enhancements diff --git a/lib/mongo.ex b/lib/mongo.ex index 243b5c3a..58db7559 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -1093,8 +1093,14 @@ defmodule Mongo do ## # Checks the validity of the document structure. that means either you use binaries or atoms as a key, but not in combination of both. # - # + # todo support for structs defp normalize_doc(doc) do + + #doc = case Map.has_key?(doc, :__struct__) do + # true -> Map.to_list(doc) + # false -> doc + #end + Enum.reduce(doc, {:unknown, []}, fn {key, _value}, {:binary, _acc} when is_atom(key) -> invalid_doc(doc) {key, _value}, {:atom, _acc} when is_binary(key) -> invalid_doc(doc) diff --git a/mix.exs b/mix.exs index bf6115d4..3315871b 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Mongodb.Mixfile do use Mix.Project - @version "0.6.2" + @version "0.6.3" def project() do [app: :mongodb_driver, diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 6d1a2c41..b45939ac 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -1,3 +1,7 @@ +defmodule TestUser do + defstruct name: "John", age: 27 +end + defmodule Mongo.Test do use ExUnit.Case @@ -602,4 +606,19 @@ defmodule Mongo.Test do end + test "save struct", c do + + coll = unique_name() + value = %TestUser{} + {:ok, %Mongo.InsertOneResult{inserted_id: id}} = Mongo.insert_one(c.pid, coll, value) + assert id != nil + + user = Mongo.find_one(c.pid, coll, %{_id: id}) + |> Enum.into(%{}, fn {key, val} -> {String.to_atom(key), val} end) + + user = struct(TestUser, user) + + assert value == user + end + end