Skip to content

Commit

Permalink
Fix spec for Xandra.Page (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Oct 24, 2023
1 parent b006240 commit 994a33d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
16 changes: 8 additions & 8 deletions lib/xandra/batch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ defmodule Xandra.Batch do
alias Xandra.{Frame, Prepared, Simple}

@enforce_keys [:type]
defstruct @enforce_keys ++
[
queries: [],
default_consistency: nil,
protocol_module: nil,
compressor: nil,
custom_payload: nil
]
defstruct [
:type,
queries: [],
default_consistency: nil,
protocol_module: nil,
compressor: nil,
custom_payload: nil
]

@type type :: :logged | :unlogged | :counter

Expand Down
23 changes: 16 additions & 7 deletions lib/xandra/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ defmodule Xandra.Page do
or `nil` if no tracing was enabled. See the "Tracing" section in `Xandra.execute/4`.
"""
defstruct [:content, :columns, :paging_state, :tracing_id, :custom_payload]
defstruct [:paging_state, :tracing_id, :custom_payload, :columns, content: []]

@typedoc """
The paging state of a page.
This is intended to be an "opaque" binary value that you can use for further
pagination. See `Xandra.execute/4`.
"""
@type paging_state :: binary
@type paging_state() :: binary()

@typedoc false
@typedoc since: "0.18.0"
@type column() :: {
keyspace :: String.t(),
table :: String.t(),
column_name :: String.t(),
type :: term()
}

@typedoc """
The type for the page struct.
Expand All @@ -58,15 +67,15 @@ defmodule Xandra.Page do
See [`%Xandra.Page{}`](`__struct__/0`).
"""
@type t :: %__MODULE__{
content: list,
columns: nonempty_list,
paging_state: paging_state | nil,
tracing_id: binary | nil,
content: [term()],
columns: [column()] | nil,
paging_state: paging_state() | nil,
tracing_id: binary() | nil,
custom_payload: Xandra.custom_payload() | nil
}

defimpl Enumerable do
def reduce(%{content: content, columns: columns}, acc, fun) do
def reduce(%@for{content: content, columns: columns}, acc, fun) do
reduce(content, columns, acc, fun)
end

Expand Down
14 changes: 11 additions & 3 deletions lib/xandra/page_stream.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule Xandra.PageStream do
@moduledoc false

@type t() :: %__MODULE__{
state: :new | :done | :run,
conn: Xandra.conn(),
params: Xandra.values(),
options: keyword(),
query: Xandra.statement() | Xandra.Prepared.t() | Xandra.Batch.t() | Xandra.Simple.t()
}

defstruct [:conn, :query, :params, :options, state: :new]

defimpl Enumerable do
Expand All @@ -25,15 +33,15 @@ defmodule Xandra.PageStream do
end

defp next(page_stream) do
%{conn: conn, query: query, params: params, options: options} = page_stream
%@for{conn: conn, query: query, params: params, options: options} = page_stream

case Xandra.execute!(conn, query, params, options) do
%Page{paging_state: nil} = page ->
{[page], %{page_stream | state: :done}}
{[page], %@for{page_stream | state: :done}}

%Page{paging_state: paging_state} = page ->
options = Keyword.put(options, :paging_state, paging_state)
{[page], %{page_stream | options: options}}
{[page], %@for{page_stream | options: options}}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/xandra/prepared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Xandra.Prepared do
statement: Xandra.statement(),
values: Xandra.values() | nil,
id: binary | nil,
bound_columns: list | nil,
bound_columns: [Xandra.Page.column()] | nil,
result_columns: list | nil,
default_consistency: atom | nil,
protocol_module: module | nil,
Expand Down
2 changes: 1 addition & 1 deletion lib/xandra/simple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Xandra.Simple do
The fields of this are not meant to be used, but are documented to avoid
Dialyzer warnings.
"""
@type t :: %__MODULE__{
@type t() :: %__MODULE__{
statement: Xandra.statement(),
values: Xandra.values() | nil,
default_consistency: atom() | nil,
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ defmodule Xandra.Mixfile do
{^port, {:exit_status, status}} ->
Mix.raise("Mix failed with exit status #{status}")
after
10_000 ->
Mix.raise("Timed out waiting for Mix to send back any data (after 10s)")
60_000 ->
Mix.raise("Timed out waiting for Mix to send back any data (after 60s)")
end
end

Expand Down

0 comments on commit 994a33d

Please sign in to comment.