Skip to content

Commit b6cd0e2

Browse files
replace init function with setters
Signed-off-by: Alexandra Oberaigner <[email protected]>
1 parent 8a9738f commit b6cd0e2

File tree

3 files changed

+27
-42
lines changed

3 files changed

+27
-42
lines changed

src/main/java/dev/openfeature/sdk/HookSupportData.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Map;
66
import lombok.Getter;
7+
import lombok.Setter;
78

89
/**
910
* Encapsulates data for hook execution per flag evaluation.
@@ -13,16 +14,22 @@ class HookSupportData {
1314

1415
private List<Pair<Hook, HookContext>> hooks;
1516
private EvaluationContext evaluationContext;
17+
18+
@Setter
1619
private Map<String, Object> hints;
17-
private boolean isInitialized = false;
1820

1921
HookSupportData() {}
2022

21-
void initialize(
22-
List<Hook> hooks,
23-
SharedHookContext sharedContext,
24-
EvaluationContext evaluationContext,
25-
Map<String, Object> hints) {
23+
public void setEvaluationContext(EvaluationContext evaluationContext) {
24+
this.evaluationContext = evaluationContext;
25+
if (hooks != null) {
26+
for (Pair<Hook, HookContext> hookContextPair : hooks) {
27+
hookContextPair.getValue().setCtx(evaluationContext);
28+
}
29+
}
30+
}
31+
32+
public void setHooks(List<Hook> hooks, SharedHookContext sharedContext, EvaluationContext evaluationContext) {
2633
List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>();
2734
for (Hook hook : hooks) {
2835
if (hook.supportsFlagValueType(sharedContext.getType())) {
@@ -32,14 +39,5 @@ void initialize(
3239
}
3340
this.hooks = hookContextPairs;
3441
this.evaluationContext = evaluationContext;
35-
this.hints = hints;
36-
isInitialized = true;
37-
}
38-
39-
public void setEvaluationContext(EvaluationContext evaluationContext) {
40-
this.evaluationContext = evaluationContext;
41-
for (Pair<Hook, HookContext> hookContextPair : hooks) {
42-
hookContextPair.getValue().setCtx(evaluationContext);
43-
}
4442
}
4543
}

src/main/java/dev/openfeature/sdk/OpenFeatureClient.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ public EvaluationContext getEvaluationContext() {
160160
+ "Instead, we return an evaluation result with the appropriate error code.")
161161
private <T> FlagEvaluationDetails<T> evaluateFlag(
162162
FlagValueType type, String key, T defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
163+
FlagEvaluationDetails<T> details = null;
164+
HookSupportData hookSupportData = new HookSupportData();
165+
163166
var flagOptions = ObjectUtils.defaultIfNull(
164167
options, () -> FlagEvaluationOptions.builder().build());
165168
var hints = Collections.unmodifiableMap(flagOptions.getHookHints());
166-
167-
FlagEvaluationDetails<T> details = null;
168-
HookSupportData hookSupportData = new HookSupportData();
169+
hookSupportData.setHints(hints);
169170

170171
try {
171172
final var stateManager = openfeatureApi.getFeatureProviderStateManager(this.domain);
@@ -179,7 +180,7 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(
179180
var sharedHookContext =
180181
new SharedHookContext(key, type, this.getMetadata(), provider.getMetadata(), defaultValue);
181182

182-
hookSupportData.initialize(mergedHooks, sharedHookContext, ctx, hints);
183+
hookSupportData.setHooks(mergedHooks, sharedHookContext, ctx);
183184

184185
var evalContext = mergeEvaluationContext(ctx);
185186
hookSupportData.setEvaluationContext(evalContext);
@@ -217,11 +218,11 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(
217218
}
218219
details.setErrorMessage(e.getMessage());
219220
enrichDetailsWithErrorDefaults(defaultValue, details);
220-
if (hookSupportData.isInitialized()) {
221+
if (hookSupportData.getHooks() != null && hookSupportData.getHints() != null) {
221222
hookSupport.executeErrorHooks(hookSupportData, e);
222223
}
223224
} finally {
224-
if (hookSupportData.isInitialized()) {
225+
if (hookSupportData.getHooks() != null && hookSupportData.getHints() != null) {
225226
hookSupport.executeAfterAllHooks(hookSupportData, details);
226227
}
227228
}

src/test/java/dev/openfeature/sdk/HookSupportTest.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import dev.openfeature.sdk.fixtures.HookFixtures;
1010
import java.util.Arrays;
11-
import java.util.Collections;
1211
import java.util.HashMap;
1312
import java.util.List;
1413
import java.util.Map;
@@ -35,11 +34,8 @@ void shouldMergeEvaluationContextsOnBeforeHooksCorrectly() {
3534
when(hook2.before(any(), any())).thenReturn(Optional.of(evaluationContextWithValue("foo", "bar")));
3635

3736
var hookSupportData = new HookSupportData();
38-
hookSupportData.initialize(
39-
Arrays.asList(hook1, hook2),
40-
getBaseHookContextForType(FlagValueType.STRING),
41-
baseContext,
42-
Collections.emptyMap());
37+
hookSupportData.setHooks(
38+
Arrays.asList(hook1, hook2), getBaseHookContextForType(FlagValueType.STRING), baseContext);
4339

4440
hookSupport.executeBeforeHooks(hookSupportData);
4541

@@ -57,11 +53,8 @@ void shouldAlwaysCallGenericHook(FlagValueType flagValueType) {
5753
Hook<?> genericHook = mockGenericHook();
5854

5955
var hookSupportData = new HookSupportData();
60-
hookSupportData.initialize(
61-
List.of(genericHook),
62-
getBaseHookContextForType(flagValueType),
63-
ImmutableContext.EMPTY,
64-
Collections.emptyMap());
56+
hookSupportData.setHooks(
57+
List.of(genericHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY);
6558

6659
callAllHooks(hookSupportData);
6760

@@ -77,11 +70,7 @@ void shouldAlwaysCallGenericHook(FlagValueType flagValueType) {
7770
void shouldPassDataAcrossStages(FlagValueType flagValueType) {
7871
var testHook = new TestHookWithData();
7972
var hookSupportData = new HookSupportData();
80-
hookSupportData.initialize(
81-
List.of(testHook),
82-
getBaseHookContextForType(flagValueType),
83-
ImmutableContext.EMPTY,
84-
Collections.emptyMap());
73+
hookSupportData.setHooks(List.of(testHook), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY);
8574

8675
hookSupport.executeBeforeHooks(hookSupportData);
8776
assertHookData(testHook, "before");
@@ -106,11 +95,8 @@ void shouldIsolateDataBetweenHooks(FlagValueType flagValueType) {
10695
var testHook2 = new TestHookWithData(2);
10796

10897
var hookSupportData = new HookSupportData();
109-
hookSupportData.initialize(
110-
List.of(testHook1, testHook2),
111-
getBaseHookContextForType(flagValueType),
112-
ImmutableContext.EMPTY,
113-
Collections.emptyMap());
98+
hookSupportData.setHooks(
99+
List.of(testHook1, testHook2), getBaseHookContextForType(flagValueType), ImmutableContext.EMPTY);
114100

115101
callAllHooks(hookSupportData);
116102

0 commit comments

Comments
 (0)