View Source Bonfire.Common.Types (Bonfire v0.9.10-classic-beta.169)

Helpers for handling the type of objects (structs and more)

Summary

Functions

Outputs the names of all object types for the purpose of adding to the localisation strings (as long as the output is piped through to Bonfire.Common.Localise.Gettext.localise_strings/1 at compile time)

Takes an object or module name and checks if it defines a struct.

Takes a value and returns true if it's a number or can be converted to a float.

Takes a string and returns true if it is a valid UUID or ULID.

Takes a map or list of maps, and if the value of a key in the map is a ULID, it replaces it with the corresponding Crockford Base32 encoded string.

Takes a string and returns an atom if it can be converted to one, else returns the input itself.

Takes a string or an atom and returns an atom if it is one or can be converted to one, else returns nil.

Takes a string or atom and attempts to convert it to an atom or module, depending on the flags.

Converts a value to a floating-point number if possible. If the value cannot be converted to a float, it returns a fallback value (which defaults to 0 if not provided).

Converts a value to an integer if possible. If the value is not an integer, it attempts to convert it to a float and then rounds it to the nearest integer. Otherwise it returns a fallback value (which defaults to 0 if not provided).

Takes a string and returns the corresponding Elixir module if it exists and is not disabled in the app.

Takes a string as input, converts it to snake_case, and converts it to an atom if such an atom exists, otherwise returns nil.

Handles multiple cases where the input value is of a different type (atom, list, tuple, etc.) and returns a string representation of it.

Takes a module name (as a string or an atom) and converts it to a human-readable string.

Takes a module atom and converts it to a string, or a string and removes the Elixir. prefix if it exists.

Takes an object, module name, or string, and returns the type of the object.

Outputs a human-readable representation of an object type.

Used for mapping schema types to user-friendly names. Given a string representing a schema type name, returns a sanitised version of it, or nil for object types (or mixins) that shouldn't be displayed.

Given a schema module, returns its table ID (i.e. Pointable ULID).

Given an object or module name, returns its respective table table ID (i.e. Pointable ULID).

Given a list of schema types, returns a list of their respective table types. Filters out any empty values.

Takes an object and returns its data type as a module name or atom.

Takes an object and returns a single ULID (Universally Unique Lexicographically Sortable Identifier) ID(s) if present in the object.

Takes an object and returns the ULID (Universally Unique Lexicographically Sortable Identifier) ID if present in the object. Throws an error if a ULID ID is not present.

Takes an object or list of objects and returns a list of ULIDs (Universally Unique Lexicographically Sortable Identifier) ID(s) if present.

Functions

Outputs the names of all object types for the purpose of adding to the localisation strings (as long as the output is piped through to Bonfire.Common.Localise.Gettext.localise_strings/1 at compile time)

> all_object_type_names()
["User", "Delete this User", "Post", "Delete this Post", ...]

Takes an object or module name and checks if it defines a struct.

Examples

iex> defines_struct?(Needle.Pointer)
true

iex> defines_struct?(%{__struct__: Bonfire.Common})
true

iex> defines_struct?(%{some_key: "some_value"})
false

Takes a value and returns true if it's a number or can be converted to a float.

Examples

iex> is_numeric(123)
true

iex> is_numeric("123.45")
true

iex> is_numeric("abc")
false
Link to this function

is_uid?(str, params \\ nil)

View Source

Takes a string and returns true if it is a valid UUID or ULID.

Examples

iex> is_uid?("01J3MQ2Q4RVB1WTE3KT1D8ZNX1")
true

iex> is_uid?("550e8400-e29b-41d4-a716-446655440000")
true

iex> is_uid?("invalid_id")
false
Link to this function

maybe_convert_ulids(list)

View Source

Takes a map or list of maps, and if the value of a key in the map is a ULID, it replaces it with the corresponding Crockford Base32 encoded string.

Examples

iex> maybe_convert_ulids(%{key: "01FJ4ZZZ8P5RMZMM00XDDDF8"})
%{key: "01FJ4ZZZ8P5RMZMM00XDDDF8"}

iex> maybe_convert_ulids([%{key: "01FJ4ZZZ8P5RMZMM00XDDDF8"}])
[%{key: "01FJ4ZZZ8P5RMZMM00XDDDF8"}]

Takes a string and returns an atom if it can be converted to one, else returns the input itself.

Examples

iex> maybe_to_atom("atom_name")
:atom_name

iex> maybe_to_atom("def_non_existing_atom")
"def_non_existing_atom"

Takes a string or an atom and returns an atom if it is one or can be converted to one, else returns nil.

