From 4269d00838fc55f3ee1daa4e522d0b9acd2ebb8b Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 4 Sep 2019 14:19:52 +0700 Subject: [PATCH 1/3] update process compiler --- src/utils/compiler.ts | 55 +++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/utils/compiler.ts b/src/utils/compiler.ts index 6b06452..1d16169 100644 --- a/src/utils/compiler.ts +++ b/src/utils/compiler.ts @@ -55,15 +55,18 @@ const nodeCompiler = (instanceResolver: (object: any) => Promise) => { key, outputs: Object.keys(def.inputs).map(key => ({ key, - ref: pick(def.inputs[key], ['key', 'nodeKey']) + ref: { + key: def.inputs[key].key, + nodeKey: def.inputs[key].stepKey, + } })) }), filter: async (def: any, key: string): Promise => ({ key, - conditions: Object.keys(def).map(key => ({ + conditions: Object.keys(def.conditions).map(key => ({ key, predicate: 1, // EQ - value: def[key] + value: def.conditions[key] })) }) } @@ -79,32 +82,32 @@ export const process = async (content: Buffer, instanceResolver: (object: any) = let nodes = [] let edges = [] - let trigger = await (definition.trigger.eventKey - ? compileNode('event', definition.trigger, definition.trigger.key) - : compileNode('result', definition.trigger, definition.trigger.key)) - - nodes.push(trigger) - - let previousKey = definition.trigger.key - for (const task of definition.tasks) { - if (task.filter) { - const filterKey = `${previousKey}-filter` - const filterNode = await compileNode('filter', task.filter, filterKey) - nodes.push(filterNode) - edges.push({src: previousKey, dst: filterKey}) - previousKey = filterKey - } - if (task.inputs) { - const mapKey = `${previousKey}-map` - const mapNode = await compileNode('map', task, mapKey) + let previousKey = null + let i = 0 + for (const step of definition.steps) { + step.key = step.key || `node-${i}` + if (step.inputs) { + const mapKey = `${step.key}-inputs` + const mapNode = await compileNode('map', step, mapKey) nodes.push(mapNode) - edges.push({src: previousKey, dst: mapKey}) + if (previousKey) { + edges.push({src: previousKey, dst: mapKey}) + } previousKey = mapKey + i++ + } + const type = step.type !== 'trigger' + ? step.type + : step.eventKey + ? 'event' + : 'result' + const stepNode = await compileNode(type, step, step.key) + nodes.push(stepNode) + if (previousKey) { + edges.push({src: previousKey, dst: step.key}) } - const taskNode = await compileNode('task', task, task.key) - nodes.push(taskNode) - edges.push({src: previousKey, dst: task.key}) - previousKey = task.key + previousKey = step.key + i++ } return { From 0f87fc07504554e9e3e9a68c3e536e50898c1697 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 4 Sep 2019 16:16:50 +0700 Subject: [PATCH 2/3] add options to node compilation - support of instance resolver - support of default node key for reference --- src/utils/compiler.ts | 51 ++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/utils/compiler.ts b/src/utils/compiler.ts index 1d16169..3291e0c 100644 --- a/src/utils/compiler.ts +++ b/src/utils/compiler.ts @@ -33,35 +33,43 @@ export const service = async (content: Buffer): Promise => { } } -const nodeCompiler = (instanceResolver: (object: any) => Promise) => { +const nodeCompiler = async ( + type: 'result' | 'event' | 'task' | 'map' | 'filter', + def: any, + key: string, + opts: { + defaultNodeKey?: string | null, + instanceResolver?(object: any): Promise + } +) => { const nodes = { - result: async (def: any, key: string): Promise => ({ + result: async (def: any, key: string, opts: any): Promise => ({ key, taskKey: def.taskKey, - instanceHash: await instanceResolver(def) + instanceHash: opts.instanceResolver ? await opts.instanceResolver(def) : def.instanceHash }), - event: async (def: any, key: string): Promise => ({ + event: async (def: any, key: string, opts: any): Promise => ({ key, eventKey: def.eventKey, - instanceHash: await instanceResolver(def) + instanceHash: opts.instanceResolver ? await opts.instanceResolver(def) : def.instanceHash }), - task: async (def: any, key: string): Promise => ({ + task: async (def: any, key: string, opts: any): Promise => ({ key, taskKey: def.taskKey, - instanceHash: await instanceResolver(def) + instanceHash: opts.instanceResolver ? await opts.instanceResolver(def) : def.instanceHash }), - map: async (def: any, key: string): Promise => ({ + map: async (def: any, key: string, opts: any): Promise => ({ key, - outputs: Object.keys(def.inputs).map(key => ({ + outputs: Object.keys(def).map(key => ({ key, ref: { - key: def.inputs[key].key, - nodeKey: def.inputs[key].stepKey, + key: def[key].key, + nodeKey: def[key].stepKey || opts.defaultNodeKey, } })) }), - filter: async (def: any, key: string): Promise => ({ + filter: async (def: any, key: string, opts: any): Promise => ({ key, conditions: Object.keys(def.conditions).map(key => ({ key, @@ -70,30 +78,30 @@ const nodeCompiler = (instanceResolver: (object: any) => Promise) => { })) }) } - return async (type: 'result' | 'event' | 'task' | 'map' | 'filter', def: any, key: string) => ({ - [type]: await nodes[type](def, key) - }) + return { + [type]: await nodes[type](def, key, opts) + } } export const process = async (content: Buffer, instanceResolver: (object: any) => Promise): Promise => { const definition = decode(content) - const compileNode = nodeCompiler(instanceResolver) let nodes = [] let edges = [] - let previousKey = null + let previousKey: string | null = null + let previousKeyWithOutputs: string | null = null let i = 0 for (const step of definition.steps) { step.key = step.key || `node-${i}` if (step.inputs) { const mapKey = `${step.key}-inputs` - const mapNode = await compileNode('map', step, mapKey) + const mapNode = await nodeCompiler('map', step.inputs, mapKey, {defaultNodeKey: previousKeyWithOutputs}) nodes.push(mapNode) if (previousKey) { edges.push({src: previousKey, dst: mapKey}) } - previousKey = mapKey + previousKeyWithOutputs = previousKey = mapKey i++ } const type = step.type !== 'trigger' @@ -101,11 +109,14 @@ export const process = async (content: Buffer, instanceResolver: (object: any) = : step.eventKey ? 'event' : 'result' - const stepNode = await compileNode(type, step, step.key) + const stepNode = await nodeCompiler(type, step, step.key, {instanceResolver}) nodes.push(stepNode) if (previousKey) { edges.push({src: previousKey, dst: step.key}) } + if (type !== 'filter') { + previousKeyWithOutputs = step.key + } previousKey = step.key i++ } From a90bce5c055f03fd400bebbf321d85e8dd50a5c8 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Wed, 4 Sep 2019 16:17:02 +0700 Subject: [PATCH 3/3] update mesg library --- package-lock.json | 12 ++++++------ package.json | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3d2b6f5..d9e823b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -162,9 +162,9 @@ } }, "@grpc/proto-loader": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.1.tgz", - "integrity": "sha512-3y0FhacYAwWvyXshH18eDkUI40wT/uGio7MAegzY8lO5+wVsc19+1A7T0pPptae4kl7bdITL+0cHpnAPmryBjQ==", + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.2.tgz", + "integrity": "sha512-eBKD/FPxQoY1x6QONW2nBd54QUEyzcFP9FenujmoeDPy1rutVSHki1s/wR68F6O1QfCNDx+ayBH1O2CVNMzyyw==", "requires": { "lodash.camelcase": "^4.3.0", "protobufjs": "^6.8.6" @@ -4338,9 +4338,9 @@ "dev": true }, "mesg-js": { - "version": "4.3.0-beta.3", - "resolved": "https://registry.npmjs.org/mesg-js/-/mesg-js-4.3.0-beta.3.tgz", - "integrity": "sha512-ZEpL59+eFDQ3rcvxWLk6ejCOsouWP+IditTRMAE0rV5xV1heu+8EKDdXW7d1btMspr5skKqt+WRVPb1g1LJ5Fg==", + "version": "4.3.0-beta.4", + "resolved": "https://registry.npmjs.org/mesg-js/-/mesg-js-4.3.0-beta.4.tgz", + "integrity": "sha512-BXvOt2IRx8pzFvw//FNhzddxEln8bAO6mZbjU6+blKug6QCeeCfek7fxQDX78wlUcbOR/keFpktCtH0uyAV3cg==", "requires": { "@grpc/proto-loader": "^0.5.1", "base-x": "^3.0.6", diff --git a/package.json b/package.json index 8f7e549..9af4c74 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "is-git-url": "^1.0.0", "js-yaml": "^3.13.1", "lodash.pick": "^4.4.0", - "mesg-js": "^4.3.0-beta.3", + "mesg-js": "^4.3.0-beta.4", "node-docker-api": "^1.1.22", "rimraf": "^2.6.3", "tar": "^4.4.8", @@ -113,4 +113,4 @@ "lint": "tslint -p ./ -t stylish --fix" }, "types": "lib/index.d.ts" -} \ No newline at end of file +}