upgrade geography configuration; handle json and json array types

This commit is contained in:
Sylvain Briat
2024-01-31 12:26:50 +01:00
parent 5a5f1172a4
commit a0757adc7d
9 changed files with 305 additions and 198 deletions

View File

@@ -12,8 +12,8 @@ export interface GeographyConfig extends Config {
geocoderConsolidate: boolean;
geocoderProximity: number;
geocoderPopulationPrioritizerCoef: number;
geocoderProviders: string[];
geocoderProvidersFallback: string[];
geocoderProviders: object[];
geocoderProvidersFallback: object[];
}
export default registerAs('geography', () => ({
@@ -47,9 +47,9 @@ export default registerAs('geography', () => ({
? parseInt(process.env.GEOCODER_POPULATION_PRIORITIZER_COEF)
: 100,
geocoderProviders: process.env.GEOCODER_PROVIDERS
? process.env.GEOCODER_PROVIDERS?.split(',')
: ['ADDOK,PELIAS_SEARCH,PELIAS_AUTOCOMPLETE'],
? JSON.parse(process.env.GEOCODER_PROVIDERS)
: [],
geocoderProvidersFallback: process.env.GEOCODER_PROVIDERS_FALLBACK
? process.env.GEOCODER_PROVIDERS_FALLBACK?.split(',')
: ['GMAPS'],
? JSON.parse(process.env.GEOCODER_PROVIDERS_FALLBACK)
: [],
}));

View File

@@ -16,7 +16,7 @@ export class ConfigurationMapper {
const response = new ConfigurationResponseDto();
response.domain = identifier.domain;
response.key = identifier.key;
response.value = value;
response.value = typeof value === 'object' ? JSON.stringify(value) : value;
response.type = this.configurationsManager.configurationType(value);
return response;
};

View File

@@ -69,6 +69,8 @@ export class ConfigurationsManagerService {
return Type.FLOAT;
case 'boolean':
return Type.BOOLEAN;
case 'object':
return Type.JSON;
default:
if (value.indexOf(',') === -1) return Type.STRING;
return this._configurationTypeArray(value.split(','));
@@ -80,7 +82,9 @@ export class ConfigurationsManagerService {
? Type.INT_ARRAY
: value.every((item) => typeof item === 'number')
? Type.FLOAT_ARRAY
: Type.STRING_ARRAY;
: value.every((item) => typeof item === 'object')
? Type.JSON_ARRAY
: Type.STRING_ARRAY;
};
cast = (value: string, type: Type): Value => {
@@ -91,6 +95,9 @@ export class ConfigurationsManagerService {
return parseInt(value);
case Type.FLOAT:
return parseFloat(value);
case Type.JSON:
case Type.JSON_ARRAY:
return JSON.parse(value);
case Type.INT_ARRAY:
return value.split(',').map((item: string) => parseInt(item));
case Type.FLOAT_ARRAY:

View File

@@ -46,7 +46,9 @@ export class PopulateService implements OnApplicationBootstrap {
this.setConfigurationRepository.set(
config.domain,
key,
`${configuration[key]}`,
typeof configuration[key] === 'object'
? JSON.stringify(configuration[key])
: `${configuration[key]}`,
);
}
}

View File

@@ -3,6 +3,6 @@ import { Type } from '@mobicoop/configuration-module';
export class ConfigurationResponseDto {
domain: string;
key: string;
value: string | boolean | number | string[] | number[];
value: string | boolean | number | object | string[] | number[] | object[];
type: Type;
}

View File

@@ -32,8 +32,8 @@ const mockConfigService = {
geocoderConsolidate: true,
geocoderProximity: 5,
geocoderPopulationPrioritizerCoef: 100,
geocoderProviders: ['provider1', 'provider2'],
geocoderProvidersFallback: ['provider3'],
geocoderProviders: [{ name: 'provider1' }, { name: 'provider2' }],
geocoderProvidersFallback: [{ name: 'provider3' }],
};
case 'match':
return {
@@ -122,6 +122,11 @@ describe('Configurations Manager Service', () => {
Type.STRING,
);
});
it('should return the configuration type of a json object', () => {
expect(
configurationsManagerService.configurationType({ key: 'value' }),
).toBe(Type.JSON);
});
it('should return the configuration type of a string array', () => {
expect(
configurationsManagerService.configurationType(['test', 'test2']),
@@ -140,6 +145,14 @@ describe('Configurations Manager Service', () => {
Type.FLOAT_ARRAY,
);
});
it('should return the configuration type of a json array', () => {
expect(
configurationsManagerService.configurationType([
{ key1: 'value1' },
{ key2: 'value2' },
]),
).toBe(Type.JSON_ARRAY);
});
});
describe('cast', () => {
@@ -159,6 +172,11 @@ describe('Configurations Manager Service', () => {
'role',
);
});
it('should cast a string to json object', () => {
expect(
configurationsManagerService.cast('{"key":"value"}', Type.JSON),
).toStrictEqual({ key: 'value' });
});
it('should cast a string to an array of strings', () => {
expect(
configurationsManagerService.cast('test,test2', Type.STRING_ARRAY),
@@ -174,5 +192,13 @@ describe('Configurations Manager Service', () => {
configurationsManagerService.cast('1.2,2.3', Type.FLOAT_ARRAY),
).toStrictEqual([1.2, 2.3]);
});
it('should cast a string to an array of json objects', () => {
expect(
configurationsManagerService.cast(
'[{"key1":"value1"},{"key2":"value2"}]',
Type.JSON_ARRAY,
),
).toStrictEqual([{ key1: 'value1' }, { key2: 'value2' }]);
});
});
});