spectra_type (spectra v0.9.3)

View Source

Utility functions for inspecting and manipulating sp_type() values.

Provides uniform meta-data access across all concrete type records (#sp_simple_type{}, #sp_union{}, #sp_rec{}, etc.), optional-value detection, and normalisation of -spectra() doc annotations.

Summary

Functions

Attaches a normalised doc map (from a -spectra() attribute) to Type.

Checks whether Type can hold an absent value (nil or undefined).

Extracts the meta map from any sp_type() record.

Validates and normalises a raw -spectra() annotation map into a type_doc(). Raises {invalid_spectra_field, Key, Value} on unknown or ill-typed fields.

Like normalize_doc/1 but for function-level -spectra() annotations. Accepts summary, description, and deprecated.

Returns the parameters entry from the type's meta map, or undefined if absent. Used for string constraints such as min_length, max_length, and pattern.

If From carries parameters in its meta, copies them into To's meta. Used when following a remote type alias to preserve string constraints (e.g. bounded :: String.t() with min_length).

Returns a copy of Type with its meta map replaced by Meta.

Extracts the type-variable bindings from an sp_type() node. Returns the list of concrete type arguments for #sp_user_type_ref{} and #sp_remote_type{}, or [] for all other types.

Merges Updates into the existing meta map of Type, returning the updated type. Existing keys not present in Updates are preserved; keys in Updates overwrite existing ones.

Functions

add_doc_to_type(Type, DocMap)

-spec add_doc_to_type(spectra:sp_type(), map()) -> spectra:sp_type().

Attaches a normalised doc map (from a -spectra() attribute) to Type.

can_be_missing(TypeInfo, Type)

-spec can_be_missing(TypeInfo :: spectra:type_info(), Type :: spectra:sp_type()) ->
                        {true, spectra:missing_value()} | false.

Checks whether Type can hold an absent value (nil or undefined).

Returns {true, MissingValue} when the type is, or expands to, a literal nil or undefined (possibly inside a union). The returned MissingValue is the atom to use when the field is omitted. Returns false otherwise. Used during encode/decode to skip optional fields whose value matches the missing sentinel.

get_meta/1

-spec get_meta(spectra:sp_type()) -> spectra:sp_type_meta().

Extracts the meta map from any sp_type() record.

normalize_doc(DocMap)

-spec normalize_doc(map()) -> spectra:type_doc().

Validates and normalises a raw -spectra() annotation map into a type_doc(). Raises {invalid_spectra_field, Key, Value} on unknown or ill-typed fields.

normalize_function_doc(DocMap)

-spec normalize_function_doc(map()) -> spectra:function_doc().

Like normalize_doc/1 but for function-level -spectra() annotations. Accepts summary, description, and deprecated.

parameters(Type)

-spec parameters(spectra:sp_type()) -> term().

Returns the parameters entry from the type's meta map, or undefined if absent. Used for string constraints such as min_length, max_length, and pattern.

propagate_params(From, To)

-spec propagate_params(spectra:sp_type(), spectra:sp_type()) -> spectra:sp_type().

If From carries parameters in its meta, copies them into To's meta. Used when following a remote type alias to preserve string constraints (e.g. bounded :: String.t() with min_length).

set_meta/2

Returns a copy of Type with its meta map replaced by Meta.

type_args/1

-spec type_args(spectra:sp_type()) -> [spectra:sp_type()].

Extracts the type-variable bindings from an sp_type() node. Returns the list of concrete type arguments for #sp_user_type_ref{} and #sp_remote_type{}, or [] for all other types.

update_meta(Type, Updates)

-spec update_meta(spectra:sp_type(), spectra:sp_type_meta()) -> spectra:sp_type().

Merges Updates into the existing meta map of Type, returning the updated type. Existing keys not present in Updates are preserved; keys in Updates overwrite existing ones.