mendraw/value_adapter
Adapts list-bound editable values to typed Gleam values.
Table-style widgets can read one editable value, classify it without
application-local instanceof probes, parse edited text back into the
same value type, compare old and new values, and write the result while
preserving failures:
import gleam/option
import gleam/result
import mendraw/datasource
import mendraw/value_adapter
let editable = datasource.attribute_value(attribute, item)
let snapshot = value_adapter.snapshot(editable)
case value_adapter.parse(snapshot.kind, edited_text) {
Ok(option.Some(next)) ->
case snapshot.value {
option.Some(current) ->
case value_adapter.values_equal(current, next) {
True -> Ok(Nil)
False -> value_adapter.write(editable, option.Some(next))
}
option.None -> value_adapter.write(editable, option.Some(next))
}
Ok(option.None) -> value_adapter.write(editable, option.None)
Error(error) -> Error(error)
}
Types
A decimal-like Mendix value such as a Big.js instance.
pub type DecimalValue
One immutable capture of an editable value.
pub type Snapshot {
Snapshot(
kind: ValueKind,
value: option.Option(TypedValue),
display_text: String,
read_only: Bool,
)
}
Constructors
-
Snapshot( kind: ValueKind, value: option.Option(TypedValue), display_text: String, read_only: Bool, )
One classified Mendix attribute value.
pub type TypedValue {
Boolean(value: Bool)
Date(value: date.JsDate)
Integer(value: Int)
Float(value: Float)
Decimal(value: DecimalValue)
Text(value: String)
}
Constructors
-
Boolean(value: Bool) -
Date(value: date.JsDate) -
Integer(value: Int) -
Float(value: Float) -
Decimal(value: DecimalValue) -
Text(value: String)
Describes why an editable value could not be adapted.
pub type ValueAdapterError {
EditableValueUnavailable(status: mendix.ValueStatus)
UnsupportedEditableValue
InvalidTextInput(kind: ValueKind, text: String, reason: String)
ReadOnlyEditableValue(kind: ValueKind)
EditableWriteFailed(reason: String)
DecimalNotFinite(text: String)
}
Constructors
-
EditableValueUnavailable(status: mendix.ValueStatus)The editable value is loading or otherwise unavailable.
-
UnsupportedEditableValueThe stored value has a shape Mendraw cannot classify.
-
InvalidTextInput(kind: ValueKind, text: String, reason: String)Edited text could not be converted to the expected value kind.
-
ReadOnlyEditableValue(kind: ValueKind)The editable value cannot be written because it is read-only.
-
EditableWriteFailed(reason: String)The Mendix runtime rejected the written value.
-
DecimalNotFinite(text: String)The decimal-like value cannot be represented as a finite float.
The value type stored by one Mendix attribute.
pub type ValueKind {
BooleanKind
DateKind
IntegerKind
FloatKind
DecimalKind
StringKind
}
Constructors
-
BooleanKindA Mendix Boolean attribute.
-
DateKindA Mendix DateTime attribute.
-
IntegerKindA JavaScript number with an integral value.
-
FloatKindA JavaScript number with a fractional value.
-
DecimalKindA decimal-like Mendix value such as a Big.js instance.
-
StringKindA Mendix String attribute.
Values
pub fn attribute_snapshot(
attr attr: list_attribute.ListAttributeValue,
item item: mendix.ObjectItem,
) -> Result(Snapshot, ValueAdapterError)
Captures one item-bound attribute value.
Empty values are classified from the declared Mendix attribute type; non-empty values are classified from their stored value shape.
pub fn decimal_to_float(
value value: DecimalValue,
) -> Result(Float, ValueAdapterError)
Converts one decimal-like value to a finite float.
pub fn decimal_to_string(value value: DecimalValue) -> String
Serializes one decimal-like value as text.
pub fn parse(
kind kind: ValueKind,
text text: String,
) -> Result(option.Option(TypedValue), ValueAdapterError)
Converts edited text into one typed value.
Surrounding whitespace is trimmed. Empty text becomes Ok(None) so an
empty attribute never collides with an invalid conversion.
pub fn snapshot(
ev ev: editable_value.EditableValue,
) -> Result(Snapshot, ValueAdapterError)
Captures one editable value using its stored value shape.
Empty values use StringKind; use attribute_snapshot when the declared
attribute type should classify empty values.
pub fn values_equal(
left left: TypedValue,
right right: TypedValue,
) -> Bool
Compares two typed values with kind-stable semantics.
Values of different kinds are never equal. Dates compare by timestamp and
decimals compare numerically, so 1.0 and 1.00 are equal.
pub fn write(
ev ev: editable_value.EditableValue,
value value: option.Option(TypedValue),
) -> Result(Nil, ValueAdapterError)
Writes one typed value through the editable-value handle.
Empty values clear the attribute. Read-only, unavailable, and runtime write failures are reported explicitly.