Skip to content

Commit

Permalink
update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
ada-x64 committed Feb 12, 2024
1 parent 977520b commit 4662fc4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 51 deletions.
1 change: 1 addition & 0 deletions rust/perspective-viewer/src/ts/column_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type PerspectiveColumnConfigValue = {
datagrid_datetime_style?: {
timeZone?: string;
datetime_color_mode?: "foreground" | "background";
color?: string;
} & (
| {
format?: "custom";
Expand Down
41 changes: 23 additions & 18 deletions rust/perspective-viewer/src/ts/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const PKG_VERSION = packageJSON.version;
* script's package.
*/
export function convert(
old: Record<string, unknown> | ArrayBuffer | string,
old: InitialConfig | ArrayBuffer | string,
{
warn = true,
verbose = false,
Expand All @@ -71,33 +71,39 @@ export function convert(
): PerspectiveViewerConfig | ArrayBuffer | string {
if (typeof old === "object" && !(old instanceof ArrayBuffer)) {
const copy = JSON.parse(JSON.stringify(old));
let options = {
warn: verbose ? true : warn,
verbose,
replace_defaults,
};
if ("viewers" in copy && "detail" in copy) {
return migrate_workspace(copy, { warn, replace_defaults });
return migrate_workspace(copy, options);
} else {
return migrate_viewer(copy, false, {
warn: verbose ? true : warn,
verbose,
replace_defaults,
});
return migrate_viewer(copy, false, options);
}
} else {
return old;
}
}

type InitialConfig = Record<string, unknown>;

export type PerspectiveConvertOptions = {
warn?: boolean;
verbose?: boolean;
replace_defaults?: boolean;
omit_attributes?: boolean;
};

export type Options = PerspectiveConvertOptions & { version_chain?: string[] };

/**
* Migrate a layout for `<perspective-workspace>`
* @param old
* @param old - TODO this should get a WorkspaceConfig type
* @param options
* @returns
*/
function migrate_workspace(old, options) {
function migrate_workspace(old: any, options: Options) {
for (const key in old.viewers) {
old.viewers[key] = migrate_viewer(old.viewers[key], true, options);
if (!("master" in old.viewers[key])) {
Expand Down Expand Up @@ -128,7 +134,7 @@ function migrate_workspace(old, options) {
* @param options
* @returns
*/
function migrate_viewer(old, omit_attributes, options) {
function migrate_viewer(old: any, omit_attributes: boolean, options: Options) {
old.version = old.version ? new Semver(old.version) : new Semver("0.0.0");
options.omit_attributes = omit_attributes;
// This array details the "next version" in line. It begins with 2.6.1
Expand All @@ -147,7 +153,12 @@ function migrate_viewer(old, omit_attributes, options) {
migrate_2_6_1,
migrate_2_7_1,
assure_latest,
semver_to_string,
(old) => {
return {
...old,
version: old.version.to_string(),
};
},
],
options
);
Expand All @@ -166,20 +177,14 @@ function assure_latest(old: { version: Semver }) {
}
}

function semver_to_string(old) {
// intentionally ignores build
old.version = `${old.version.major}.${old.version.minor}.${old.version.patch}`;
return old;
}

/**
* Chains functions of `args` and apply to `old`
* @param old
* @param args
* @param options
* @returns
*/
export function chain(old, args, options) {
export function chain(old: object, args: Function[], options: Options) {
for (const arg of args) {
old = arg(old, options);
}
Expand Down
8 changes: 4 additions & 4 deletions rust/perspective-viewer/src/ts/migrate/0-0-0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

import { chain } from "../migrate";
import { Options, chain } from "../migrate";
import Semver from "./semver";

/**
Expand All @@ -20,7 +20,7 @@ import Semver from "./semver";
* @param options
* @returns The migrated viewer.
*/
export default function migrate_0_0_0(old, options) {
export default function migrate_0_0_0(old: any, options: Options) {
const next_version = options.version_chain.shift();
if (old.version?.gt(next_version)) {
return old;
Expand All @@ -36,7 +36,7 @@ export default function migrate_0_0_0(old, options) {
migrate_split_by,
migrate_filters,
migrate_expressions,
options.replace_defaults ? migrate_nulls : false,
options.replace_defaults ? migrate_nulls : (x) => x,
migrate_plugins,
migrate_plugin_config,
migrate_title,
Expand All @@ -48,7 +48,7 @@ export default function migrate_0_0_0(old, options) {
old.version = new Semver("0.0.0");
return old;
},
].filter((x) => !!x),
],
options
);

Expand Down
38 changes: 20 additions & 18 deletions rust/perspective-viewer/src/ts/migrate/2-6-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

import { ViewConfig } from "@finos/perspective";
import Semver from "./semver";
import { Options } from "../migrate";

/**
* Migrates from 2.6.1
* @param old
* @param options
* @returns
*/
export default function migrate_2_6_1(old, options) {
export default function migrate_2_6_1(old: any, options: Options) {
let next_version = options.version_chain.shift();
if (old.version?.gt(next_version)) {
return old;
Expand Down Expand Up @@ -56,26 +57,27 @@ export default function migrate_2_6_1(old, options) {
}

// check for string expressions, replace with objects
let new_exprs = {};
for (let i in old.expressions) {
if (typeof old.expressions[i] === "string") {
if (options.warn) {
console.warn(
"Replacing deprecated string expression with object"
);
}
let old_expr = old.expressions[i];
let [whole_expr, name, expr] = old_expr.match(
/\/\/\s*([^\n]+)\n(.*)/
) ?? [old_expr, null, null];
if (name && expr) {
new_exprs[name] = expr;
} else {
new_exprs[whole_expr] = whole_expr;
if (Array.isArray(old.expressions)) {
let new_exprs = {};
for (let old_expr of old.expressions) {
if (typeof old_expr === "string") {
if (options.warn) {
console.warn(
"Replacing deprecated string expression with object"
);
}
let [whole_expr, name, expr] = old_expr.match(
/\/\/\s*([^\n]+)\n(.*)/
) ?? [old_expr, null, null];
if (name && expr) {
new_exprs[name] = expr;
} else {
new_exprs[whole_expr] = whole_expr;
}
}
}
old.expressions = new_exprs;
}
old.expressions = new_exprs;

if (options.verbose) {
console.log(old);
Expand Down
94 changes: 83 additions & 11 deletions rust/perspective-viewer/src/ts/migrate/2-7-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

import Semver from "./semver";
import { PerspectiveConvertOptions } from "../migrate";
import { Options } from "../migrate";
import { Config261 } from "./2-6-1";
import { ColumnConfig } from "../viewer";
import {
PerspectiveColumnConfig,
PerspectiveColumnConfigValue,
} from "../perspective-viewer";

/**
* Migrates from 2.6.1
* Migrates from 2.7.1. Focus is on plugin API changes, moving plugin_config.columns to column_config
* @param old
* @param options
* @returns
*/
export default function migrate_2_7_1(
old: Config261 & {
column_config?: ColumnConfig;
column_config?: PerspectiveColumnConfig;
},
options: PerspectiveConvertOptions & { version_chain: string[] }
options: Options
) {
let next_version = options.version_chain.shift();
if (old.version?.gt(next_version!)) {
Expand All @@ -35,10 +38,12 @@ export default function migrate_2_7_1(
}
old.version = new Semver(next_version!);

old.column_config = old.column_config ?? {};

const has_config = !!old.plugin_config?.columns;
const has_plugin =
old.plugin === "Datagrid" || old.plugin === "X/Y Scatter";
if (has_config || has_plugin) {
if (has_config && has_plugin) {
if (options.warn) {
console.warn("Migrating plugin_config to column_config!");
}
Expand All @@ -49,7 +54,7 @@ export default function migrate_2_7_1(
old.column_config[column_name] ?? {};

if (old.plugin === "X/Y Scatter") {
old.column_config[column_name].Symbols =
old.column_config[column_name].symbols =
old.plugin_config.columns[column_name]?.symbols ?? {};
delete old.plugin_config.columns[column_name];
} else {
Expand All @@ -59,9 +64,73 @@ export default function migrate_2_7_1(
delete old.plugin_config.columns[column_name]
.column_width_override;
for (const [key, val] of Object.entries(value_map)) {
old.column_config[column_name].styles =
old.column_config[column_name].styles ?? {};
old.column_config[column_name].styles[key] = val;
let control: keyof PerspectiveColumnConfigValue;
switch (key) {
case "number_fg_mode":
case "number_bg_mode":
case "fixed":
case "pos_fg_color":
case "neg_fg_color":
case "pos_bg_color":
case "neg_bg_color":
case "fg_gradient":
case "bg_gradient": {
control = "datagrid_number_style";
break;
}
case "timeZone":
case "datetime_color_mode":
case "fractionalSecondDigits":
case "second":
case "minute":
case "hour":
case "day":
case "weekday":
case "month":
case "year":
case "hour12":
case "dateStyle":
case "timeStyle": {
control = "datagrid_datetime_style";
break;
}
case "string_color_mode": {
control = "datagrid_string_style";
break;
}
case "format": {
if (val === "custom") {
control = "datagrid_datetime_style";
} else {
control = "datagrid_string_style";
}
break;
}
case "color": {
if (
//@ts-ignore
value_map.datetime_color_mode ||
old.column_config[column_name]
?.datagrid_datetime_style
?.datetime_color_mode
) {
control = "datagrid_datetime_style";
} else if (
//@ts-ignore
value_map.string_color_mode ||
old.column_config[column_name]
?.datagrid_string_style?.string_color_mode
) {
control = "datagrid_string_style";
}
break;
}
}

// @ts-ignore this is confused by union of struct types with symbols as Record<string, string>
old.column_config[column_name][control] =
old.column_config[column_name][control] ?? {};
old.column_config[column_name][control][key] = val;
delete old.plugin_config.columns[column_name][key];
}
if (!!cwo) {
Expand All @@ -73,7 +142,10 @@ export default function migrate_2_7_1(
}
}
}
if (Object.keys(old.plugin_config?.columns).length === 0) {
if (
old.plugin === "X/Y Scatter" &&
Object.keys(old.plugin_config?.columns).length === 0
) {
delete old.plugin_config?.columns;
}
}
Expand Down
4 changes: 4 additions & 0 deletions rust/perspective-viewer/src/ts/migrate/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default class Semver {
return this;
}

to_string() {
return `${this.major}.${this.minor}.${this.patch}`;
}

gt(val: string | Semver) {
let right = new Semver(val);
return (
Expand Down

0 comments on commit 4662fc4

Please sign in to comment.