-
Couldn't load subscription status.
- Fork 50
feat: add hook data #1587
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
toddbaert
merged 23 commits into
open-feature:main
from
open-feature-forking:feat/hook-data
Sep 15, 2025
Merged
feat: add hook data #1587
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
13dbeee
Added hook data
mdxabu 549e97c
to support null values while maintaining thread safety, you must avoi…
mdxabu c06c679
Build successfully runned:spotless plugin
mdxabu e00885f
Merge branch 'main' into main
aepfli e0ad9d9
Merge branch 'main' into main
beeme1mr f4c97ee
Merge branch 'main' into main
aepfli ce1a60b
fixup: fix comment
aepfli bc883c5
fixup: remove synchronized map
aepfli 1339f2f
fixup: removed unneeded import
aepfli c66e2c8
fixup: spotless
aepfli d9ebe63
Update src/main/java/dev/openfeature/sdk/HookData.java
mdxabu f3ea94d
Update src/main/java/dev/openfeature/sdk/HookData.java
mdxabu 81f2e4f
feat: add support for hook data sharing between stages
alexandraoberaigner 6d4f700
fixup: tests
aepfli a4fc956
fixup: pr comments and extraction of own pair class
aepfli 0dee929
feat: add HookContext decorator
aepfli 1de7aea
fixup: add default method for interface
aepfli 52cbaa7
feat: performance boost
aepfli 0b224de
feat: performance improvements
aepfli 711e0be
fixup: lazy init
aepfli c3f8f36
fixup: pr comments
aepfli 88afd35
Merge branch 'main' into feat/hook-data
aepfli 76445d2
Merge branch 'main' into feat/hook-data
aepfli 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
50 changes: 50 additions & 0 deletions
50
src/main/java/dev/openfeature/sdk/HookContextWithData.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,50 @@ | ||
| package dev.openfeature.sdk; | ||
|
|
||
| class HookContextWithData<T> implements HookContext<T> { | ||
| private final HookContext<T> context; | ||
| private final HookData data; | ||
|
|
||
| private HookContextWithData(HookContext<T> context, HookData data) { | ||
| this.context = context; | ||
| this.data = data; | ||
| } | ||
|
|
||
| public static <T> HookContextWithData<T> of(HookContext<T> context, HookData data) { | ||
| return new HookContextWithData<>(context, data); | ||
| } | ||
|
|
||
| @Override | ||
| public String getFlagKey() { | ||
| return context.getFlagKey(); | ||
| } | ||
|
|
||
| @Override | ||
| public FlagValueType getType() { | ||
| return context.getType(); | ||
| } | ||
|
|
||
| @Override | ||
| public T getDefaultValue() { | ||
| return context.getDefaultValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public EvaluationContext getCtx() { | ||
| return context.getCtx(); | ||
| } | ||
|
|
||
| @Override | ||
| public ClientMetadata getClientMetadata() { | ||
| return context.getClientMetadata(); | ||
| } | ||
|
|
||
| @Override | ||
| public Metadata getProviderMetadata() { | ||
| return context.getProviderMetadata(); | ||
| } | ||
|
|
||
| @Override | ||
| public HookData getHookData() { | ||
| return data; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/dev/openfeature/sdk/HookContextWithoutData.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 dev.openfeature.sdk; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NonNull; | ||
| import lombok.Setter; | ||
| import lombok.With; | ||
|
|
||
| /** | ||
| * A data class to hold immutable context that {@link Hook} instances use. | ||
| * | ||
| * @param <T> the type for the flag being evaluated | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @With | ||
| @Setter(AccessLevel.PRIVATE) | ||
| class HookContextWithoutData<T> implements HookContext<T> { | ||
| @NonNull String flagKey; | ||
|
|
||
| @NonNull FlagValueType type; | ||
|
|
||
| @NonNull T defaultValue; | ||
|
|
||
| @Setter(AccessLevel.PACKAGE) | ||
| @NonNull EvaluationContext ctx; | ||
|
|
||
| ClientMetadata clientMetadata; | ||
| Metadata providerMetadata; | ||
|
|
||
| /** | ||
| * Builds a {@link HookContextWithoutData} instances from request data. | ||
| * | ||
| * @param key feature flag key | ||
| * @param type flag value type | ||
| * @param clientMetadata info on which client is calling | ||
| * @param providerMetadata info on the provider | ||
| * @param defaultValue Fallback value | ||
| * @param <T> type that the flag is evaluating against | ||
| * @return resulting context for hook | ||
| */ | ||
| static <T> HookContextWithoutData<T> from( | ||
| String key, FlagValueType type, ClientMetadata clientMetadata, Metadata providerMetadata, T defaultValue) { | ||
| return new HookContextWithoutData<>( | ||
| key, type, defaultValue, ImmutableContext.EMPTY, clientMetadata, providerMetadata); | ||
| } | ||
| } |
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,81 @@ | ||
| package dev.openfeature.sdk; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Hook data provides a way for hooks to maintain state across their execution stages. | ||
| * Each hook instance gets its own isolated data store that persists only for the duration | ||
| * of a single flag evaluation. | ||
| */ | ||
| public interface HookData { | ||
|
|
||
| /** | ||
| * Sets a value for the given key. | ||
| * | ||
| * @param key the key to store the value under | ||
| * @param value the value to store | ||
| */ | ||
| void set(String key, Object value); | ||
|
|
||
| /** | ||
| * Gets the value for the given key. | ||
| * | ||
| * @param key the key to retrieve the value for | ||
| * @return the value, or null if not found | ||
| */ | ||
| Object get(String key); | ||
|
|
||
| /** | ||
| * Gets the value for the given key, cast to the specified type. | ||
| * | ||
| * @param <T> the type to cast to | ||
| * @param key the key to retrieve the value for | ||
| * @param type the class to cast to | ||
| * @return the value cast to the specified type, or null if not found | ||
| * @throws ClassCastException if the value cannot be cast to the specified type | ||
| */ | ||
| <T> T get(String key, Class<T> type); | ||
|
|
||
| /** | ||
| * Default implementation uses a HashMap. | ||
| */ | ||
| static HookData create() { | ||
| return new DefaultHookData(); | ||
| } | ||
|
|
||
| /** | ||
| * Default implementation of HookData. | ||
| */ | ||
| class DefaultHookData implements HookData { | ||
| private Map<String, Object> data; | ||
|
|
||
| @Override | ||
| public void set(String key, Object value) { | ||
| if (data == null) { | ||
| data = new HashMap<>(); | ||
| } | ||
| data.put(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(String key) { | ||
| if (data == null) { | ||
| return null; | ||
| } | ||
| return data.get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T get(String key, Class<T> type) { | ||
| Object value = get(key); | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| if (!type.isInstance(value)) { | ||
| throw new ClassCastException("Value for key '" + key + "' is not of type " + type.getName()); | ||
| } | ||
| return type.cast(value); | ||
| } | ||
| } | ||
| } | ||
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.