Examples

iex> maybe_to_atom!("atom_name")
:atom_name

iex> maybe_to_atom!("def_non_existing_atom")
nil
Link to this function

maybe_to_atom_or_module(k, force, to_snake)

View Source

Takes a string or atom and attempts to convert it to an atom or module, depending on the flags.

Examples

iex> maybe_to_atom_or_module(:some_atom, true, true)
:some_atom

iex> maybe_to_atom_or_module("Enum", true, true)
Enum
Link to this function

maybe_to_float(val, fallback \\ 0)

View Source

Converts a value to a floating-point number if possible. If the value cannot be converted to a float, it returns a fallback value (which defaults to 0 if not provided).

Examples

iex> maybe_to_float(123)
123

iex> maybe_to_float("123.45")
123.45

iex> maybe_to_float("abc", 0.0)
0.0
Link to this function

maybe_to_integer(val, fallback \\ 0)

View Source

Converts a value to an integer if possible. If the value is not an integer, it attempts to convert it to a float and then rounds it to the nearest integer. Otherwise it returns a fallback value (which defaults to 0 if not provided).

Examples

iex> maybe_to_integer(123.45)
123

iex> maybe_to_integer("123")
123

iex> maybe_to_integer("abc", 0)
0
Link to this function

maybe_to_module(str, force \\ true)

View Source

Takes a string and returns the corresponding Elixir module if it exists and is not disabled in the app.

Examples

iex> maybe_to_module("Enum")
Enum

iex> maybe_to_module("NonExistentModule")
nil
Link to this function

maybe_to_snake_atom(string)

View Source

Takes a string as input, converts it to snake_case, and converts it to an atom if such an atom exists, otherwise returns nil.

Examples

iex> maybe_to_snake_atom("SomeString")
:some_string

iex> maybe_to_snake_atom("DefNonExistingAtom")
nil

Handles multiple cases where the input value is of a different type (atom, list, tuple, etc.) and returns a string representation of it.

Examples

iex> maybe_to_string(:some_atom)
"some_atom"

iex> maybe_to_string([1, 2, 3])
"[1, 2, 3]"

iex> maybe_to_string({:a, :tuple})
"a: tuple"
Link to this function

module_to_human_readable(module)

View Source

Takes a module name (as a string or an atom) and converts it to a human-readable string.

It removes the Elixir. prefix (if it exists) and any other prefixes (e.g., Bonfire.Common.) and converts the final part of the module name to a string in title case (e.g., Types).

Examples

iex> module_to_human_readable("Elixir.Bonfire.Common.Types")
"Types"

iex> module_to_human_readable(Bonfire.Common.Types)
"Types"

Takes a module atom and converts it to a string, or a string and removes the Elixir. prefix if it exists.

Examples

iex> module_to_str(SomeModule)
"SomeModule"

iex> module_to_str(Elixir.SomeModule)
"SomeModule"
Link to this function

object_type(object, opts \\ [])

View Source

Takes an object, module name, or string, and returns the type of the object.

The function uses various patterns to match different object types (such as associations, Pointables, edges/verbs, etc.). If none of the patterns match, the function returns nil.

Examples

iex> object_type(%Ecto.Association.NotLoaded{})
nil

> object_type(%{table_id: "601NTERTAB1EF0RA11TAB1ES00"})
Needle.Table

iex> object_type(%{pointer_id: "User"})
Bonfire.Data.Identity.User

iex> object_type("User")
Bonfire.Data.Identity.User

iex> object_type(:some_atom)
:some_atom
Link to this function

object_type_display(object_type)

View Source

Outputs a human-readable representation of an object type.

Examples

iex> object_type_display(:user)
"user"

> object_type_display(%Bonfire.Data.Social.APActivity{})
"apactivity"

Used for mapping schema types to user-friendly names. Given a string representing a schema type name, returns a sanitised version of it, or nil for object types (or mixins) that shouldn't be displayed.

Examples

iex> sanitise_name("Apactivity")
"Federated Object"

iex> sanitise_name("Settings")
"Setting"

iex> sanitise_name("Created")
nil

Given a schema module, returns its table ID (i.e. Pointable ULID).

Examples

> table_id(Bonfire.Data.Social.APActivity)
"30NF1REAPACTTAB1ENVMBER0NE"

Given an object or module name, returns its respective table table ID (i.e. Pointable ULID).

Examples

> table_type(%Bonfire.Data.Social.APActivity{})
"30NF1REAPACTTAB1ENVMBER0NE"

