Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more workflow parsing and validation methods #135

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
51 changes: 26 additions & 25 deletions packages/core/src/toml.test.ts
Original file line number Diff line number Diff line change
@@ -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()', () => {
Expand Down Expand Up @@ -373,7 +373,7 @@ param_C = 33
})
})

describe('parseWorkflow()', () => {
describe('parseWorkflowByCatalogPieces()', () => {
it('should divide global and module parameters', () => {
const workflow = `
myglobalvar = 'something'
Expand All @@ -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'
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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)
})

Expand All @@ -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: [{
Expand All @@ -585,8 +586,8 @@ key8 = [
}
const tomSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['foo']),
tomlSchema4global,
tomSchema4nodes
Expand All @@ -610,8 +611,8 @@ key8 = [
}
const tomlSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['foo']),
tomlSchema4global,
tomlSchema4nodes
Expand Down Expand Up @@ -643,8 +644,8 @@ key8 = [
}
const tomSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['fle']),
tomlSchema4global,
tomSchema4nodes
Expand Down Expand Up @@ -693,8 +694,8 @@ key8 = [
}
const tomSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['mol']),
tomlSchema4global,
tomSchema4nodes
Expand Down Expand Up @@ -737,8 +738,8 @@ key8 = [
}
const tomSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['mol']),
tomlSchema4global,
tomSchema4nodes
Expand Down Expand Up @@ -774,8 +775,8 @@ key8 = [
}
}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(),
tomlSchema4global,
tomSchema4nodes
Expand Down Expand Up @@ -809,8 +810,8 @@ key8 = [
}
const tomSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['fle']),
tomlSchema4global,
tomSchema4nodes
Expand Down Expand Up @@ -848,8 +849,8 @@ key8 = [
}
const tomlSchema4nodes = {}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(['foo']),
tomlSchema4global,
tomlSchema4nodes
Expand All @@ -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: [{
Expand Down Expand Up @@ -899,8 +900,8 @@ key8 = [
}
}

const result = parseWorkflow(
workflow,
const result = parseWorkflowByCatalogPieces(
tomlstring2table(workflow),
new Set(),
tomlSchema4global,
tomSchema4nodes
Expand Down
36 changes: 35 additions & 1 deletion packages/core/src/toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TomlObjectSchema>
Expand Down Expand Up @@ -186,9 +187,24 @@ function toml2parameters (tomledParameters: IParameters, tomlSchema: TomlObjectS
return parameters
}

export function parseWorkflow (workflow: string, globalKeys: Set<string>, tomlSchema4global: TomlObjectSchema, tomSchema4nodes: Record<string, TomlObjectSchema>): IWorkflow {
export function parseWorkflow (workflow: string,
catalog: ICatalog): IWorkflow {
const table = tomlstring2table(workflow)
return parseWorkflowFromTable(table, catalog)
}

export function tomlstring2table (workflow: string): ReturnType<typeof parse> {
const deduppedWorkflow = dedupWorkflow(workflow)
const table = parse(deduppedWorkflow, { bigint: false })
return table
}

export function parseWorkflowByCatalogPieces (
table: ReturnType<typeof parse>,
globalKeys: Set<string>,
tomlSchema4global: TomlObjectSchema,
tomSchema4nodes: Record<string, TomlObjectSchema>
): IWorkflow {
const global: IParameters = {}
const nodes: IWorkflowNode[] = []
const sectionwithindex = /\.?\d+$/
Expand All @@ -213,6 +229,24 @@ export function parseWorkflow (workflow: string, globalKeys: Set<string>, tomlSc
}
}

export function parseWorkflowFromTable (
table: ReturnType<typeof parse>,
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
*/
Expand Down
24 changes: 3 additions & 21 deletions packages/core/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,31 +10,14 @@ export interface ILoadedworkflow extends IWorkflow {

export async function loadWorkflowArchive (archiveURL: string, catalog: ICatalog): Promise<ILoadedworkflow> {
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
}
}
Expand Down
Loading