Skip to content

WIP: Parse SSRC JSON strings into objects #2504

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/remote-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
EvaluationContext,
ExplicitParameterValue,
InAppDefaultValue,
JSONObject,
ListVersionsOptions,
ListVersionsResult,
MicroPercentRange,
Expand Down
13 changes: 12 additions & 1 deletion src/remote-config/remote-config-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,15 @@ export interface ListVersionsOptions {
/**
* Represents the configuration produced by evaluating a server template.
*/
export type ServerConfig = { [key: string]: string | boolean | number }
export type ServerConfig = { [key: string]: string | boolean | number | JSONObject }

/**
* Represents a parameter value of type JSON parsed into an object.
*/
export type JSONObject =
| string
| number
| boolean
| null
| JSONObject[]
| {[key: string]: JSONObject};
11 changes: 10 additions & 1 deletion src/remote-config/remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as validator from '../utils/validator';
import { FirebaseRemoteConfigError, RemoteConfigApiClient } from './remote-config-api-client-internal';
import { ConditionEvaluator } from './condition-evaluator-internal';
import {
JSONObject,
ListVersionsOptions,
ListVersionsResult,
RemoteConfigCondition,
Expand Down Expand Up @@ -390,7 +391,7 @@ class ServerTemplateImpl implements ServerTemplate {
* Private helper method that coerces a parameter value string to the {@link ParameterValueType}.
*/
private parseRemoteConfigParameterValue(parameterType: ParameterValueType | undefined,
parameterValue: string): string | number | boolean {
parameterValue: string): string | number | boolean | JSONObject {
const BOOLEAN_TRUTHY_VALUES = ['1', 'true', 't', 'yes', 'y', 'on'];
const DEFAULT_VALUE_FOR_NUMBER = 0;
const DEFAULT_VALUE_FOR_STRING = '';
Expand All @@ -403,6 +404,14 @@ class ServerTemplateImpl implements ServerTemplate {
return DEFAULT_VALUE_FOR_NUMBER;
}
return num;
} else if (parameterType === 'JSON') {
let parsed = {};
try {
parsed = JSON.parse(parameterValue);
} catch (e) {
// TODO: add logging once we have a wrapped logger.
}
return parsed as JSONObject
} else {
// Treat everything else as string
return parameterValue || DEFAULT_VALUE_FOR_STRING;
Expand Down
7 changes: 6 additions & 1 deletion test/unit/remote-config/remote-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,12 @@ describe('RemoteConfig', () => {
expect(config.dog_type).to.equal('corgi');
expect(config.dog_type_enabled).to.equal(true);
expect(config.dog_age).to.equal(22);
expect(config.dog_jsonified).to.equal('{"name":"Taro","breed":"Corgi","age":1,"fluffiness":100}');
expect(config.dog_jsonified).to.deep.equal({
name: 'Taro',
breed: 'Corgi',
age: 1,
fluffiness: 100
});
});
});

Expand Down