Lissome.GleamType (Lissome v0.3.1)

View Source

Helpers to work with Gleam types and their Erlang representations.

Summary

Types

t()

The GleamType struct type.

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

t()

@type t() :: %Lissome.GleamType{name: atom(), record?: term(), values: any()}

The GleamType struct type.

Functions

flat_tuple_map(values)

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"}

from_record(type, module, values, opts \\ [])

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 in lissome, or "assets/lustre_app" if not set
    • gleam_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 in lissome, 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}}

from_value(type, value)

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"}

gleam_type?(value)

Checks if a value is a GleamType struct.

Examples

iex> gleam_type?(%Lissome.GleamType{})
true

iex> gleam_type?("not a gleam type")
false

to_erlang_tuple(gleam_type)

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}}