Skip to content

Commit

Permalink
- Implement a decoder for batch get document struct instead of parsin…
Browse files Browse the repository at this point in the history
…g it inside the repo method.

- Include the path for each document since the order of the response isn't guaranteed to match the input.
- Since we include each path for each requested document, it's better not to filter out missing documents. Include them with value `nil` for document.
- Fix the capability of passing body params
  • Loading branch information
MajdSehwail committed Jul 25, 2024
1 parent 0397282 commit 8d4c01c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
11 changes: 11 additions & 0 deletions lib/firestore/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ defimpl Firestore.Decoder, for: GoogleApi.Firestore.V1.Model.Empty do
def decode(_response), do: {:ok, []}
end

defimpl Firestore.Decoder, for: GoogleApi.Firestore.V1.Model.BatchGetDocumentsResponse do
def decode(%{found: document, missing: nil}) do
{:ok, decoded_document} = Firestore.Decoder.decode(document)
%{path: document.name, document: decoded_document}
end

def decode(%{found: nil, missing: missing}) do
%{path: missing, document: nil}
end
end

defimpl Firestore.Decoder, for: GoogleApi.Firestore.V1.Model.ListDocumentsResponse do
def decode(%{documents: docs, nextPageToken: next}) do
response = %{documents: Enum.map(docs, &decode/1), nextPageToken: next}
Expand Down
29 changes: 8 additions & 21 deletions lib/firestore/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ defmodule Firestore.Repo do
# ...
]
"""
require Logger

@type t() :: module()

Expand Down Expand Up @@ -84,8 +83,6 @@ defmodule Firestore.Repo do
Enum.each(opts, &validate_option/1)

quote bind_quoted: [opts: opts] do
require Logger

@behaviour Firestore.Repo

@otp_app opts[:otp_app]
Expand Down Expand Up @@ -115,26 +112,15 @@ defmodule Firestore.Repo do
end
end

def batch_get_documents(document_paths, params \\ []) do
payload = %{documents: Enum.map(document_paths, &build_document_path/1)}
params = Keyword.put(params, :body, payload)
def batch_get_documents(document_paths, params \\ %{}) do
params =
params
|> Map.put(:documents, Enum.map(document_paths, &build_document_path/1))
|> then(fn params -> Keyword.put([], :body, params) end)

with {:ok, client} <- get_client(),
{:ok, response} <- Firestore.API.batch_get_documents(client, db_path(), params) do
response
|> Enum.filter(fn
%{missing: nil} ->
true

%{missing: missing} ->
Logger.warning("Missing document path: #{inspect(missing)}")
false

other ->
Logger.warning("Unknown response: #{inspect(other)}")
end)
|> Enum.map(fn %{found: document} -> Firestore.Decoder.decode(document) end)
|> Enum.map(fn {:ok, document} -> document end)
Firestore.Decoder.decode(response)
end
end

Expand All @@ -158,7 +144,8 @@ defmodule Firestore.Repo do
Firestore.API.update_document(
client,
build_document_path(document_path),
Keyword.put([], :body, Firestore.Encoder.encode(payload)) |> should_mask?(params)
Keyword.put([], :body, Firestore.Encoder.encode(payload))
|> should_mask?(params)
) do
Firestore.Decoder.decode(response)
end
Expand Down

0 comments on commit 8d4c01c

Please sign in to comment.