Lissome.GleamType (Lissome v0.3.1)
View SourceHelpers to work with Gleam types and their Erlang representations.
Summary
Functions
Flattens a nested GleamType record structure into a single map.
Creates a GleamType from a Gleam record.
Creates a new GleamType from a type name and value.
Checks if a value is a GleamType struct.
Converts a GleamType struct to its corresponding Erlang tuple.
Types
Functions
Flattens a nested GleamType record structure into a single map.
Recursively traverses the values and merges nested GleamTypes into a single flat map. This is intended to convert a GleamType struct into a map that can be serialized to JSON.
Examples
iex> flat_tuple_map(%{name: {0, "John"}, address: {1, %GleamType{name: :address, values: %{city: {0, "NY"}}}}})
%{name: "John", city: "NY"}
Creates a GleamType from a Gleam record.
Extracts record information from the corresponding .hrl file and builds a GleamType with the proper structure. Each field is stored with the index where that field should be in the Erlang tuple.
Options
:hrl_file_path
- Path to the .hrl file where the record is defined.Defaults to{gleam_dir}/build/dev/erlang/{gleam_app}/{module}_{capitalized_type}.hrl
, where:gleam_dir
is the value of:gleam_dir
config inlissome
, or"assets/lustre_app"
if not setgleam_app
is the:gleam_app
option or its default value if not provided.capitalized_type
is the name of the type but with its first character in uppercase.
:gleam_app
- Name of the Gleam application Defaults to the value of:gleam_app
config inlissome
, or"lustre_app"
if not set
Examples
iex> from_record(:person, :my_gleam_module, %{name: "John", age: 30})
%GleamType{name: :person, values: %{name: {0, "John"}, age: {1, 30}, __gleam_record__: true}}
Creates a new GleamType from a type name and value.
This function only wraps the value in a tuple with the type name.
For cases where the type has multiple fields, use from_record/4
instead.
Examples
iex> from_value(:some, "hello")
%GleamType{name: :some, values: "hello"}
iex> from_value(:error, "something went wrong")
%GleamType{name: :error, values: "something went wrong"}
Checks if a value is a GleamType struct.
Examples
iex> gleam_type?(%Lissome.GleamType{})
true
iex> gleam_type?("not a gleam type")
false
Converts a GleamType struct to its corresponding Erlang tuple.
Handles nested GleamTypes structs inside the values, converting them to their Erlang tuples as well.
If the GleamType was constructed using a record, then this functions guarantees that the tuple will have the values in the order Erlang expects them.
Examples
iex> to_erlang_tuple(%GleamType{name: :point, values: %{x: {0, 1}, y: {1, 2}}})
{:point, 1, 2}
iex> to_erlang_tuple(%GleamType{name: :person, values: %{name: {0, "John"}, skill: {1, %GleamType{name: :skill, values: %{name: {0, "coding"}, proficiency: {1, 10}}}}}})
{:person, "John", {:skill, "coding", 10}}