Skip to content

Commit 97de14c

Browse files
fix: update hooks to include feature (#184)
* chore: refactor imports, add builder.default to platformData sdkPlatform attribute * fix: update hooks to have VariableMetadata passed to after and onFinally hooks to add variable specific metadata
1 parent 26d63e9 commit 97de14c

File tree

9 files changed

+204
-98
lines changed

9 files changed

+204
-98
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.executeAfter(reversedHooks, context, variable, null);
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.executeFinally(reversedHooks, context, Optional.ofNullable(variable), null);
174174
}
175175
return variable;
176176
}

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

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

33
import java.util.Optional;
44

5+
import com.devcycle.sdk.server.local.model.VariableMetadata;
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) {}
12+
default void after(HookContext<T> ctx, Variable<T> variable, VariableMetadata variableMetadata) {}
1113
default void error(HookContext<T> ctx, Throwable e) {}
12-
default void onFinally(HookContext<T> ctx, Optional<Variable<T>> variable) {}
14+
default void onFinally(HookContext<T> ctx, Optional<Variable<T>> variable, VariableMetadata variableMetadata) {}
1315
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.devcycle.sdk.server.common.model;
22

3-
import com.devcycle.sdk.server.common.exception.AfterHookError;
4-
import com.devcycle.sdk.server.common.exception.BeforeHookError;
5-
import com.devcycle.sdk.server.common.logging.DevCycleLogger;
6-
73
import java.util.ArrayList;
84
import java.util.List;
95
import java.util.Optional;
106

7+
import com.devcycle.sdk.server.common.exception.AfterHookError;
8+
import com.devcycle.sdk.server.common.exception.BeforeHookError;
9+
import com.devcycle.sdk.server.common.logging.DevCycleLogger;
10+
import com.devcycle.sdk.server.local.model.VariableMetadata;
11+
1112
/**
1213
* A class that manages evaluation hooks for the DevCycle SDK.
1314
* Provides functionality to add and clear hooks, storing them in an array.
@@ -81,16 +82,16 @@ 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, Variable<T> variable, VariableMetadata variableMetadata) {
8586
for (EvalHook<T> hook : hooks) {
8687
try {
87-
hook.after(context, variable);
88+
hook.after(context, variable, variableMetadata);
8889
} catch (Exception e) {
8990
throw new AfterHookError("After hook failed", e);
9091
}
9192
}
9293
}
93-
94+
9495
/**
9596
* Runs all error hooks in reverse order.
9697
*
@@ -115,10 +116,10 @@ public void executeError(ArrayList<EvalHook<T>> hooks, HookContext<T> context, T
115116
* @param context The context to pass to the hooks
116117
* @param variable The variable result to pass to the hooks (may be null)
117118
*/
118-
public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable) {
119+
public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable, VariableMetadata variableMetadata) {
119120
for (EvalHook<T> hook : hooks) {
120121
try {
121-
hook.onFinally(context, variable);
122+
hook.onFinally(context, variable, variableMetadata);
122123
} catch (Exception e) {
123124
// Log finally hook error but don't throw
124125
DevCycleLogger.error("Finally hook failed: " + e.getMessage(), e);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.devcycle.sdk.server.common.model;
22

3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
36
import com.devcycle.sdk.server.common.logging.DevCycleLogger;
47
import com.fasterxml.jackson.annotation.JsonInclude;
58
import com.fasterxml.jackson.annotation.JsonValue;
69
import com.fasterxml.jackson.core.JsonProcessingException;
710
import com.fasterxml.jackson.databind.ObjectMapper;
811
import com.fasterxml.jackson.databind.node.ObjectNode;
12+
913
import io.swagger.v3.oas.annotations.media.Schema;
1014
import lombok.Builder;
1115
import lombok.Data;
1216

13-
import java.net.InetAddress;
14-
import java.net.UnknownHostException;
15-
1617
@Data
1718
@Builder
1819
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -34,6 +35,7 @@ public class PlatformData {
3435
private String sdkVersion = "2.8.1";
3536

3637
@Schema(description = "DevCycle SDK Platform")
38+
@Builder.Default
3739
private String sdkPlatform = null;
3840

3941
@Schema(description = "Hostname where the SDK is running")

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.devcycle.sdk.server.local.model.BucketedUserConfig;
2828
import com.devcycle.sdk.server.local.model.ConfigMetadata;
2929
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
30+
import com.devcycle.sdk.server.local.model.VariableMetadata;
3031
import com.devcycle.sdk.server.local.protobuf.SDKVariable_PB;
3132
import com.devcycle.sdk.server.local.protobuf.VariableForUserParams_PB;
3233
import com.devcycle.sdk.server.local.protobuf.VariableType_PB;
@@ -181,6 +182,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
181182

182183
HookContext<T> hookContext = new HookContext<T>(user, key, defaultValue, getMetadata());
183184
Variable<T> variable = null;
185+
VariableMetadata variableMetadata = null;
184186
ArrayList<EvalHook<T>> hooks = new ArrayList<EvalHook<T>>(evalHooksRunner.getHooks());
185187
ArrayList<EvalHook<T>> reversedHooks = new ArrayList<EvalHook<T>>(evalHooksRunner.getHooks());
186188
Collections.reverse(reversedHooks);
@@ -207,12 +209,13 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
207209
variable.setEval(EvalReason.defaultReason(EvalReason.DefaultReasonDetailsEnum.VARIABLE_TYPE_MISMATCH));
208210
} else {
209211
variable = ProtobufUtils.createVariable(sdkVariable, defaultValue);
212+
variableMetadata = new VariableMetadata(sdkVariable.getFeature().getValue());
210213
}
211214
}
212215
if (beforeError != null) {
213216
throw beforeError;
214217
}
215-
evalHooksRunner.executeAfter(reversedHooks, hookContext, variable);
218+
evalHooksRunner.executeAfter(reversedHooks, hookContext, variable, variableMetadata);
216219
} catch (Throwable e) {
217220
if (!(e instanceof BeforeHookError)) {
218221
DevCycleLogger.error("Unable to evaluate Variable " + key + " due to error: " + e, e);
@@ -225,7 +228,7 @@ public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue) {
225228
variable = defaultVariable;
226229
variable.setEval(EvalReason.defaultReason(EvalReason.DefaultReasonDetailsEnum.USER_NOT_TARGETED));
227230
}
228-
evalHooksRunner.executeFinally(reversedHooks, hookContext, Optional.of(variable));
231+
evalHooksRunner.executeFinally(reversedHooks, hookContext, Optional.ofNullable(variable), variableMetadata);
229232
}
230233
return variable;
231234
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.devcycle.sdk.server.local.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
import lombok.AllArgsConstructor;
6+
7+
@AllArgsConstructor
8+
@JsonIgnoreProperties(ignoreUnknown = true)
9+
public class VariableMetadata {
10+
11+
public final String featureId;
12+
13+
}

0 commit comments

Comments
 (0)