zookzook / elixir-mongodb-driver

MongoDB driver for Elixir
Apache License 2.0
244 stars 61 forks source link

Typing for Update functions #167

Closed RoboZoom closed 1 year ago

RoboZoom commented 1 year ago

The fourth argument for the Mongo.update/5 functions is spec'd to be a BSON document, however MongoDb will also accept an aggregation pipeline. When using a pipeline, Dialyzer provides the following error:

lib/dx_app/mongo_adapter/utils.ex:56:call
The function call will not succeed.

Mongo.update_one(
  any(),
  _collection :: any(),
  %{:id => _},
  _update_map :: [
    %{
      :"$set" => %{
        atom() => %{
          :"$cond" => [
            %{
              :"$concatArrays" => [<<_::8, _::size(1)>> | [map(), ...], ...],
              :"$in" => [any(), ...],
              :"$map" => %{
                :as => <<_::16>>,
                :in => %{:"$mergeObjects" => [<<_::32>> | %{:"$cond" => [map(), ...]}, ...]},
                :input => <<_::8, _::size(1)>>
              }
            },
            ...
          ]
        }
      }
    },
    ...
  ]
)

will never return since the 4th arguments differ
from the success typing arguments:

(
  atom() | pid() | {atom(), _} | {:via, atom(), _},
  binary(),
  [{atom() | binary(), atom() | binary() | [any()] | number() | %{atom() | binary() => _}}]
  | %{
      atom()
      | binary() => atom() | binary() | [any()] | number() | %{atom() | binary() => _}
    },
  [{atom() | binary(), atom() | binary() | [any()] | number() | %{atom() | binary() => _}}]
  | %{
      atom()
      | binary() => atom() | binary() | [any()] | number() | %{atom() | binary() => _}
    }
)

In practice, the query works as intended.

zookzook commented 1 year ago

If you like, you could provide a PR to fix the spec. I don't really care about the specs, so PRs are welcome to fix those specs.

zookzook commented 1 year ago

I changed the specs in the master branch. Feel free to try it out.

zookzook commented 1 year ago

Closing due to no response.