This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 74fb372c461e21224950e37f949f9297d2b0e998
Author: Alex Heneveld <[email protected]>
AuthorDate: Thu May 30 11:41:12 2024 +0100

    allow workflow scratch to store an explicit null, if somehow it is set
    
    freemarker will still refuse to return it, but the internal model is now 
more consistent
    (that is what we do eg for sensors, config, etc; rather than using 'null' 
as an indication to remove something)
---
 .../core/workflow/WorkflowExecutionContext.java         | 17 +++++++++++++----
 .../steps/variables/ClearVariableWorkflowStep.java      |  2 +-
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExecutionContext.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExecutionContext.java
index f026023198..522a6d1c71 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExecutionContext.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExecutionContext.java
@@ -441,10 +441,18 @@ public class WorkflowExecutionContext {
         return MutableMap.copyOf(workflowScratchVariables).asUnmodifiable();
     }
 
+    public Object clearWorkflowScratchVariable(String s) {
+        if (workflowScratchVariables == null) getWorkflowScratchVariables();
+        Object old = workflowScratchVariables.remove(s);
+        if (workflowScratchVariablesUpdatedThisStep==null) 
workflowScratchVariablesUpdatedThisStep = MutableMap.of();
+        workflowScratchVariablesUpdatedThisStep.put(s, Entities.REMOVE);
+        return old;
+    }
+
     public Object updateWorkflowScratchVariable(String s, Object v) {
-        if (workflowScratchVariables ==null) getWorkflowScratchVariables();
+        if (workflowScratchVariables == null) getWorkflowScratchVariables();
         Object old = workflowScratchVariables.put(s, v);
-        if (v==null) workflowScratchVariables.remove(s);
+        if (Entities.REMOVE.equals(v)) workflowScratchVariables.remove(s);
         if (workflowScratchVariablesUpdatedThisStep==null) 
workflowScratchVariablesUpdatedThisStep = MutableMap.of();
         workflowScratchVariablesUpdatedThisStep.put(s, v);
         return old;
@@ -452,8 +460,9 @@ public class WorkflowExecutionContext {
 
     public void updateWorkflowScratchVariables(Map<String,Object> newValues) {
         if (newValues!=null && !newValues.isEmpty()) {
-            if (workflowScratchVariables ==null) getWorkflowScratchVariables();
+            if (workflowScratchVariables == null) 
getWorkflowScratchVariables();
             workflowScratchVariables.putAll(newValues);
+            newValues.forEach((k,v)->{ if (Entities.REMOVE.equals(v)) 
workflowScratchVariables.remove(k); });
             if (workflowScratchVariablesUpdatedThisStep==null) 
workflowScratchVariablesUpdatedThisStep = MutableMap.of();
             workflowScratchVariablesUpdatedThisStep.putAll(newValues);
         }
@@ -787,7 +796,7 @@ public class WorkflowExecutionContext {
             includeUpdates = true;
             if (last.workflowScratch !=null) {
                 result = MutableMap.copyOf(last.workflowScratch).add(result);
-                result.entrySet().stream().filter(e -> 
e.getValue()==null).map(Map.Entry::getKey).forEach(result::remove);
+                result.entrySet().stream().filter(e -> 
Entities.REMOVE.equals(e.getValue())).map(Map.Entry::getKey).forEach(result::remove);
                 break;
             }
             if (last.previous==null || last.previous.isEmpty()) break;
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/ClearVariableWorkflowStep.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/ClearVariableWorkflowStep.java
index 3623c472de..ab6cbb958f 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/ClearVariableWorkflowStep.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/ClearVariableWorkflowStep.java
@@ -43,7 +43,7 @@ public class ClearVariableWorkflowStep extends 
WorkflowStepDefinition {
         if (variable ==null) throw new IllegalArgumentException("Variable name 
is required");
         String name = 
context.resolve(WorkflowExpressionResolution.WorkflowExpressionStage.STEP_INPUT,
 variable.name, String.class);
         if (Strings.isBlank(name)) throw new 
IllegalArgumentException("Variable name is required");
-        
context.getWorkflowExectionContext().updateWorkflowScratchVariable(name, null);
+        
context.getWorkflowExectionContext().clearWorkflowScratchVariable(name);
         return context.getPreviousStepOutput();
     }
 

Reply via email to