Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/src/api/models/action/set_variable_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ class SetVariableAction extends ActionModel
/// Value that the variable needs to be updated with.
final String newValue;

/// Whether to toggle the value of the variable instead of setting it
/// if the variable is a boolean.
final bool toggled;

/// Creates a new [SetValueAction].
SetVariableAction({
required this.variable,
required this.newValue,
this.toggled = false,
}) : super(type: ActionType.setVariable);

@override
Expand All @@ -35,10 +40,12 @@ class SetVariableAction extends ActionModel
SetVariableAction copyWith({
VariableData? variable,
String? newValue,
bool? toggled,
}) =>
SetVariableAction(
variable: variable ?? this.variable,
newValue: newValue ?? this.newValue,
toggled: toggled ?? this.toggled,
);

/// Creates a new [SetVariableAction] instance from a JSON data.
Expand Down
36 changes: 36 additions & 0 deletions lib/src/api/models/variables_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ enum VariableType {
VariableType.list => 'List',
VariableType.map => 'Map',
};

factory VariableType.fromObjectType(Object obj) {
return switch (obj) {
int() => VariableType.integer,
double() => VariableType.decimal,
bool() => VariableType.boolean,
ColorRGBA() => VariableType.color,
ColorRGB() => VariableType.color,
List() => VariableType.list,
Map() => VariableType.map,
String() => VariableType.text,
_ => throw UnsupportedError(
'object type ${obj.runtimeType} is not supported. Cannot determine variable type'),
};
}

/// Whether the variable type is [VariableType.map].
bool get isMap => this == VariableType.map;

/// Whether the variable type is [VariableType.list].
bool get isList => this == VariableType.list;

/// Whether the variable type is [VariableType.color].
bool get isColor => this == VariableType.color;

/// Whether the variable type is [VariableType.boolean].
bool get isBoolean => this == VariableType.boolean;

/// Whether the variable type is [VariableType.decimal].
bool get isDecimal => this == VariableType.decimal;

/// Whether the variable type is [VariableType.integer].
bool get isInteger => this == VariableType.integer;

/// Whether the variable type is [VariableType.text].
bool get isText => this == VariableType.text;
}

/// Store information of a variable. [id] must not be empty when creating a
Expand Down
16 changes: 11 additions & 5 deletions lib/src/api/regexes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
/// ${model.list[5]}
///
/// // valid
/// ${model.list[5][2]}
///
/// // valid
/// ${items[5][2]}
///
/// // valid
/// ${model.json.name}
///
/// // invalid
Expand All @@ -84,8 +90,8 @@
/// [a-zA-Z0-9_]* # Matches zero or more alphanumeric characters or underscores
/// ) # End of the named capture group "name"
/// (
/// (?<accessor>\[\d+\]) # Named capture group "accessor" for array access:
/// # - \[\d+\] matches one or more digits surrounded by square brackets
/// (?<accessor> # Named capture group "accessor" for array access:
/// (?:\[\d+\])+ # Non-capturing group for multiple occurrences of digits surrounded by square brackets
/// |
/// (?: # Non-capturing group for nested path access:
/// \. # Matches a dot (for nested properties)
Expand All @@ -106,9 +112,9 @@
/// 3. accessor: The array accessor applied directly to the variable.
/// 4. path: The nested property path applied directly to the variable.
///
/// Try it out here: https://regex101.com/r/FOyWLJ/1
/// Try it out here: https://regex101.com/r/FOyWLJ/2
const String variablePathPattern =
r'(?<!\\)\$\{(?<value>(?<name>[a-zA-Z][a-zA-Z0-9_]*)((?<accessor>\[\d+\])|(?:\.(?<path>[a-zA-Z]+[a-zA-Z0-9_]*(?:\[\d+\])*))*)?)\}';
r'(?<!\\)\$\{(?<value>(?<name>[a-zA-Z][a-zA-Z0-9_]*)((?<accessor>(?:\[\d+\])+)|(?:\.(?<path>[a-zA-Z]+[a-zA-Z0-9_]*(?:\[\d+\])*))*)?)\}';

/// Regex for [variablePathPattern].
final RegExp variablePathRegex = RegExp(variablePathPattern);
Expand All @@ -117,7 +123,7 @@ final RegExp variablePathRegex = RegExp(variablePathPattern);
/// on $ or ${} curly braces in a string while the text/path is actively being
/// composed.
const String variablePathComposingPattern =
r'(?<!\\)\$\{?(?<value>(?<name>[a-zA-Z][a-zA-Z0-9_]*)((?<accessor>\[\d+\])|(?:\.(?<path>[a-zA-Z]+[a-zA-Z0-9_]*(?:\[\d+\])*))*)?)?\}?';
r'(?<!\\)\$\{?(?<value>(?<name>[a-zA-Z][a-zA-Z0-9_]*)((?<accessor>(?:\[\d+\])+)|(?:\.(?<path>[a-zA-Z]+[a-zA-Z0-9_]*(?:\[\d+\])*))*)?)?\}?';

/// Regex for [variablePathComposingPattern].
final RegExp variablePathComposingRegex = RegExp(variablePathComposingPattern);
Expand Down