Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/_build
.DS_Store
/doc
/deps
erl_crash.dump
Expand Down
28 changes: 27 additions & 1 deletion lib/bson/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ defmodule BSON.Encoder do
def encode(%BSON.ObjectId{value: <<_::binary(12)>> = value}),
do: value

def encode(%Date{} = date) do
unix_ms =
NaiveDateTime.from_erl!({Date.to_erl(date), {0, 0, 0}}, 0, Calendar.ISO)
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_unix(:millisecond)

<<unix_ms::int64>>
end

def encode(%DateTime{} = datetime) do
unix_ms = DateTime.to_unix(datetime, :millisecond)
<<unix_ms::int64>>
Expand All @@ -40,6 +49,20 @@ defmodule BSON.Encoder do
BSON.Decimal128.encode(decimal)
end

def encode(%Time{} = time) do
value = Time.to_iso8601(time)
[<<byte_size(value) + 1::int32>>, value, 0x00]
end

def encode(%NaiveDateTime{} = datetime) do
unix_ms =
datetime
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_unix(:millisecond)

<<unix_ms::int64>>
end

def encode(%BSON.Regex{pattern: pattern, options: options}),
do: [cstring(pattern) | cstring(options)]

Expand Down Expand Up @@ -119,7 +142,7 @@ defmodule BSON.Encoder do
type = type(value)

value =
if Mongo.Encoder.impl_for(value),
if Mongo.Encoder.impl_for(value) != nil,
do: value |> Mongo.Encoder.encode() |> encode(),
else: value |> encode()

Expand Down Expand Up @@ -152,6 +175,9 @@ defmodule BSON.Encoder do
defp type(%BSON.ObjectId{}), do: @type_objectid
defp type(%DateTime{}), do: @type_datetime
defp type(%Decimal{}), do: @type_decimal128
defp type(%Date{}), do: @type_datetime
defp type(%Time{}), do: @type_string
defp type(%NaiveDateTime{}), do: @type_datetime
defp type(%BSON.Regex{}), do: @type_regex
defp type(%BSON.JavaScript{scope: nil}), do: @type_js
defp type(%BSON.JavaScript{}), do: @type_js_scope
Expand Down
4 changes: 4 additions & 0 deletions lib/mongo/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ end
defimpl Mongo.Encoder, for: Map do
def encode(v), do: v
end

defimpl Mongo.Encoder, for: List do
def encode(v), do: v
end
5 changes: 1 addition & 4 deletions lib/mongo/grid_fs/bucket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,13 @@ defmodule Mongo.GridFs.Bucket do
end

@doc """
Finds one file document with the file_id as a string
Finds one file document with the file_id as a string or as an ObjectID-struct
"""
@spec find_one(Bucket.t(), String.t()) :: BSON.document() | nil
def find_one(%Bucket{} = bucket, file_id) when is_binary(file_id) do
find_one(bucket, ObjectId.decode!(file_id))
end

@doc """
Finds one file document with the file_id as an ObjectID-struct
"""
@spec find_one(Bucket.t(), BSON.ObjectId.t()) :: BSON.document() | nil
def find_one(%Bucket{topology_pid: topology_pid, opts: opts} = bucket, %BSON.ObjectId{} = oid) do
Mongo.find_one(topology_pid, files_collection_name(bucket), %{"_id" => oid}, opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Mongo.Session do
rescue
exception ->
_ = abort_transaction(pid)
reraise exception, System.stacktrace()
reraise exception, __STACKTRACE__
else
val ->
with :ok <- commit_transaction(pid), do: {:ok, val}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Mongodb.Mixfile do
{:jason, "~> 1.0", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev},
{:earmark, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 1.0.0-rc.4", only: :dev, runtime: false}
{:dialyxir, "~> 1.1.0", only: :dev, runtime: false}
]
end

Expand Down
7 changes: 4 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%{
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"},
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
"db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"},
"decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"earmark": {:hex, :earmark, "1.4.10", "bddce5e8ea37712a5bfb01541be8ba57d3b171d3fa4f80a0be9bcf1db417bcaf", [:mix], [{:earmark_parser, ">= 1.4.10", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "12dbfa80810478e521d3ffb941ad9fbfcbbd7debe94e1341b4c4a1b2411c1c27"},
"earmark_parser": {:hex, :earmark_parser, "1.4.10", "6603d7a603b9c18d3d20db69921527f82ef09990885ed7525003c7fe7dc86c56", [:mix], [], "hexpm", "8e2d5370b732385db2c9b22215c3f59c84ac7dda7ed7e544d7c459496ae519c0"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
Expand All @@ -11,4 +11,5 @@
"makeup": {:hex, :makeup, "1.0.3", "e339e2f766d12e7260e6672dd4047405963c5ec99661abdc432e6ec67d29ef95", [:mix], [{:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "2e9b4996d11832947731f7608fed7ad2f9443011b3b479ae288011265cdd3dad"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"},
"nimble_parsec": {:hex, :nimble_parsec, "0.6.0", "32111b3bf39137144abd7ba1cce0914533b2d16ef35e8abc5ec8be6122944263", [:mix], [], "hexpm", "27eac315a94909d4dc68bc07a4a83e06c8379237c5ea528a9acff4ca1c873c52"},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
}