Skip to content
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

feat: ImmuatableConfiguration #231

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open

feat: ImmuatableConfiguration #231

wants to merge 34 commits into from

Conversation

typotter
Copy link
Collaborator

@typotter typotter commented Mar 5, 2025


labels: mergeable

Fixes: #issue

Motivation and Context

In order to isolate the internal configuration from any changes outside a configuration fetch, we want to make available only an immutable version of the Configuration. In java, for instance, we can leverage immutable maps and objects to prevent changes. Here, we use a copy or a snapshot of the configuration.

Description

  • New ImmutableConfiguration class which creates a deep copy of the payload data

How has this been tested?

  • tests


await requestor.fetchAndStoreConfigurations();

const config = requestor.getConfiguration();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since config is a snapshot, we take it after the fetch&store

@@ -11,6 +11,7 @@ import { BanditVariation, BanditParameters, Flag, BanditReference } from './inte
export default class ConfigurationRequestor {
private banditModelVersions: string[] = [];
private readonly configuration: StoreBackedConfiguration;
private configurationCopy: IConfiguration;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save and return the copy only

Copy link
Contributor

@aarsilv aarsilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice defensiveness!

Comment on lines +537 to +541
// Attempt to modify the returned data
flags['feature-a'].enabled = false;

// Verify original configuration remains unchanged
expect(originalFlags['feature-a'].enabled).toBeTruthy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Comment on lines +193 to +198
this.flags = JSON.parse(JSON.stringify(flags));
this.flagConfigDetails = JSON.parse(JSON.stringify(flagConfigDetails));
this.banditVariations = banditVariations
? JSON.parse(JSON.stringify(banditVariations))
: undefined;
this.bandits = bandits ? JSON.parse(JSON.stringify(bandits || {})) : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to further inforce immutability, you could look into leveraging Object.freeze()

@@ -25,14 +26,15 @@ export default class ConfigurationRequestor {
this.banditVariationConfigurationStore,
this.banditModelConfigurationStore,
);
this.configurationCopy = this.configuration.copy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be large; I wonder if we do the copying just in time when getConfiguration() is called. We could still check this.configurationCopy so that we only do it once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants