From cbe56167f9220558f829e381c4d638f69aa009d7 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Tue, 24 Jan 2023 11:41:31 +0100 Subject: [PATCH] feat: Improve typing of migrate config --- app/charts/index.ts | 1 + app/configurator/config-types.ts | 1 + app/utils/chart-config/versioning.spec.ts | 15 ++++---- app/utils/chart-config/versioning.ts | 42 ++++++++++++++--------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/app/charts/index.ts b/app/charts/index.ts index 266c338a08..4dd2b23b62 100644 --- a/app/charts/index.ts +++ b/app/charts/index.ts @@ -201,6 +201,7 @@ export const getInitialSymbolLayer = ({ componentIri: component.iri, measureIri: measure?.iri || FIELD_VALUE_NONE, color: DEFAULT_FIXED_COLOR_FIELD, + show: false, }; }; diff --git a/app/configurator/config-types.ts b/app/configurator/config-types.ts index 6f10fda2bc..913a4057a3 100644 --- a/app/configurator/config-types.ts +++ b/app/configurator/config-types.ts @@ -495,6 +495,7 @@ const MapAreaLayer = t.type({ export type MapAreaLayer = t.TypeOf; const MapSymbolLayer = t.type({ + show: t.boolean, componentIri: t.string, // symbol radius (size) measureIri: t.string, diff --git a/app/utils/chart-config/versioning.spec.ts b/app/utils/chart-config/versioning.spec.ts index 16cc697f5c..02d633ddb7 100644 --- a/app/utils/chart-config/versioning.spec.ts +++ b/app/utils/chart-config/versioning.spec.ts @@ -1,4 +1,4 @@ -import { decodeChartConfig } from "@/configurator/config-types"; +import { decodeChartConfig, MapConfig } from "@/configurator/config-types"; import { migrateChartConfig } from "./versioning"; @@ -56,17 +56,20 @@ describe("config migrations", () => { const migratedOldConfig = migrateChartConfig(decodedConfig, { toVersion: "1.0.0", - }); + }) as MapConfig; expect(migratedOldConfig.version).toEqual("1.0.0"); - expect(migratedOldConfig.fields.symbolLayer.show).toEqual(false); + const symbolLayer = migratedOldConfig.fields.symbolLayer!; + console.log({ symbolLayer }); + // @ts-ignore + expect(symbolLayer.show).toEqual(false); // Should migrate "GeoCoordinatesDimensionIri" to iri defined in Area Layer. - expect(migratedOldConfig.fields.symbolLayer.componentIri).toEqual( + expect(symbolLayer.componentIri).toEqual( oldConfig.fields.areaLayer.componentIri ); - expect(migratedOldConfig.fields.symbolLayer.measureIri).toEqual( + expect(symbolLayer.measureIri).toEqual( oldConfig.fields.areaLayer.measureIri ); - expect(migratedOldConfig.fields.symbolLayer.color).toEqual("#1f77b4"); + expect(symbolLayer.color).toEqual("#1f77b4"); }); it("should migrate to initial config from migrated config for minor version changes", () => { diff --git a/app/utils/chart-config/versioning.ts b/app/utils/chart-config/versioning.ts index 9f0230ab88..d26d866651 100644 --- a/app/utils/chart-config/versioning.ts +++ b/app/utils/chart-config/versioning.ts @@ -1,5 +1,7 @@ import produce from "immer"; +import { ChartConfig } from "@/configurator"; + export const CHART_CONFIG_VERSION = "1.3.0"; type Migration = { @@ -484,25 +486,33 @@ export const migrateChartConfig = ( fromVersion, toVersion = CHART_CONFIG_VERSION, }: { fromVersion?: string; toVersion?: string } = {} -): any => { - const fromVersionFinal = fromVersion || config.version || "1.0.0"; - const direction = upOrDown(fromVersionFinal, toVersion); - - if (direction === "same") { - return config; - } +): ChartConfig => { + const _migrateChartConfig = ( + config: any, + { + fromVersion, + toVersion = CHART_CONFIG_VERSION, + }: { fromVersion?: string; toVersion?: string } = {} + ): any => { + const fromVersionFinal = fromVersion || config.version || "1.0.0"; + const direction = upOrDown(fromVersionFinal, toVersion); + + if (direction === "same") { + return config; + } - const currentMigration = migrations.find( - (migration) => - migration[direction === "up" ? "from" : "to"] === fromVersionFinal - ); + const currentMigration = migrations.find( + (migration) => + migration[direction === "up" ? "from" : "to"] === fromVersionFinal + ); - if (currentMigration) { - const newConfig = currentMigration[direction](config); - return migrateChartConfig(newConfig, { fromVersion, toVersion }); - } + if (currentMigration) { + const newConfig = currentMigration[direction](config); + return _migrateChartConfig(newConfig, { fromVersion, toVersion }); + } + }; - return config; + return _migrateChartConfig(config, { fromVersion, toVersion }); }; const upOrDown = (