-
Notifications
You must be signed in to change notification settings - Fork 0
feat: added evaluation hooks #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/devcycle/sdk/server/common/exception/AfterHookError.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.devcycle.sdk.server.common.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when an after hook fails during variable evaluation. | ||
| */ | ||
| public class AfterHookError extends RuntimeException { | ||
| public AfterHookError(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public AfterHookError(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/devcycle/sdk/server/common/exception/BeforeHookError.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.devcycle.sdk.server.common.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when a before hook fails during variable evaluation. | ||
| */ | ||
| public class BeforeHookError extends RuntimeException { | ||
| public BeforeHookError(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public BeforeHookError(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/devcycle/sdk/server/common/model/EvalHook.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.devcycle.sdk.server.common.model; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface EvalHook<T> { | ||
|
|
||
| default Optional<HookContext<T>> before(HookContext<T> ctx) { | ||
| return Optional.empty(); | ||
| } | ||
| default void after(HookContext<T> ctx, Variable<T> variable) {} | ||
| default void error(HookContext<T> ctx, Throwable e) {} | ||
| default void onFinally(HookContext<T> ctx, Optional<Variable<T>> variable) {} | ||
| } |
120 changes: 120 additions & 0 deletions
120
src/main/java/com/devcycle/sdk/server/common/model/EvalHooksRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package com.devcycle.sdk.server.common.model; | ||
|
|
||
| import com.devcycle.sdk.server.common.exception.AfterHookError; | ||
| import com.devcycle.sdk.server.common.exception.BeforeHookError; | ||
| import com.devcycle.sdk.server.common.logging.DevCycleLogger; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * A class that manages evaluation hooks for the DevCycle SDK. | ||
| * Provides functionality to add and clear hooks, storing them in an array. | ||
| */ | ||
| public class EvalHooksRunner<T> { | ||
| private List<EvalHook<T>> hooks; | ||
|
|
||
| /** | ||
| * Default constructor initializes an empty list of hooks. | ||
| */ | ||
| public EvalHooksRunner() { | ||
| this.hooks = new ArrayList<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a single hook to the collection. | ||
| * | ||
| * @param hook The hook to add | ||
| */ | ||
| public void addHook(EvalHook<T> hook) { | ||
| if (hook != null) { | ||
| hooks.add(hook); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears all hooks from the collection. | ||
| */ | ||
| public void clearHooks() { | ||
| hooks.clear(); | ||
| } | ||
|
|
||
| public List<EvalHook<T>> getHooks() { | ||
| return this.hooks; | ||
| } | ||
|
|
||
| /** | ||
| * Runs all before hooks in order. | ||
| * | ||
| * @param context The context to pass to the hooks | ||
| * @param <T> The type of the variable value | ||
| * @return The potentially modified context | ||
| */ | ||
| public <T> HookContext<T> executeBefore(ArrayList<EvalHook<T>> hooks, HookContext<T> context) { | ||
| HookContext<T> beforeContext = context; | ||
| for (EvalHook<T> hook : hooks) { | ||
suthar26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| Optional<HookContext<T>> newContext = hook.before(beforeContext); | ||
| if (newContext.isPresent()) { | ||
| beforeContext = beforeContext.merge(newContext.get()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new BeforeHookError("Before hook failed", e); | ||
| } | ||
JamieSinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return beforeContext; | ||
| } | ||
|
|
||
| /** | ||
| * Runs all after hooks in reverse order. | ||
| * | ||
| * @param context The context to pass to the hooks | ||
| * @param variable The variable result to pass to the hooks | ||
| * @param <T> The type of the variable value | ||
| */ | ||
| public void executeAfter(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Variable<T> variable) { | ||
| for (EvalHook<T> hook : hooks) { | ||
suthar26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| hook.after(context, variable); | ||
| } catch (Exception e) { | ||
| throw new AfterHookError("After hook failed", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs all error hooks in reverse order. | ||
| * | ||
| * @param context The context to pass to the hooks | ||
| * @param error The error that occurred | ||
| * @param <T> The type of the variable value | ||
| */ | ||
| public void executeError(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Throwable error) { | ||
| for (EvalHook<T> hook : hooks) { | ||
| try { | ||
| hook.error(context, error); | ||
| } catch (Exception hookError) { | ||
| // Log hook error but don't throw to avoid masking the original error | ||
| DevCycleLogger.error("Error hook failed: " + hookError.getMessage(), hookError); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs all finally hooks in reverse order. | ||
| * | ||
| * @param context The context to pass to the hooks | ||
| * @param variable The variable result to pass to the hooks (may be null) | ||
| */ | ||
| public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable) { | ||
| for (EvalHook<T> hook : hooks) { | ||
| try { | ||
| hook.onFinally(context, variable); | ||
| } catch (Exception e) { | ||
| // Log finally hook error but don't throw | ||
| DevCycleLogger.error("Finally hook failed: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/main/java/com/devcycle/sdk/server/common/model/HookContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.devcycle.sdk.server.common.model; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Context object passed to hooks during variable evaluation. | ||
| * Contains the user, variable key, default value, and additional context data. | ||
| */ | ||
| public class HookContext<T> { | ||
| private DevCycleUser user; | ||
| private final String key; | ||
| private final T defaultValue; | ||
| private Variable<T> variableDetails; | ||
|
|
||
| public HookContext(DevCycleUser user, String key, T defaultValue) { | ||
| this.user = user; | ||
| this.key = key; | ||
| this.defaultValue = defaultValue; | ||
| } | ||
|
|
||
| public HookContext(DevCycleUser user, String key, T defaultValue, Variable<T> variable) { | ||
| this.user = user; | ||
| this.key = key; | ||
| this.defaultValue = defaultValue; | ||
| this.variableDetails = variable; | ||
| } | ||
|
|
||
| public DevCycleUser getUser() { | ||
| return user; | ||
| } | ||
|
|
||
| public String getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public T getDefaultValue() { | ||
| return defaultValue; | ||
| } | ||
|
|
||
| public Variable<T> getVariableDetails() { return variableDetails; } | ||
|
|
||
| public HookContext<T> merge(HookContext<T> other) { | ||
| if (other == null) { | ||
| return this; | ||
| } | ||
| return new HookContext<>(other.getUser(), key, defaultValue, variableDetails); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.