-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
|
||
await requestor.fetchAndStoreConfigurations(); | ||
|
||
const config = requestor.getConfiguration(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice defensiveness!
// Attempt to modify the returned data | ||
flags['feature-a'].enabled = false; | ||
|
||
// Verify original configuration remains unchanged | ||
expect(originalFlags['feature-a'].enabled).toBeTruthy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
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; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
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
ImmutableConfiguration
class which creates a deep copy of the payload dataHow has this been tested?