ExSift.Collection protocol (ex_sift v0.2.0)

Protocol for types that can be queried by ExSift.

Implement this protocol to make any collection type work with ExSift's filtering and query matching. By default, maps, lists, MapSets, and any struct are supported.

Implementing for custom types

defimpl ExSift.Collection, for: MyStruct do
  def fetch(collection, key), do: Map.get(collection, key)
  def to_pairs(collection), do: Map.to_list(collection)
  def member?(collection, value), do: value in collection.items
  def size(collection), do: length(collection.items)
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Fetch a value by key. Returns the value or nil if not found.

Check if a value is a member of the collection. Used for $in and $all.

Return the size/count of the collection. Used for $size.

Convert collection to a list of {key, value} pairs for shape matching. Return nil if the collection cannot be used as a key-value structure.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

fetch(collection, key)

@spec fetch(t(), term()) :: term()

Fetch a value by key. Returns the value or nil if not found.

member?(collection, value)

@spec member?(t(), term()) :: boolean()

Check if a value is a member of the collection. Used for $in and $all.

size(collection)

@spec size(t()) :: non_neg_integer()

Return the size/count of the collection. Used for $size.

to_pairs(collection)

@spec to_pairs(t()) :: [{term(), term()}] | nil

Convert collection to a list of {key, value} pairs for shape matching. Return nil if the collection cannot be used as a key-value structure.