diff --git a/CHANGELOG.md b/CHANGELOG.md index 687d5e6..040c296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## @i-vresse/wb-core 1.2.3 - 2024-02-02 + +### Changed + +* Export more workflow parsing and validation methods + ## @i-vresse/wb-core 1.2.2 - 2024-01-25 ### Added diff --git a/packages/core/package.json b/packages/core/package.json index d15ceb2..c3543b6 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@i-vresse/wb-core", - "version": "1.2.2", + "version": "1.2.3", "description": "React components to construct a workflow builder application", "keywords": [ "react", diff --git a/packages/core/src/toml.test.ts b/packages/core/src/toml.test.ts index 9f893dd..32afd61 100644 --- a/packages/core/src/toml.test.ts +++ b/packages/core/src/toml.test.ts @@ -1,6 +1,6 @@ import dedent from 'ts-dedent' import { expect, describe, it } from 'vitest' -import { dedupWorkflow, parseWorkflow, TomlSchemas, workflow2tomltext } from './toml' +import { dedupWorkflow, parseWorkflowByCatalogPieces, TomlSchemas, workflow2tomltext, tomlstring2table } from './toml' import { IParameters } from './types' describe('workflow2tomltext()', () => { @@ -373,7 +373,7 @@ param_C = 33 }) }) -describe('parseWorkflow()', () => { +describe('parseWorkflowByCatalogPieces()', () => { it('should divide global and module parameters', () => { const workflow = ` myglobalvar = 'something' @@ -383,7 +383,7 @@ myglobalvar = 'something' foo = 'bar' ` const globalKeys = new Set(['myglobalvar']) - const result = parseWorkflow(workflow, globalKeys, {}, {}) + const result = parseWorkflowByCatalogPieces(tomlstring2table(workflow), globalKeys, {}, {}) const expected = { global: { myglobalvar: 'something' @@ -411,7 +411,8 @@ foo = 'bar' foo = 'fizz' ` - const result = parseWorkflow(workflow, new Set(), {}, {}) + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(), {}, {}) const expected = { global: {}, nodes: [ @@ -553,7 +554,7 @@ key8 = [ id: expect.stringMatching(/\w+/) }] } - const result = parseWorkflow(workflow, new Set(Object.keys(expected.global)), {}, {}) + const result = parseWorkflowByCatalogPieces(tomlstring2table(workflow), new Set(Object.keys(expected.global)), {}, {}) expect(result).toEqual(expected) }) @@ -563,7 +564,7 @@ key8 = [ [[foo]] bar = 'fizz' ` - const result = parseWorkflow(workflow, new Set(['foo']), {}, {}) + const result = parseWorkflowByCatalogPieces(tomlstring2table(workflow), new Set(['foo']), {}, {}) const expected = { global: { foo: [{ @@ -585,8 +586,8 @@ key8 = [ } const tomSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['foo']), tomlSchema4global, tomSchema4nodes @@ -610,8 +611,8 @@ key8 = [ } const tomlSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['foo']), tomlSchema4global, tomlSchema4nodes @@ -643,8 +644,8 @@ key8 = [ } const tomSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['fle']), tomlSchema4global, tomSchema4nodes @@ -693,8 +694,8 @@ key8 = [ } const tomSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['mol']), tomlSchema4global, tomSchema4nodes @@ -737,8 +738,8 @@ key8 = [ } const tomSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['mol']), tomlSchema4global, tomSchema4nodes @@ -774,8 +775,8 @@ key8 = [ } } - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(), tomlSchema4global, tomSchema4nodes @@ -809,8 +810,8 @@ key8 = [ } const tomSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['fle']), tomlSchema4global, tomSchema4nodes @@ -848,8 +849,8 @@ key8 = [ } const tomlSchema4nodes = {} - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(['foo']), tomlSchema4global, tomlSchema4nodes @@ -870,7 +871,7 @@ key8 = [ [[somenode.foo]] bar = 'fizz' ` - const result = parseWorkflow(workflow, new Set(), {}, {}) + const result = parseWorkflowByCatalogPieces(tomlstring2table(workflow), new Set(), {}, {}) const expected = { global: {}, nodes: [{ @@ -899,8 +900,8 @@ key8 = [ } } - const result = parseWorkflow( - workflow, + const result = parseWorkflowByCatalogPieces( + tomlstring2table(workflow), new Set(), tomlSchema4global, tomSchema4nodes diff --git a/packages/core/src/toml.ts b/packages/core/src/toml.ts index 0e4676a..491981a 100644 --- a/packages/core/src/toml.ts +++ b/packages/core/src/toml.ts @@ -3,6 +3,7 @@ import { isObject } from './utils/isObject' import { IWorkflowNode, IParameters, IWorkflow, TomlObjectSchema, ICatalog } from './types' import { mergeHeader, splitHeader } from './dsv' import { nanoid } from 'nanoid' +import { globalParameterKeys } from './catalog' export interface TomlSchemas { nodes: Record @@ -186,9 +187,24 @@ function toml2parameters (tomledParameters: IParameters, tomlSchema: TomlObjectS return parameters } -export function parseWorkflow (workflow: string, globalKeys: Set, tomlSchema4global: TomlObjectSchema, tomSchema4nodes: Record): IWorkflow { +export function parseWorkflow (workflow: string, + catalog: ICatalog): IWorkflow { + const table = tomlstring2table(workflow) + return parseWorkflowFromTable(table, catalog) +} + +export function tomlstring2table (workflow: string): ReturnType { const deduppedWorkflow = dedupWorkflow(workflow) const table = parse(deduppedWorkflow, { bigint: false }) + return table +} + +export function parseWorkflowByCatalogPieces ( + table: ReturnType, + globalKeys: Set, + tomlSchema4global: TomlObjectSchema, + tomSchema4nodes: Record +): IWorkflow { const global: IParameters = {} const nodes: IWorkflowNode[] = [] const sectionwithindex = /\.?\d+$/ @@ -213,6 +229,24 @@ export function parseWorkflow (workflow: string, globalKeys: Set, tomlSc } } +export function parseWorkflowFromTable ( + table: ReturnType, + catalog: ICatalog +): IWorkflow { + const globalKeys = globalParameterKeys(catalog.global) + const tomlSchema4global = catalog.global.tomlSchema ?? {} + const tomSchema4nodes = Object.fromEntries(catalog.nodes.map( + n => [n.id, n.tomlSchema !== undefined ? n.tomlSchema : {}]) + ) + return parseWorkflowByCatalogPieces( + table, + globalKeys, + tomlSchema4global, + tomSchema4nodes + + ) +} + /** * Adds index to every repeated header */ diff --git a/packages/core/src/workflow.ts b/packages/core/src/workflow.ts index 9d415c2..5ace7f0 100644 --- a/packages/core/src/workflow.ts +++ b/packages/core/src/workflow.ts @@ -1,5 +1,4 @@ import { readArchive } from './archive' -import { globalParameterKeys } from './catalog' import { parseWorkflow } from './toml' import { ICatalog, IFiles, IParameters, IWorkflow, IWorkflowNode } from './types' import { walk } from './utils/searchreplace' @@ -11,31 +10,14 @@ export interface ILoadedworkflow extends IWorkflow { export async function loadWorkflowArchive (archiveURL: string, catalog: ICatalog): Promise { const { tomlstring, files } = await readArchive(archiveURL) - const globalKeys = globalParameterKeys(catalog.global) - const tomlSchema4global = catalog.global.tomlSchema ?? {} - const tomSchema4nodes = Object.fromEntries(catalog.nodes.map( - n => [n.id, n.tomlSchema !== undefined ? n.tomlSchema : {}]) - ) - const { nodes, global } = parseWorkflow( - tomlstring, globalKeys, tomlSchema4global, tomSchema4nodes - ) - const errors = await validateWorkflow( - { - global, - nodes - }, { - global: catalog.global, - nodes: catalog.nodes - }, - files - ) + const workflow = parseWorkflow(tomlstring, catalog) + const errors = await validateWorkflow(workflow, catalog, files) if (errors.length > 0) { console.error(errors) throw new ValidationError('Invalid workflow loaded', errors) } return { - global, - nodes, + ...workflow, files } }