-
Notifications
You must be signed in to change notification settings - Fork 397
Add #set and #toJSON to RC ServerTemplate #2522
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b171d2b
Add #set on ServerTemplate to allow for setting and caching a server …
trekforever f427727
Simplify initServerTemplate to make use of the new setter
trekforever 3d8d3d4
Update tests
trekforever 18f5630
Address comments and feedback
trekforever 44101ea
Update API docs
trekforever 05ba48c
Add export for new type ServerTemplateDataType to index.ts
trekforever 9bbe9e3
Update some inline comments
trekforever 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
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
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
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 |
---|---|---|
|
@@ -40,6 +40,7 @@ import { | |
DefaultConfig, | ||
GetServerTemplateOptions, | ||
InitServerTemplateOptions, | ||
ServerTemplateDataType, | ||
} from './remote-config-api'; | ||
import { isString } from 'lodash'; | ||
|
||
|
@@ -200,20 +201,9 @@ export class RemoteConfig { | |
public initServerTemplate(options?: InitServerTemplateOptions): ServerTemplate { | ||
const template = new ServerTemplateImpl( | ||
this.client, new ConditionEvaluator(), options?.defaultConfig); | ||
|
||
if (options?.template) { | ||
// Check and instantiates the template via a json string | ||
if (isString(options?.template)) { | ||
try { | ||
template.cache = new ServerTemplateDataImpl(JSON.parse(options?.template)); | ||
} catch (e) { | ||
throw new FirebaseRemoteConfigError( | ||
'invalid-argument', | ||
`Failed to parse the JSON string: ${options?.template}. ` + e | ||
); | ||
} | ||
} else { | ||
template.cache = options?.template; | ||
} | ||
template.set(options?.template); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, intuitive reuse of |
||
} | ||
return template; | ||
} | ||
|
@@ -307,7 +297,7 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate { | |
* Remote Config dataplane template data implementation. | ||
*/ | ||
class ServerTemplateImpl implements ServerTemplate { | ||
public cache: ServerTemplateData; | ||
private cache: ServerTemplateData; | ||
private stringifiedDefaultConfig: {[key: string]: string} = {}; | ||
|
||
constructor( | ||
|
@@ -333,6 +323,27 @@ class ServerTemplateImpl implements ServerTemplate { | |
}); | ||
} | ||
|
||
/** | ||
* Parses a {@link ServerTemplateDataType} and caches it. | ||
*/ | ||
public set(template: ServerTemplateDataType): void { | ||
let parsed; | ||
if (isString(template)) { | ||
try { | ||
parsed = JSON.parse(template); | ||
} catch (e) { | ||
// Transforms JSON parse errors to Firebase error. | ||
throw new FirebaseRemoteConfigError( | ||
'invalid-argument', | ||
`Failed to parse the JSON string: ${template}. ` + e); | ||
} | ||
} else { | ||
parsed = template; | ||
} | ||
// Throws template parse errors. | ||
this.cache = new ServerTemplateDataImpl(parsed); | ||
} | ||
|
||
/** | ||
* Evaluates the current template in cache to produce a {@link ServerConfig}. | ||
*/ | ||
|
@@ -402,6 +413,13 @@ class ServerTemplateImpl implements ServerTemplate { | |
|
||
return new ServerConfigImpl(configValues); | ||
} | ||
|
||
/** | ||
* @returns JSON representation of the server template | ||
*/ | ||
public toJSON(): ServerTemplateData { | ||
return this.cache; | ||
} | ||
} | ||
|
||
class ServerConfigImpl implements ServerConfig { | ||
|
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.
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.
Just curious (because I think either is fine) so we need a separate type for this or should this just be
ServerTemplateData | string
here?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.
I think either works, I originally had it as
ServerTemplateData | string
but we are defining it in quite a few places across multiple files, so I moved it to a type 😃