Skip to content

Commit a2063fe

Browse files
committed
Tests updated to the latest spec.
This drove a few changes within the code base.
1 parent 4487959 commit a2063fe

19 files changed

+249
-101
lines changed

lib/src/main/java/dev/openfeature/javasdk/BaseEvaluation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public interface BaseEvaluation<T> {
2323
* The error code, if applicable. Should only be set when the Reason is ERROR.
2424
* @return {ErrorCode}
2525
*/
26-
ErrorCode getErrorCode();
26+
String getErrorCode();
2727
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package dev.openfeature.javasdk;
22

3-
public interface Client extends FlagEvaluationLifecycle, Features{
3+
public interface Client extends FlagEvaluationLifecycle, Features {
4+
Metadata getMetadata();
45
}

lib/src/main/java/dev/openfeature/javasdk/FeatureProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.openfeature.javasdk;
22

33
public interface FeatureProvider {
4-
String getName();
4+
Metadata getMetadata();
55
ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx, FlagEvaluationOptions options);
66
ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx, FlagEvaluationOptions options);
77
ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx, FlagEvaluationOptions options);

lib/src/main/java/dev/openfeature/javasdk/FlagEvaluationDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class FlagEvaluationDetails<T> implements BaseEvaluation<T> {
1111
T value;
1212
@Nullable String variant;
1313
Reason reason;
14-
@Nullable ErrorCode errorCode;
14+
@Nullable String errorCode;
1515

1616
public static <T> FlagEvaluationDetails<T> from(ProviderEvaluation<T> providerEval, String flagKey) {
1717
return FlagEvaluationDetails.<T>builder()

lib/src/main/java/dev/openfeature/javasdk/HookContext.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ public class HookContext<T> {
1111
@NonNull FlagValueType type;
1212
@NonNull T defaultValue;
1313
@NonNull EvaluationContext ctx;
14-
Client client;
15-
FeatureProvider provider;
14+
Metadata clientMetadata;
15+
Metadata providerMetadata;
1616

17-
public static <T> HookContext<T> from(String key, FlagValueType type, Client client, EvaluationContext ctx, T defaultValue) {
17+
public static <T> HookContext<T> from(String key, FlagValueType type, Metadata clientMetadata, Metadata providerMetadata, EvaluationContext ctx, T defaultValue) {
1818
return HookContext.<T>builder()
1919
.flagKey(key)
2020
.type(type)
21-
.client(client)
21+
.clientMetadata(clientMetadata)
22+
.providerMetadata(providerMetadata)
2223
.ctx(ctx)
2324
.defaultValue(defaultValue)
2425
.build();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dev.openfeature.javasdk;
2+
3+
public interface Metadata {
4+
public String getName();
5+
}

lib/src/main/java/dev/openfeature/javasdk/NoOpProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ public class NoOpProvider implements FeatureProvider {
77
@Getter
88
private final String name = "No-op Provider";
99

10+
@Override
11+
public Metadata getMetadata() {
12+
return new Metadata() {
13+
@Override
14+
public String getName() {
15+
return name;
16+
}
17+
};
18+
}
19+
1020
@Override
1121
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
1222
return ProviderEvaluation.<Boolean>builder()

lib/src/main/java/dev/openfeature/javasdk/OpenFeatureAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public Client getClient() {
3030
return getClient(null, null);
3131
}
3232

33+
public Metadata getProviderMetadata() {
34+
return provider.getMetadata();
35+
}
36+
3337
public Client getClient(@Nullable String name) {
3438
return getClient(name, null);
3539
}

lib/src/main/java/dev/openfeature/javasdk/OpenFeatureClient.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key, T defa
4343
// merge of: API.context, client.context, invocation.context
4444

4545
// TODO: Context transformation?
46-
HookContext hookCtx = HookContext.from(key, type, this, ctx, defaultValue);
46+
HookContext hookCtx = HookContext.from(key, type, this.getMetadata(), OpenFeatureAPI.getInstance().getProvider().getMetadata(), ctx, defaultValue);
4747

4848
List<Hook> mergedHooks;
4949
if (options != null && options.getHooks() != null) {
@@ -84,11 +84,7 @@ <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key, T defa
8484
}
8585
details.value = defaultValue;
8686
details.reason = Reason.ERROR;
87-
if (e instanceof OpenFeatureError) { //NOPMD - suppressed AvoidInstanceofChecksInCatchClause - Don't want to duplicate detail creation logic.
88-
details.errorCode = ((OpenFeatureError) e).getErrorCode();
89-
} else {
90-
details.errorCode = ErrorCode.GENERAL;
91-
}
87+
details.errorCode = e.getMessage();
9288
this.errorHooks(hookCtx, e, mergedHooks, hints);
9389
} finally {
9490
this.afterAllHooks(hookCtx, mergedHooks, hints);
@@ -99,13 +95,21 @@ <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key, T defa
9995

10096
private void errorHooks(HookContext hookCtx, Exception e, List<Hook> hooks, ImmutableMap<String, Object> hints) {
10197
for (Hook hook : hooks) {
102-
hook.error(hookCtx, e, hints);
98+
try {
99+
hook.error(hookCtx, e, hints);
100+
} catch (Exception inner_exception) {
101+
log.error("Exception when running error hooks " + hook.getClass().toString(), inner_exception);
102+
}
103103
}
104104
}
105105

106106
private void afterAllHooks(HookContext hookCtx, List<Hook> hooks, ImmutableMap<String, Object> hints) {
107107
for (Hook hook : hooks) {
108-
hook.finallyAfter(hookCtx, hints);
108+
try {
109+
hook.finallyAfter(hookCtx, hints);
110+
} catch (Exception inner_exception) {
111+
log.error("Exception when running finally hooks " + hook.getClass().toString(), inner_exception);
112+
}
109113
}
110114
}
111115

@@ -247,4 +251,14 @@ public <T> FlagEvaluationDetails<T> getObjectDetails(String key, T defaultValue,
247251
public <T> FlagEvaluationDetails<T> getObjectDetails(String key, T defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
248252
return this.evaluateFlag(FlagValueType.OBJECT, key, defaultValue, ctx, options);
249253
}
254+
255+
@Override
256+
public Metadata getMetadata() {
257+
return new Metadata() {
258+
@Override
259+
public String getName() {
260+
return name;
261+
}
262+
};
263+
}
250264
}

lib/src/main/java/dev/openfeature/javasdk/ProviderEvaluation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public class ProviderEvaluation<T> implements BaseEvaluation<T> {
1010
T value;
1111
@Nullable String variant;
1212
Reason reason;
13-
@Nullable ErrorCode errorCode;
13+
@Nullable String errorCode;
1414
}

0 commit comments

Comments
 (0)