Skip to content

Commit

Permalink
SLEIGHT-43: starting id rewriters
Browse files Browse the repository at this point in the history
  • Loading branch information
synkarius committed Sep 20, 2022
1 parent c50988d commit 29dd3b2
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/data/data-format-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SleightDataInternalFormat,
} from './data-formats';
import { SCHEMA_VERSION } from './exports/schema-version';
import { Ided } from './model/domain';
import { reduceIded } from './imports/model-update/reduce-ided';

export type FormatMapper = {
internalFormatToArrays: (
Expand All @@ -21,10 +21,6 @@ export type FormatMapper = {
};

export const getFormatMapper = (): FormatMapper => {
const reduceIded = <T extends Ided>(record: Record<string, T>, ided: T) => ({
...record,
[ided.id]: ided,
});
const internalFormatToArrays = (
data: SleightDataInternalFormat
): SleightDataArrays => {
Expand Down
6 changes: 3 additions & 3 deletions src/data/imports/import-data-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Command } from '../model/command/command';
import { Context } from '../model/context/context';
import { SpecDTO } from '../model/spec/spec-dto';
import { VariableDTO } from '../model/variable/variable-dto';
import { ModelUpdateEvaluator } from './role-key-overriding/model-update-evaluator';
import { createModelUpdateEvaluator } from './role-key-overriding/model-update-evaluator-factory';
import { ModelUpdateEvaluator } from './model-update/model-update-evaluator';
import { createModelUpdateEvaluator } from './model-update/model-update-evaluator-factory';
import {
getSelectorModelUpdateEvaluator,
SelectorModelUpdateEvaluator,
} from './role-key-overriding/selector-model-update-evaluator';
} from './model-update/selector-model-update-evaluator';

/** Merges import data over existing data. */
export type ImportDataMerger = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SleightDataInternalFormat } from '../../../data-formats';
import { createPauseAction } from '../../../model/action/pause/pause';
import { createCommand } from '../../../model/command/command';
import { getCommandActionIdsRewriter } from './command-action-ids-rewriter';

const createEmptySleightData = (): SleightDataInternalFormat => ({
actions: {},
commands: {},
contexts: {},
selectors: {},
specs: {},
variables: {},
});

describe('action id rewriter tests', () => {
it('should rewrite command action ids', () => {
const action = createPauseAction();
const command = { ...createCommand(), actionIds: [action.id] };
const commands = { [command.id]: command };
const data = {
...createEmptySleightData(),
commands,
};

const rewriter = getCommandActionIdsRewriter();
const newId = 'newId';
const rewrittenData = rewriter.rewriteId(action, newId, data);

const expected = {
...createEmptySleightData(),
commands: { [command.id]: { ...command, actionIds: [newId] } },
};
expect(rewrittenData).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Action } from '../../../model/action/action';
import { reduceIded } from '../reduce-ided';
import { IdRewriter } from './id-rewriter';

export const getCommandActionIdsRewriter = (): IdRewriter<Action> => {
return {
rewriteId: (action, newId, data) => {
// replace id in action object and slice
const oldId = action.id;

// TODO: move this replacement to action id rewriter which uses this
// const removed = Object.values(data.actions)
// .filter((ided) => ided.id !== oldId)
// .reduce(reduceIded, {});
// const newAction = { ...action, id: newId };
// const added = { ...removed, [newId]: newAction };

// replace action id in commands
const newCommands = Object.values(data.commands)
.map((command) => {
if (command.actionIds.includes(oldId)) {
return {
...command,
actionIds: command.actionIds.map((actionId) =>
actionId === oldId ? newId : actionId
),
};
}
return command;
})
.reduce(reduceIded, {});

return { ...data, commands: newCommands };
},
};
};
10 changes: 10 additions & 0 deletions src/data/imports/model-update/id-rewriter/id-rewriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SleightDataInternalFormat } from '../../../data-formats';
import { Ided } from '../../../model/domain';

export type IdRewriter<E extends Ided> = {
rewriteId: (
ided: E,
newId: string,
data: SleightDataInternalFormat
) => SleightDataInternalFormat;
};
3 changes: 3 additions & 0 deletions src/data/imports/model-update/import-targetable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Lockable, RoleKeyed } from '../../model/domain';

export interface ImportTargetable extends RoleKeyed, Lockable {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Lockable, RoleKeyed } from '../../model/domain';
import { ImportTargetable } from './import-targetable';

export enum ModelUpdateEvaluationType {
DISCARD,
Expand Down Expand Up @@ -36,6 +36,6 @@ export type ModelUpdateEvaluation<E> =
| PassThroughEvaluation<E>
| ModelUpdateEvaluationFailure<E>;

export type ModelUpdateEvaluator<E extends RoleKeyed & Lockable> = {
export type ModelUpdateEvaluator<E extends ImportTargetable> = {
evaluate: (candidate: E, baseDataElements: E[]) => ModelUpdateEvaluation<E>;
};
9 changes: 9 additions & 0 deletions src/data/imports/model-update/reduce-ided.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Ided } from '../../model/domain';

export const reduceIded = <T extends Ided>(
record: Record<string, T>,
ided: T
) => ({
...record,
[ided.id]: ided,
});

0 comments on commit 29dd3b2

Please sign in to comment.