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
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
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
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
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
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
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
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"
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"
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
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
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")
[]