Skip to content

Commit 5b088e5

Browse files
(feature) Check for correct typings.
0 parents  commit 5b088e5

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@buggyorg/typify",
3+
"version": "0.1.0",
4+
"description": "Applies a type scheme to a weakly typed buggy graph.",
5+
"main": "lib/api.js",
6+
"scripts": {
7+
"test": "node node_modules/standard/bin/cmd.js src/**/*.js && node_modules/mocha/bin/mocha --compilers js:babel-register"
8+
},
9+
"author": "Maximilian Klein",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"babel-cli": "^6.7.5",
13+
"babel-preset-es2015": "^6.6.0",
14+
"babel-register": "^6.7.2",
15+
"chai": "^3.5.0",
16+
"mocha": "^2.4.5",
17+
"standard": "^6.0.8"
18+
},
19+
"dependencies": {
20+
"@buggyorg/graphtools": "^0.2.6",
21+
"graphlib": "^2.1.0",
22+
"lodash": "^4.11.1"
23+
}
24+
}

src/api.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import {utils} from '@buggyorg/graphtools'
3+
import _ from 'lodash'
4+
5+
export function validateTypings (typings) {
6+
return !!typings && !!typings.number &&
7+
!!typings.string &&
8+
!!typings.bool
9+
}
10+
11+
function retypePorts (ports, typings) {
12+
return _.mapValues(ports, (p) => {
13+
if (typings[p]) {
14+
return typings[p]
15+
} else {
16+
return p
17+
}
18+
})
19+
}
20+
21+
function retypeNode (node, typings) {
22+
return _.merge({}, node, {
23+
inputPorts: retypePorts(node.inputPorts, typings),
24+
outputPorts: retypePorts(node.outputPorts, typings)
25+
})
26+
}
27+
28+
export function applyTypings (graph, typings) {
29+
if (!validateTypings(typings)) throw new Error('Cannot apply invalid typings')
30+
var editGraph = utils.edit(graph)
31+
editGraph.nodes = _.map(editGraph.nodes, (n) => {
32+
return _.merge({}, n, {value: retypeNode(n.value, typings)})
33+
})
34+
return utils.finalize(editGraph)
35+
}

test/fixtures/add.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"options": {
3+
"directed": true,
4+
"multigraph": true,
5+
"compound": true
6+
},
7+
"nodes": [
8+
{
9+
"v": "a",
10+
"value": {
11+
"id": "math/add",
12+
"version": "0.1.1",
13+
"inputPorts": {
14+
"s1": "number",
15+
"s2": "bool"
16+
},
17+
"outputPorts": {
18+
"sum": "string"
19+
},
20+
"atomic": true
21+
}
22+
}
23+
],
24+
"edges" : []
25+
}

test/typings.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* global describe, it */
2+
import chai from 'chai'
3+
import fs from 'fs'
4+
import graphlib from 'graphlib'
5+
import * as api from '../src/api'
6+
7+
var expect = chai.expect
8+
9+
describe('Typgins', () => {
10+
11+
it('can validate typings', () => {
12+
expect(api.validateTypings({number: 'int', bool: 'bool', string: 'string'})).to.be.true
13+
expect(api.validateTypings({bool: 'bool', string: 'string'})).to.be.false
14+
expect(api.validateTypings({int: 'int', bool: 'bool', string: 'string'})).to.be.false
15+
expect(api.validateTypings({number: 'int', bool: null, string: 'string'})).to.be.false
16+
})
17+
18+
it('converts every generic port-type into a concrete one', () => {
19+
var graph = graphlib.json.read(JSON.parse(fs.readFileSync('test/fixtures/add.json', 'utf8')))
20+
var newGraph = api.applyTypings(graph, {number: 'a', bool: 'b', string: 'c'})
21+
expect(newGraph.node('a').inputPorts['s1']).to.equal('a')
22+
expect(newGraph.node('a').inputPorts['s2']).to.equal('b')
23+
expect(newGraph.node('a').outputPorts['sum']).to.equal('c')
24+
})
25+
26+
it('fails to apply invalid typings', () => {
27+
expect(() => api.applyTypings({}, {numba: 'a', bool: 'b', string: 'c'}))
28+
.to.throw(Error)
29+
})
30+
})

0 commit comments

Comments
 (0)