6
6
import java .util .List ;
7
7
import java .util .Map ;
8
8
import java .util .Optional ;
9
- import java .util .UUID ;
10
9
11
10
import com .microsoft .azure .functions .ExecutionContext ;
12
11
import com .microsoft .azure .functions .rpc .messages .ParameterBinding ;
13
12
import com .microsoft .azure .functions .rpc .messages .TypedData ;
14
- import com .microsoft .azure .functions .worker .broker . CoreTypeResolver ;
13
+ import com .microsoft .azure .functions .worker .converter . CoreTypeConverter ;
15
14
16
15
import org .apache .commons .lang3 .exception .ExceptionUtils ;
17
16
21
20
* Thread-safety: Single thread.
22
21
*/
23
22
public final class BindingDataStore {
23
+
24
+ private final Map <String , DataTarget > targets ;
25
+ private final Map <String , DataSource <?>> inputSources ;
26
+ private final Map <Type , DataSource <?>> otherSources ;
27
+ private final Map <String , DataSource <?>> metadataSources ;
28
+ private Map <String , BindingDefinition > definitions ;
29
+ public static final String RETURN_NAME = "$return" ;
30
+
24
31
public BindingDataStore () {
25
32
this .targets = new HashMap <>();
26
33
this .inputSources = new HashMap <>();
27
34
this .otherSources = new HashMap <>();
28
35
this .metadataSources = new HashMap <>();
29
- this .promotedTargets = null ;
30
36
}
31
37
32
- ///////////////////////// region Input Binding Data
33
-
38
+ //Logics for input Binding Data
34
39
public void addParameterSources (List <ParameterBinding > parameters ) {
35
40
assert parameters != null ;
36
41
for (ParameterBinding parameter : parameters ) {
@@ -92,24 +97,21 @@ private static DataSource<?> rpcSourceFromParameter(ParameterBinding parameter)
92
97
return rpcSourceFromTypedData (parameter .getName (), parameter .getData ());
93
98
}
94
99
95
- ///////////////////////// end region Input Binding Data
96
-
97
- ///////////////////////// region Output Binding Data
98
100
99
- public List <ParameterBinding > getOutputParameterBindings (boolean excludeReturn ) throws Exception {
101
+ //Logics for output Binding Data
102
+ public List <ParameterBinding > getOutputParameterBindings () throws Exception {
100
103
List <ParameterBinding > bindings = new ArrayList <>();
101
- for (Map .Entry <String , DataTarget > entry : this .getTarget (this .promotedTargets ).entrySet ()) {
102
- if (!excludeReturn || !entry .getKey ().equals (RETURN_NAME )) {
103
- entry .getValue ().computeFromValue ().ifPresent (data ->
104
- bindings .add (ParameterBinding .newBuilder ().setName (entry .getKey ()).setData (data ).build ())
105
- );
106
- }
104
+ for (String key : this .targets .keySet ()) {
105
+ if (key .equals (RETURN_NAME )) continue ;
106
+ DataTarget dataTarget = this .targets .get (key );
107
+ dataTarget .computeFromValue ().ifPresent (data ->
108
+ bindings .add (ParameterBinding .newBuilder ().setName (key ).setData (data ).build ()));
107
109
}
108
110
return bindings ;
109
111
}
110
112
111
113
public Optional <TypedData > getDataTargetTypedValue (String name ) throws Exception {
112
- return Optional .ofNullable (this .getTarget ( this . promotedTargets ) .get (name )).map (o -> {
114
+ return Optional .ofNullable (this .targets .get (name )).map (o -> {
113
115
try {
114
116
return o .computeFromValue ().orElse (null );
115
117
} catch (Exception ex ) {
@@ -119,48 +121,37 @@ public Optional<TypedData> getDataTargetTypedValue(String name) throws Exception
119
121
});
120
122
}
121
123
122
- public Optional <BindingData > getOrAddDataTarget (UUID outputId , String name , Type target , boolean hasImplicitOutput ) {
124
+ public Optional <BindingData > getOrAddDataTarget (String name , Type target , boolean hasImplicitOutput ) {
123
125
DataTarget output = null ;
124
126
if (this .isDataTargetValid (name , target )) {
125
- output = this .getTarget ( outputId ) .get (name );
127
+ output = this .targets .get (name );
126
128
if (output == null && (this .isDefinitionOutput (name ) || hasImplicitOutput )) {
127
- this .getTarget ( outputId ) .put (name , output = rpcDataTargetFromType (target ));
129
+ this .targets .put (name , output = rpcDataTargetFromType (target ));
128
130
}
129
131
}
130
- return Optional .ofNullable (output ).map (out -> new BindingData ( out ) );
132
+ return Optional .ofNullable (output ).map (BindingData :: new );
131
133
}
132
134
133
135
public void setDataTargetValue (String name , Object value ) {
134
- Optional .ofNullable (this .getTarget (this .promotedTargets ).get (name )).ifPresent (out -> out .setValue (value ));
135
- }
136
-
137
- public void promoteDataTargets (UUID outputId ) {
138
- this .promotedTargets = outputId ;
139
- }
140
-
141
- private Map <String , DataTarget > getTarget (UUID outputId ) {
142
- return this .targets .computeIfAbsent (outputId , m -> new HashMap <>());
136
+ Optional .ofNullable (this .targets .get (name )).ifPresent (out -> out .setValue (value ));
143
137
}
144
138
145
139
private boolean isDataTargetValid (String name , Type target ) {
146
140
if (!name .equals (RETURN_NAME )) {
147
- if (!CoreTypeResolver .isValidOutputType (target )) { return false ; }
148
- target = CoreTypeResolver .getParameterizedActualTypeArgumentsType (target );
141
+ if (!CoreTypeConverter .isValidOutputType (target )) { return false ; }
142
+ target = CoreTypeConverter .getParameterizedActualTypeArgumentsType (target );
149
143
}
150
144
return true ;
151
145
}
152
146
153
147
private static DataTarget rpcDataTargetFromType (Type target ) {
154
- if (CoreTypeResolver .isHttpResponse (target )) {
148
+ if (CoreTypeConverter .isHttpResponse (target )) {
155
149
return new RpcHttpDataTarget ();
156
150
}
157
151
return new RpcUnspecifiedDataTarget ();
158
152
}
159
153
160
- ///////////////////////// end region Output Binding Data
161
-
162
- ///////////////////////// region Binding Definitions
163
-
154
+ //Logics for binding Definitions
164
155
public void setBindingDefinitions (Map <String , BindingDefinition > definitions ) {
165
156
this .definitions = definitions ;
166
157
}
@@ -172,14 +163,4 @@ private boolean isDefinitionOutput(String name) {
172
163
private Optional <BindingDefinition > getDefinition (String name ) {
173
164
return Optional .ofNullable (this .definitions .get (name ));
174
165
}
175
-
176
- ///////////////////////// endregion Binding Definitions
177
-
178
- private UUID promotedTargets ;
179
- private final Map <UUID , Map <String , DataTarget >> targets ;
180
- private final Map <String , DataSource <?>> inputSources ;
181
- private final Map <Type , DataSource <?>> otherSources ;
182
- private final Map <String , DataSource <?>> metadataSources ;
183
- private Map <String , BindingDefinition > definitions ;
184
- public static final String RETURN_NAME = "$return" ;
185
166
}
0 commit comments