Lotus.Web.Formatters.VariableOptionsFormatter (Lotus Web v0.14.5)

View Source

Handles 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

from_display_format(display_string)

@spec from_display_format(String.t()) :: [map()]

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

from_lotus_result(arg1)

@spec from_lotus_result(map()) :: [map()]

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

normalize_to_maps(options)

@spec normalize_to_maps([any()]) :: [map()]

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

static_options_to_storage(static_options)

@spec static_options_to_storage([struct()] | [map()]) :: [map()]

Converts StaticOption structs to storage format maps.

to_display_format(options)

@spec to_display_format([map()]) :: String.t()

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"

to_form_params(options)

@spec to_form_params([map()]) :: [map()]

Converts storage format options to form params format for LiveView forms.

to_select_options(static_options)

@spec to_select_options([map()] | nil) :: [{String.t(), String.t()}]

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