LemonGateway.Shared (lemon_gateway v0.1.0)

View Source

Shared utility functions used across LemonGateway modules.

This module centralizes common helper functions to reduce duplication and provide consistent behavior for configuration access, data normalization, and type conversions across the codebase.

Summary

Functions

Safely broadcasts to LemonCore.Bus if available.

Canonicalizes a hostname by trimming, removing trailing dot, and lowercasing.

Safely emits telemetry events if LemonCore.Telemetry is available.

Fetches a value from a map or keyword list, trying both atom and string keys.

Fetches a nested value from a map by trying multiple key paths. Returns the first non-nil value found.

Fetches a value from a nested map structure following a path of keys.

Returns the first non-blank value from a list.

Returns the first non-nil value from a list.

Gets configuration for a transport from Config or Application env.

Converts a value to an integer with a default fallback.

Puts a value into a map only if the value is not nil.

Normalizes a bind IP configuration value to :loopback, :any, or parsed IP.

Normalizes a blank value (nil or empty/whitespace string) to nil.

Converts a map to a normalized map (handles keyword lists).

Parses an IP address string into a tuple.

Converts a value to a boolean with a default fallback.

Determines if a transport is enabled based on Config and Application env.

Checks if a value is truthy.

Functions

bool_value(value)

@spec bool_value(any()) :: boolean() | nil

broadcast_to_bus(topic, event)

@spec broadcast_to_bus(String.t(), any()) :: :ok

Safely broadcasts to LemonCore.Bus if available.

canonicalize_host(host)

@spec canonicalize_host(String.t()) :: String.t() | nil

Canonicalizes a hostname by trimming, removing trailing dot, and lowercasing.

emit_telemetry(event_name, metadata)

@spec emit_telemetry(atom(), map()) :: any()

Safely emits telemetry events if LemonCore.Telemetry is available.

fetch(map, key)

@spec fetch(map() | keyword(), atom() | String.t()) :: any()

Fetches a value from a map or keyword list, trying both atom and string keys.

fetch_any(map, paths)

@spec fetch_any(map() | keyword(), [[String.t() | atom()]]) :: any()

Fetches a nested value from a map by trying multiple key paths. Returns the first non-nil value found.

fetch_path(value, list)

@spec fetch_path(any(), [String.t() | atom()]) :: any()

Fetches a value from a nested map structure following a path of keys.

first_non_blank(values)

@spec first_non_blank(list()) :: any()

Returns the first non-blank value from a list.

first_non_nil(values)

@spec first_non_nil(list()) :: any()

Returns the first non-nil value from a list.

get_transport_config(config_key, app_env_key)

@spec get_transport_config(atom(), atom()) :: map()

Gets configuration for a transport from Config or Application env.

int_value(value, default)

@spec int_value(any(), integer()) :: integer()

Converts a value to an integer with a default fallback.

maybe_put(map, key, value)

@spec maybe_put(map(), any(), any()) :: map()

Puts a value into a map only if the value is not nil.

normalize_bind_ip(other)

@spec normalize_bind_ip(any()) :: :loopback | :any | :inet.ip_address()

Normalizes a bind IP configuration value to :loopback, :any, or parsed IP.

normalize_blank(value)

@spec normalize_blank(any()) :: any()

Normalizes a blank value (nil or empty/whitespace string) to nil.

normalize_map(map)

@spec normalize_map(map() | keyword()) :: map()

Converts a map to a normalized map (handles keyword lists).

parse_ip(value)

@spec parse_ip(String.t()) :: :inet.ip_address() | nil

Parses an IP address string into a tuple.

resolve_boolean(values, default)

@spec resolve_boolean([any()], boolean()) :: boolean()

Converts a value to a boolean with a default fallback.

transport_enabled?(config_key)

@spec transport_enabled?(atom()) :: boolean()

Determines if a transport is enabled based on Config and Application env.

truthy?(value)

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

Checks if a value is truthy.