iex> table_type(%Needle.Pointer{table_id: "30NF1REAPACTTAB1ENVMBER0NE"})
"30NF1REAPACTTAB1ENVMBER0NE"

> table_type(Bonfire.Data.Social.APActivity)
"30NF1REAPACTTAB1ENVMBER0NE"

Given a list of schema types, returns a list of their respective table types. Filters out any empty values.

> table_types([%Needle.Pointer{table_id: "30NF1REAPACTTAB1ENVMBER0NE"}, %Bonfire.Data.Social.APActivity{}])
["30NF1REAPACTTAB1ENVMBER0NE"]

Given a single schema type, it returns its respective table type.

> table_types(Bonfire.Data.Social.APActivity)
["30NF1REAPACTTAB1ENVMBER0NE"]

Takes an object and returns its data type as a module name or atom.

Examples

iex> typeof(%{__struct__: Ecto.Schema})
Ecto.Schema

iex> typeof(%{__context__: nil, __changed__: nil})
:assigns

iex> typeof(nil)
:empty

iex> typeof(%Ecto.Changeset{})
Ecto.Changeset

iex> typeof([1, 2])
List

iex> typeof([])
:empty

iex> typeof("string")
String

iex> typeof(:atom)
Atom

iex> typeof(123)
Integer

iex> typeof(%{id: 1})
Map

iex> typeof(%{})
:empty

iex> typeof(3.14)
Float

iex> typeof({:ok, 42})
Tuple

iex> typeof(fn -> :ok end)
Function

iex> typeof(self())
Process

iex> typeof(Port.open({:spawn, "cat"}, [:binary]))
Port

iex> typeof(make_ref())
:reference

iex> typeof(%{__struct__: Bonfire.Classify.Category})
Bonfire.Classify.Category
Link to this function

uid(input, fallback \\ nil)

View Source

Takes an object and returns a single ULID (Universally Unique Lexicographically Sortable Identifier) ID(s) if present in the object.

Examples

iex> uid(%{pointer_id: "01J3MNBPD0VX96MFY9B15BCHYP"})
"01J3MNBPD0VX96MFY9B15BCHYP"

iex> uid(%{pointer: %{id: "01J3MNBPD0VX96MFY9B15BCHYP"}})
"01J3MNBPD0VX96MFY9B15BCHYP"

iex> uid(%{id: "01J3MNBPD0VX96MFY9B15BCHYP"})
"01J3MNBPD0VX96MFY9B15BCHYP"

iex> uid("01J3MNBPD0VX96MFY9B15BCHYP")
"01J3MNBPD0VX96MFY9B15BCHYP"

> uid(["01J3MNBPD0VX96MFY9B15BCHYP", "01J3MQ2Q4RVB1WTE3KT1D8ZNX1"])
# ** (ArgumentError) Expected an ID (ULID or UUID) or an object or list containing a single one, but got several

iex> uid("invalid_id")
nil

iex> uid("invalid_id", :fallback)
:fallback

Takes an object and returns the ULID (Universally Unique Lexicographically Sortable Identifier) ID if present in the object. Throws an error if a ULID ID is not present.

Examples

iex> uid!(%{pointer_id: "01J3MNBPD0VX96MFY9B15BCHYP"})
"01J3MNBPD0VX96MFY9B15BCHYP"

iex> uid!("invalid_id")
** (RuntimeError) Expected an object or ID (ULID)

Takes an object or list of objects and returns a list of ULIDs (Universally Unique Lexicographically Sortable Identifier) ID(s) if present.

Examples

iex> uids(%{pointer_id: "01J3MNBPD0VX96MFY9B15BCHYP"})
["01J3MNBPD0VX96MFY9B15BCHYP"]

iex> uids(%{pointer: %{id: "01J3MNBPD0VX96MFY9B15BCHYP"}})
["01J3MNBPD0VX96MFY9B15BCHYP"]

iex> uids([%{id: "01J3MNBPD0VX96MFY9B15BCHYP"}])
["01J3MNBPD0VX96MFY9B15BCHYP"]

iex> uids("01J3MNBPD0VX96MFY9B15BCHYP")
["01J3MNBPD0VX96MFY9B15BCHYP"]

iex> uids(["01J3MNBPD0VX96MFY9B15BCHYP", "01J3MQ2Q4RVB1WTE3KT1D8ZNX1"])
["01J3MNBPD0VX96MFY9B15BCHYP", "01J3MQ2Q4RVB1WTE3KT1D8ZNX1"]

iex> uids("invalid_id")
[]
Link to this function

uids_or(objects, fallback_or_fun)

View Source