Skip to content

Commit 999222f

Browse files
committed
fix: update hooks to work with SDKVariable_PB and Variable<T> and introduce cloud hooks to work with Variable<T>
1 parent 29688c7 commit 999222f

File tree

7 files changed

+249
-89
lines changed

7 files changed

+249
-89
lines changed

src/main/java/com/devcycle/sdk/server/cloud/api/DevCycleCloudClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
150150
}
151151

152152
variable.setIsDefaulted(false);
153-
evalHooksRunner.executeAfter(reversedHooks, context, variable);
153+
evalHooksRunner.executeAfterCloud(reversedHooks, context, variable);
154154
} catch (Throwable exception) {
155155
if (!(exception instanceof BeforeHookError || exception instanceof AfterHookError)) {
156156
variable = (Variable<T>) Variable.builder()
@@ -170,7 +170,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
170170

171171
evalHooksRunner.executeError(reversedHooks, context, exception);
172172
} finally {
173-
evalHooksRunner.executeFinally(reversedHooks, context, Optional.ofNullable(variable));
173+
evalHooksRunner.executeFinallyCloud(reversedHooks, context, Optional.ofNullable(variable));
174174
}
175175
return variable;
176176
}

src/main/java/com/devcycle/sdk/server/common/model/EvalHook.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import java.util.Optional;
44

5+
import com.devcycle.sdk.server.local.protobuf.SDKVariable_PB;
6+
57
public interface EvalHook<T> {
68

79
default Optional<HookContext<T>> before(HookContext<T> ctx) {
810
return Optional.empty();
911
}
10-
default void after(HookContext<T> ctx, Variable<T> variable) {}
1112
default void error(HookContext<T> ctx, Throwable e) {}
12-
default void onFinally(HookContext<T> ctx, Optional<Variable<T>> variable) {}
13+
14+
default void after(HookContext<T> ctx, SDKVariable_PB variable) {}
15+
default void onFinally(HookContext<T> ctx, Optional<SDKVariable_PB> variable) {}
16+
17+
default void afterCloud(HookContext<T> ctx, Variable<T> variable) {}
18+
default void onFinallyCloud(HookContext<T> ctx, Optional<Variable<T>> variable) {}
1319
}

src/main/java/com/devcycle/sdk/server/common/model/EvalHooksRunner.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.devcycle.sdk.server.common.exception.AfterHookError;
88
import com.devcycle.sdk.server.common.exception.BeforeHookError;
99
import com.devcycle.sdk.server.common.logging.DevCycleLogger;
10+
import com.devcycle.sdk.server.local.protobuf.SDKVariable_PB;
1011

1112
/**
1213
* A class that manages evaluation hooks for the DevCycle SDK.
@@ -81,7 +82,7 @@ public <T> HookContext<T> executeBefore(ArrayList<EvalHook<T>> hooks, HookContex
8182
* @param variable The variable result to pass to the hooks
8283
* @param <T> The type of the variable value
8384
*/
84-
public void executeAfter(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Variable<T> variable) {
85+
public void executeAfter(ArrayList<EvalHook<T>> hooks, HookContext<T> context, SDKVariable_PB variable) {
8586
for (EvalHook<T> hook : hooks) {
8687
try {
8788
hook.after(context, variable);
@@ -91,6 +92,23 @@ public void executeAfter(ArrayList<EvalHook<T>> hooks, HookContext<T> context, V
9192
}
9293
}
9394

95+
/**
96+
* Runs all after hooks in reverse order.
97+
*
98+
* @param context The context to pass to the hooks
99+
* @param variable The variable result to pass to the hooks
100+
* @param <T> The type of the variable value
101+
*/
102+
public void executeAfterCloud(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Variable<T> variable) {
103+
for (EvalHook<T> hook : hooks) {
104+
try {
105+
hook.afterCloud(context, variable);
106+
} catch (Exception e) {
107+
throw new AfterHookError("After hook failed", e);
108+
}
109+
}
110+
}
111+
94112
/**
95113
* Runs all error hooks in reverse order.
96114
*
@@ -115,7 +133,7 @@ public void executeError(ArrayList<EvalHook<T>> hooks, HookContext<T> context, T
115133
* @param context The context to pass to the hooks
116134
* @param variable The variable result to pass to the hooks (may be null)
117135
*/
118-
public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable) {
136+
public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<SDKVariable_PB> variable) {
119137
for (EvalHook<T> hook : hooks) {
120138
try {
121139
hook.onFinally(context, variable);
@@ -125,4 +143,21 @@ public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context,
125143
}
126144
}
127145
}
146+
147+
/**
148+
* Runs all finally hooks in reverse order.
149+
*
150+
* @param context The context to pass to the hooks
151+
* @param variable The variable result to pass to the hooks (may be null)
152+
*/
153+
public void executeFinallyCloud(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable) {
154+
for (EvalHook<T> hook : hooks) {
155+
try {
156+
hook.onFinallyCloud(context, variable);
157+
} catch (Exception e) {
158+
// Log finally hook error but don't throw
159+
DevCycleLogger.error("Finally hook failed: " + e.getMessage(), e);
160+
}
161+
}
162+
}
128163
}

src/main/java/com/devcycle/sdk/server/local/api/DevCycleLocalClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
181181

182182
HookContext<T> hookContext = new HookContext<T>(user, key, defaultValue, getMetadata());
183183
Variable<T> variable = null;
184+
SDKVariable_PB sdkVariable = null;
184185
ArrayList<EvalHook<T>> hooks = new ArrayList<EvalHook<T>>(evalHooksRunner.getHooks());
185186
ArrayList<EvalHook<T>> reversedHooks = new ArrayList<EvalHook<T>>(evalHooksRunner.getHooks());
186187
Collections.reverse(reversedHooks);
@@ -200,7 +201,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
200201
variable = defaultVariable;
201202
variable.setEval(EvalReason.defaultReason(EvalReason.DefaultReasonDetailsEnum.USER_NOT_TARGETED));
202203
} else {
203-
SDKVariable_PB sdkVariable = SDKVariable_PB.parseFrom(variableData);
204+
sdkVariable = SDKVariable_PB.parseFrom(variableData);
204205
if (sdkVariable.getType() != pbVariableType) {
205206
DevCycleLogger.warning("Variable type mismatch, returning default value");
206207
variable = defaultVariable;
@@ -212,7 +213,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
212213
if (beforeError != null) {
213214
throw beforeError;
214215
}
215-
evalHooksRunner.executeAfter(reversedHooks, hookContext, variable);
216+
evalHooksRunner.executeAfter(reversedHooks, hookContext, sdkVariable);
216217
} catch (Throwable e) {
217218
if (!(e instanceof BeforeHookError)) {
218219
DevCycleLogger.error("Unable to evaluate Variable " + key + " due to error: " + e, e);
@@ -225,7 +226,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
225226
variable = defaultVariable;
226227
variable.setEval(EvalReason.defaultReason(EvalReason.DefaultReasonDetailsEnum.USER_NOT_TARGETED));
227228
}
228-
evalHooksRunner.executeFinally(reversedHooks, hookContext, Optional.of(variable));
229+
evalHooksRunner.executeFinally(reversedHooks, hookContext, Optional.ofNullable(sdkVariable));
229230
}
230231
return variable;
231232
}

0 commit comments

Comments
 (0)