macula_utils (macula v0.20.5)

View Source

Common utility functions for Macula.

This module contains pure utility functions used across the Macula codebase to improve testability and eliminate duplication.

All functions in this module are pure (no side effects) and can be tested independently.

Summary

Functions

Decode JSON binary to map/list.

Encode map/list to JSON binary.

Ensure value is binary.

Generate a random node ID.

Get next message ID from counter. Returns {MessageId, NewCounter}.

Normalize provider map from binary keys to atom keys.

Parse host:port string with default port.

Parse URL to extract host and port.

Check if a published topic matches a subscription topic pattern. Supports configurable wildcards (defaults: dot-separated with * and **): - WildcardSingle (e.g., '*') matches a single segment - WildcardMulti (e.g., '**') matches multiple segments - Separator (e.g., '.') splits topic into segments - exact match otherwise

Functions

decode_json(Binary)

-spec decode_json(binary()) -> map() | list().

Decode JSON binary to map/list.

encode_json(Data)

-spec encode_json(map() | list()) -> binary().

Encode map/list to JSON binary.

ensure_binary(B)

-spec ensure_binary(binary() | list() | atom()) -> binary().

Ensure value is binary.

generate_node_id()

-spec generate_node_id() -> binary().

Generate a random node ID.

next_message_id(Counter)

-spec next_message_id(non_neg_integer()) -> {binary(), non_neg_integer()}.

Get next message ID from counter. Returns {MessageId, NewCounter}.

normalize_provider(Provider)

-spec normalize_provider(map()) -> map().

Normalize provider map from binary keys to atom keys.

parse_host_port(HostPort, DefaultPort)

-spec parse_host_port(string(), inet:port_number()) -> {string(), inet:port_number()}.

Parse host:port string with default port.

parse_url(Url)

-spec parse_url(binary()) -> {string(), inet:port_number()}.

Parse URL to extract host and port.

topic_matches(Pattern, Topic, Separator, WildcardSingle, WildcardMulti)

-spec topic_matches(binary() | list(), binary() | list(), binary(), binary(), binary()) -> boolean().

Check if a published topic matches a subscription topic pattern. Supports configurable wildcards (defaults: dot-separated with * and **): - WildcardSingle (e.g., '*') matches a single segment - WildcardMulti (e.g., '**') matches multiple segments - Separator (e.g., '.') splits topic into segments - exact match otherwise