SuperWorker.Supervisor.Validator (SuperWorker v0.3.6)

View Source

Utility functions for SuperWorker.Supervisor.

This module provides common utility functions used across the supervisor implementation, including:

  • Option normalization and validation
  • API call helpers
  • Hashing and partitioning utilities
  • Process information helpers

Summary

Functions

Checks that the value of a key in options passes a validation function.

Computes a hash-based order for partitioning data.

Converts a keyword option shorthand to a full keyword tuple.

Types

api_result()

@type api_result() :: {:ok, any()} | {:error, any()}

timeout_ms()

@type timeout_ms() :: non_neg_integer() | :infinity

Functions

check_type(opts, key, validator_fun)

@spec check_type(map(), atom(), (any() -> boolean())) :: api_result()

Checks that the value of a key in options passes a validation function.

Examples

iex> check_type(%{count: 5}, :count, &is_integer/1)
{:ok, %{count: 5}}

iex> check_type(%{count: "five"}, :count, &is_integer/1)
{:error, :invalid_type}

get_hash_order(term, num_partitions)

@spec get_hash_order(term(), pos_integer()) :: non_neg_integer()

Computes a hash-based order for partitioning data.

Uses Erlang's phash2 for consistent hashing across the cluster.

Examples

iex> order = get_hash_order("my_data", 10)
iex> order >= 0 and order < 10
true

get_keyword(arg1)

@spec get_keyword(atom()) :: {:type, atom()} | {:error, :invalid_options}

Converts a keyword option shorthand to a full keyword tuple.

Used for converting shorthand like :group to {:type, :group}.

normalize_options(opts, allowed_params)

@spec normalize_options([keyword() | atom()], [atom()]) :: api_result()

validate_and_convert(options)