Skip to content

Commit f907ec4

Browse files
committed
Add SPI method to process endpoint properties
This will allow extracting things like auth schemes and other AWS specific properties.
1 parent aad4ead commit f907ec4

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/RulesCompiler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
final class RulesCompiler {
3636

37+
private final List<RulesExtension> extensions;
3738
private final EndpointRuleSet rules;
3839

3940
private final Map<Object, Integer> constantPool = new LinkedHashMap<>();
@@ -66,11 +67,13 @@ final class RulesCompiler {
6667
private int temporaryRegisters = 0;
6768

6869
RulesCompiler(
70+
List<RulesExtension> extensions,
6971
EndpointRuleSet rules,
7072
Map<String, RulesFunction> functions,
7173
BiFunction<String, Context, Object> builtinProvider,
7274
boolean performOptimizations
7375
) {
76+
this.extensions = extensions;
7477
this.rules = rules;
7578
this.builtinProvider = builtinProvider;
7679
this.performOptimizations = performOptimizations;
@@ -433,6 +436,7 @@ RulesProgram buildProgram() {
433436
var constPool = new Object[this.constantPool.size()];
434437
constantPool.keySet().toArray(constPool);
435438
return new RulesProgram(
439+
extensions,
436440
this.instructions,
437441
0,
438442
instructionSize,

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/RulesEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public final class RulesEngine {
3030
}
3131
}
3232

33+
private final List<RulesExtension> extensions = new ArrayList<>();
3334
private final Map<String, RulesFunction> functions = new LinkedHashMap<>();
3435
private final List<BiFunction<String, Context, Object>> builtinProviders = new ArrayList<>();
3536
private boolean performOptimizations = true;
@@ -82,6 +83,7 @@ public RulesEngine addBuiltinProvider(BiFunction<String, Context, Object> builti
8283
* @return the RulesEngine.
8384
*/
8485
public RulesEngine addExtension(RulesExtension extension) {
86+
extensions.add(extension);
8587
addBuiltinProvider(extension.getBuiltinProvider());
8688
for (var f : extension.getFunctions()) {
8789
addFunction(f);
@@ -120,7 +122,7 @@ private BiFunction<String, Context, Object> createBuiltinProvider() {
120122
* @return the compiled program.
121123
*/
122124
public RulesProgram compile(EndpointRuleSet rules) {
123-
return new RulesCompiler(rules, functions, createBuiltinProvider(), performOptimizations).compile();
125+
return new RulesCompiler(extensions, rules, functions, createBuiltinProvider(), performOptimizations).compile();
124126
}
125127

126128
/**
@@ -191,6 +193,7 @@ public RulesProgram build() {
191193
}
192194

193195
return new RulesProgram(
196+
extensions,
194197
bytecode.array(),
195198
bytecode.arrayOffset() + bytecode.position(),
196199
bytecode.remaining(),

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/RulesExtension.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package software.amazon.smithy.java.client.rulesengine;
77

88
import java.util.List;
9+
import java.util.Map;
910
import java.util.function.BiFunction;
11+
import software.amazon.smithy.java.client.core.endpoint.Endpoint;
1012
import software.amazon.smithy.java.context.Context;
1113

1214
/**
@@ -30,4 +32,21 @@ default BiFunction<String, Context, Object> getBuiltinProvider() {
3032
default List<RulesFunction> getFunctions() {
3133
return List.of();
3234
}
35+
36+
/**
37+
* Allows processing a resolved endpoint, extracting properties, and updating the endpoint builder.
38+
*
39+
* @param builder The endpoint being created. Modify this based on properties and headers.
40+
* @param context The context provided when resolving the endpoint. The endpoint has its own context properties.
41+
* @param properties The raw properties returned from the endpoint resolver. Process these to update the builder.
42+
* @param headers The headers returned from the endpoint resolver. Process these if needed.
43+
*/
44+
default void extractEndpointProperties(
45+
Endpoint.Builder builder,
46+
Context context,
47+
Map<String, Object> properties,
48+
Map<String, List<String>> headers
49+
) {
50+
// by default does nothing.
51+
}
3352
}

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/RulesProgram.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public final class RulesProgram {
147147
*/
148148
static final byte RETURN_VALUE = 17;
149149

150+
final List<RulesExtension> extensions;
150151
final Object[] constantPool;
151152
final byte[] instructions;
152153
final int instructionOffset;
@@ -157,6 +158,7 @@ public final class RulesProgram {
157158
private final int paramCount; // number of provided params.
158159

159160
RulesProgram(
161+
List<RulesExtension> extensions,
160162
byte[] instructions,
161163
int instructionOffset,
162164
int instructionSize,
@@ -165,6 +167,7 @@ public final class RulesProgram {
165167
BiFunction<String, Context, Object> builtinProvider,
166168
final Object[] constantPool
167169
) {
170+
this.extensions = extensions;
168171
this.instructions = instructions;
169172
this.instructionOffset = instructionOffset;
170173
this.instructionSize = instructionSize;

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/RulesVm.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected boolean removeEldestEntry(Map.Entry<String, URI> eldest) {
3232
};
3333
});
3434

35+
private final Context context;
3536
private final RulesProgram program;
3637
private final Object[] registers;
3738
private final BiFunction<String, Context, Object> builtinProvider;
@@ -46,6 +47,7 @@ protected boolean removeEldestEntry(Map.Entry<String, URI> eldest) {
4647
Map<String, Object> parameters,
4748
BiFunction<String, Context, Object> builtinProvider
4849
) {
50+
this.context = context;
4951
this.program = program;
5052
this.instructions = program.instructions;
5153
this.builtinProvider = builtinProvider;
@@ -258,11 +260,10 @@ private Endpoint setEndpoint(boolean hasProperties, boolean hasHeaders) {
258260
builder.putProperty(Endpoint.HEADERS, headers);
259261
}
260262

261-
for (var _e : properties.entrySet()) {
262-
// TODO: map properties to endpoint properties.
263+
for (var extension : program.extensions) {
264+
extension.extractEndpointProperties(builder, context, properties, headers);
263265
}
264266

265-
// TODO: Add auth schemes and figure out how to map properties there too.
266267
return builder.build();
267268
}
268269

0 commit comments

Comments
 (0)