Skip to content

Commit b1f3b78

Browse files
committed
Implement map operations
(cherry picked from commit 5177113)
1 parent 87fee4e commit b1f3b78

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/src/functions/functions_repository.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,43 @@ class FunctionsRepository {
529529
newValue = jsonEncode(currentValue);
530530
}
531531

532+
if (action.variable.type.isMap &&
533+
action.mapOperation != MapOperation.replace) {
534+
// Get current value of the map variable.
535+
final Map? currentValue =
536+
variableNotifier.value.getValue().typedValue<Map>();
537+
// If map variable does not exist, return false.
538+
if (currentValue == null) return false;
539+
// Retrieve all variables.
540+
final Iterable<VariableData> variables =
541+
codelesslyContext.variables.values.map((e) => e.value);
542+
// Find the value of variable referenced by key.
543+
final keyVariableValue = PropertyValueDelegate.retrieveVariableValue(
544+
action.key,
545+
variables,
546+
codelesslyContext.data,
547+
IndexedItemProvider.of(context),
548+
);
549+
// If key is a variable, use its value. Else, use the key as it is.
550+
final String key =
551+
keyVariableValue is String ? keyVariableValue : action.key;
552+
// Perform map operations.
553+
switch (action.mapOperation) {
554+
case MapOperation.add:
555+
currentValue.addAll(newValue.toMap() ?? {});
556+
break;
557+
case MapOperation.remove:
558+
currentValue.remove(key);
559+
break;
560+
case MapOperation.update:
561+
currentValue[key] = newValue;
562+
break;
563+
default:
564+
break;
565+
}
566+
newValue = jsonEncode(currentValue);
567+
}
568+
532569
final VariableData updatedVariable =
533570
variableNotifier.value.copyWith(value: newValue);
534571

0 commit comments

Comments
 (0)