Lotus.Web.Formatters.VariableOptionsFormatter (Lotus Web v0.14.5)
View SourceHandles conversion and formatting of variable static options between different formats:
- Display format: Smart text representation for editing UI
- Storage format: Maps with value/label keys for Lotus library
- Legacy format: Simple string arrays for backward compatibility
Display Format Rules
When all options have the same value and label:
"Bob
Alice
Charlie"When options have different values and labels:
"bob | Bob
alice | Alice
charlie | Charlie"Storage Format
All options are stored as maps with string keys:
[
%{"value" => "bob", "label" => "Bob"},
%{"value" => "alice", "label" => "Alice"}
]
Summary
Functions
Converts display format string to storage format.
Converts Lotus query result to option maps format.
Normalizes various option formats to the standard storage format.
Converts StaticOption structs to storage format maps.
Converts storage format options to display format for editing.
Converts storage format options to form params format for LiveView forms.
Converts static options to Phoenix select format: [{label, value}, ...].
Functions
Converts display format string to storage format.
Examples
iex> VariableOptionsFormatter.from_display_format("Bob\nAlice")
[%{"value" => "Bob", "label" => "Bob"}, %{"value" => "Alice", "label" => "Alice"}]
iex> VariableOptionsFormatter.from_display_format("bob | Bob\nalice | Alice")
[%{"value" => "bob", "label" => "Bob"}, %{"value" => "alice", "label" => "Alice"}]
Converts Lotus query result to option maps format.
Handles results with:
- 1 column: uses value for both value and label
- 2 columns: first is value, second is label
- 3+ columns: uses first two columns
Values are formatted using Lotus.Value.to_display_string/1 to handle UUIDs and other binary data types properly.
Examples
iex> result = %{columns: ["name"], rows: [["Alice"], ["Bob"]]}
iex> VariableOptionsFormatter.from_lotus_result(result)
[%{value: "Alice", label: "Alice"}, %{value: "Bob", label: "Bob"}]
iex> result = %{columns: ["id", "name"], rows: [[1, "Alice"], [2, "Bob"]]}
iex> VariableOptionsFormatter.from_lotus_result(result)
[%{value: 1, label: "Alice"}, %{value: 2, label: "Bob"}]
Normalizes various option formats to the standard storage format.
Handles:
- Legacy string arrays:
["Bob", "Alice"] - Storage format:
[%{"value" => "bob", "label" => "Bob"}] - StaticOption structs:
[%{value: "bob", label: "Bob"}] - Mixed formats during migration
Converts StaticOption structs to storage format maps.
Converts storage format options to display format for editing.
Examples
iex> options = [%{"value" => "bob", "label" => "Bob"}, %{"value" => "alice", "label" => "Alice"}]
iex> VariableOptionsFormatter.to_display_format(options)
"bob | Bob\nalice | Alice"
iex> options = [%{"value" => "Bob", "label" => "Bob"}, %{"value" => "Alice", "label" => "Alice"}]
iex> VariableOptionsFormatter.to_display_format(options)
"Bob\nAlice"
Converts storage format options to form params format for LiveView forms.
Converts static options to Phoenix select format: [{label, value}, ...].
Examples
iex> options = [%{"value" => "bob", "label" => "Bob"}, %{"value" => "alice", "label" => "Alice"}]
iex> VariableOptionsFormatter.to_select_options(options)
[{"Bob", "bob"}, {"Alice", "alice"}]
iex> VariableOptionsFormatter.to_select_options(nil)
[]