PhoenixGenApi.Helpers.Shared (PhoenixGenApi v2.11.0)

Copy Markdown View Source

Shared utility functions used across PhoenixGenApi modules.

This module centralizes common logic that was previously duplicated across ConfigPuller, ConfigReceiver, PushConfig, FunConfig, and NodeSelector.

Summary

Functions

Enforces that a FunConfig's service name matches the expected service.

Ensures a FunConfig has a version string, defaulting to "0.0.0".

Validates that a value is a valid node identifier.

Compares two service names for equality, handling atom↔string comparisons.

Validates a list of nodes, filtering out invalid entries.

Functions

enforce_service_name(config, service_name)

Enforces that a FunConfig's service name matches the expected service.

If the service names don't match, logs a warning and overwrites the FunConfig's service with the expected service name.

Parameters

  • config - A %FunConfig{} struct
  • service_name - The expected service name (atom or string)

Returns

The FunConfig with the correct service name.

ensure_version(config)

Ensures a FunConfig has a version string, defaulting to "0.0.0".

If the FunConfig's version is nil or empty, sets it to "0.0.0".

Parameters

  • config - A %FunConfig{} struct

Returns

The FunConfig with a guaranteed version string.

is_valid_node?(node)

@spec is_valid_node?(any()) :: boolean()

Validates that a value is a valid node identifier.

A valid node is either an atom (e.g., :node1@host) or a binary string (e.g., "node1@host").

Examples

iex> is_valid_node?(:node1@host)
true

iex> is_valid_node?("node1@host")
true

iex> is_valid_node?(123)
false

same_service?(fun_service, push_service)

@spec same_service?(atom() | String.t(), atom() | String.t()) :: boolean()

Compares two service names for equality, handling atom↔string comparisons.

Service names can be atoms or strings throughout the system. This function normalizes the comparison so that :my_service and "my_service" are considered the same service.

Examples

iex> same_service?(:my_service, "my_service")
true

iex> same_service?("my_service", :my_service)
true

iex> same_service?(:my_service, :other_service)
false

validate_nodes(nodes)

@spec validate_nodes(list()) :: list()

Validates a list of nodes, filtering out invalid entries.

Parameters

  • nodes - A list of potential node identifiers

Returns

A list containing only valid node identifiers.