spectra_type_info (spectra v0.9.3)

View Source

Stores and queries the type, record, and function metadata extracted from a module's BEAM debug info.

A type_info() value is the primary context object passed through serialisation and schema-generation traversals. It maps {TypeName, Arity} keys to their sp_type() representations and also tracks whether the owning module implements the spectra_codec behaviour.

Summary

Functions

Registers a function's spec list in TypeInfo. Called during module analysis.

Registers a record type in TypeInfo. Called during module analysis.

Registers a named type in TypeInfo. Called during module analysis.

Resolves the codec module for {Mod, TypeRef}.

Looks up a function's spec list by name and arity. Returns error when not found.

Looks up a record by name. Returns error when not found.

Looks up a type by name and arity. Returns error when not found.

Returns the module atom that this type_info() was built for.

Like find_record/2 but raises {record_not_found, Name} when absent.

Like find_type/3 but raises {type_not_found, Name, Arity} when absent.

Creates a fresh type_info() for Module. ImplementsCodec is true when the module exports encode/5, decode/5 and is therefore its own codec.

Types

function_key()

-type function_key() :: {Name :: atom(), Arity :: arity()}.

type_info()

-type type_info() ::
          #type_info{module :: module(),
                     types :: #{spectra_type_info:type_key() => spectra:sp_type()},
                     records ::
                         #{atom() =>
                               #sp_rec{name :: atom(),
                                       fields ::
                                           [#sp_rec_field{name :: atom(),
                                                          binary_name :: binary(),
                                                          type :: spectra:sp_type()}],
                                       arity :: pos_integer(),
                                       meta :: spectra:sp_type_meta()}},
                     functions ::
                         #{spectra_type_info:function_key() =>
                               [#sp_function_spec{args :: [spectra:sp_type()],
                                                  return :: spectra:sp_type(),
                                                  meta :: spectra:sp_function_spec_meta()}]},
                     implements_codec :: boolean()}.

type_key()

-type type_key() :: {Name :: atom(), Arity :: arity()}.

Functions

add_function/4

-spec add_function(type_info(), atom(), arity(), [spectra:sp_function_spec()]) -> type_info().

Registers a function's spec list in TypeInfo. Called during module analysis.

add_record/3

-spec add_record(type_info(),
                 atom(),
                 #sp_rec{name :: atom(),
                         fields ::
                             [#sp_rec_field{name :: atom(),
                                            binary_name :: binary(),
                                            type :: spectra:sp_type()}],
                         arity :: pos_integer(),
                         meta :: spectra:sp_type_meta()}) ->
                    type_info().

Registers a record type in TypeInfo. Called during module analysis.

add_type/4

-spec add_type(type_info(), atom(), arity(), spectra:sp_type()) -> type_info().

Registers a named type in TypeInfo. Called during module analysis.

find_codec(Mod, TypeRef)

-spec find_codec(module(), spectra:sp_type_reference()) -> {ok, module()} | error.

Resolves the codec module for {Mod, TypeRef}.

Checks the application env ({spectra, [{codecs, #{...}}]}) first, then falls back to the module's own spectra_codec behaviour if it implements one. Calls code:ensure_loaded/1 on any codec found in app env so it is ready before its callbacks are dispatched.

find_function/3

-spec find_function(type_info(), atom(), arity()) -> {ok, [spectra:sp_function_spec()]} | error.

Looks up a function's spec list by name and arity. Returns error when not found.

find_record/2

-spec find_record(type_info(), atom()) ->
                     {ok,
                      #sp_rec{name :: atom(),
                              fields ::
                                  [#sp_rec_field{name :: atom(),
                                                 binary_name :: binary(),
                                                 type :: spectra:sp_type()}],
                              arity :: pos_integer(),
                              meta :: spectra:sp_type_meta()}} |
                     error.

Looks up a record by name. Returns error when not found.

find_type/3

-spec find_type(type_info(), atom(), arity()) -> {ok, spectra:sp_type()} | error.

Looks up a type by name and arity. Returns error when not found.

get_module/1

-spec get_module(type_info()) -> module().

Returns the module atom that this type_info() was built for.

get_record/2

-spec get_record(type_info(), atom()) ->
                    #sp_rec{name :: atom(),
                            fields ::
                                [#sp_rec_field{name :: atom(),
                                               binary_name :: binary(),
                                               type :: spectra:sp_type()}],
                            arity :: pos_integer(),
                            meta :: spectra:sp_type_meta()}.

Like find_record/2 but raises {record_not_found, Name} when absent.

get_type/3

-spec get_type(type_info(), atom(), arity()) -> spectra:sp_type().

Like find_type/3 but raises {type_not_found, Name, Arity} when absent.

new(Module, ImplementsCodec)

-spec new(module(), ImplementsCodec :: boolean()) -> type_info().

Creates a fresh type_info() for Module. ImplementsCodec is true when the module exports encode/5, decode/5 and is therefore its own codec.