@@ -33,39 +33,25 @@ public class AssignerNode implements NodeAction {
33
33
34
34
public enum WriteMode {
35
35
36
- OVER_WRITE , APPEND , CLEAR
36
+ OVER_WRITE , APPEND , CLEAR , INPUT_CONSTANT
37
37
38
38
}
39
39
40
40
/**
41
41
* description of a single assignment operation
42
42
*/
43
- public static class AssignItem {
44
-
45
- private final String targetKey ;
46
-
47
- private final String inputKey ;
48
-
49
- private final WriteMode writeMode ;
50
-
43
+ public record AssignItem (String targetKey , String inputKey , WriteMode writeMode , Object inputValue ) {
51
44
public AssignItem (String targetKey , String inputKey , WriteMode writeMode ) {
52
- this .targetKey = targetKey ;
53
- this .inputKey = inputKey ;
54
- this .writeMode = writeMode ;
55
- }
56
-
57
- public String getTargetKey () {
58
- return targetKey ;
45
+ this (targetKey , inputKey , writeMode , null );
59
46
}
60
47
61
- public String getInputKey ( ) {
62
- return inputKey ;
48
+ public AssignItem ( String targetKey , Object inputValue ) {
49
+ this ( targetKey , null , WriteMode . INPUT_CONSTANT , inputValue ) ;
63
50
}
64
51
65
- public WriteMode getWriteMode ( ) {
66
- return writeMode ;
52
+ public AssignItem ( String targetKey ) {
53
+ this ( targetKey , null , WriteMode . OVER_WRITE ) ;
67
54
}
68
-
69
55
}
70
56
71
57
private final List <AssignItem > items ;
@@ -88,15 +74,12 @@ public AssignerNode(String targetKey, String inputKey, WriteMode writeMode) {
88
74
public Map <String , Object > apply (OverAllState state ) {
89
75
Map <String , Object > updates = new HashMap <>();
90
76
for (AssignItem item : items ) {
91
- Object value = state .value (item .inputKey ).orElse (null );
92
- Object targetValue = state .value (item .targetKey ).orElse (null );
93
- Object result = null ;
94
-
95
- switch (item .writeMode ) {
96
- case OVER_WRITE :
97
- result = value ;
98
- break ;
99
- case APPEND :
77
+ Object value = state .value (item .inputKey ()).orElse (null );
78
+ Object targetValue = state .value (item .targetKey ()).orElse (null );
79
+
80
+ Object result = switch (item .writeMode ()) {
81
+ case OVER_WRITE -> value ;
82
+ case APPEND -> {
100
83
if (targetValue instanceof List && value != null ) {
101
84
List <Object > newList = new ArrayList <>((List <?>) targetValue );
102
85
if (value instanceof Collection <?> col ) {
@@ -105,35 +88,41 @@ public Map<String, Object> apply(OverAllState state) {
105
88
else {
106
89
newList .add (value );
107
90
}
108
- result = newList ;
91
+ yield newList ;
109
92
}
110
93
else if (value != null ) {
111
94
if (value instanceof Collection <?> col ) {
112
- result = new ArrayList <>(col );
95
+ yield new ArrayList <>(col );
113
96
}
114
97
else {
115
- result = new ArrayList <>(List .of (value ));
98
+ yield new ArrayList <>(List .of (value ));
116
99
}
117
100
}
118
- break ;
119
- case CLEAR :
101
+ else {
102
+ throw new IllegalArgumentException (
103
+ "Cannot append to non-list value for key: " + item .targetKey ());
104
+ }
105
+ }
106
+ case CLEAR -> {
120
107
if (targetValue instanceof List ) {
121
- result = new ArrayList <>();
108
+ yield new ArrayList <>();
122
109
}
123
110
else if (targetValue instanceof Map ) {
124
- result = new HashMap <>();
111
+ yield new HashMap <>();
125
112
}
126
113
else if (targetValue instanceof String ) {
127
- result = "" ;
114
+ yield "" ;
128
115
}
129
116
else if (targetValue instanceof Number ) {
130
- result = 0 ;
117
+ yield 0 ;
131
118
}
132
119
else {
133
- result = null ;
120
+ yield null ;
134
121
}
135
- break ;
136
- }
122
+ }
123
+ case INPUT_CONSTANT -> item .inputValue ();
124
+ default -> throw new IllegalArgumentException ("Invalid write mode: " + item .writeMode ());
125
+ };
137
126
updates .put (item .targetKey , result );
138
127
}
139
128
return updates ;
@@ -146,13 +135,28 @@ public static Builder builder() {
146
135
147
136
public static class Builder {
148
137
149
- private final List <AssignItem > items = new ArrayList <>();
138
+ private List <AssignItem > items = new ArrayList <>();
139
+
140
+ public Builder setItems (List <AssignItem > items ) {
141
+ this .items = new ArrayList <>(items );
142
+ return this ;
143
+ }
150
144
151
145
public Builder addItem (String targetKey , String inputKey , WriteMode writeMode ) {
152
146
items .add (new AssignItem (targetKey , inputKey , writeMode ));
153
147
return this ;
154
148
}
155
149
150
+ public Builder addConst (String targetKey , Object inputValue ) {
151
+ items .add (new AssignItem (targetKey , inputValue ));
152
+ return this ;
153
+ }
154
+
155
+ public Builder addClear (String targetKey ) {
156
+ items .add (new AssignItem (targetKey ));
157
+ return this ;
158
+ }
159
+
156
160
public Builder addItem (AssignItem item ) {
157
161
items .add (item );
158
162
return this ;
0 commit comments