-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.js
80 lines (63 loc) · 2.21 KB
/
input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var u = require('util')
var R = require('ramda')
var hashOf = require('./utils/hashOf')
var requireSourceTarget = function (input) {
// must have source and target
if (!(input.source && input.target)) {
throw new Error(u.format("Type '%s' has %s structure and thus requires `source` and `target` fields.", input.type, input.structure))
}
// if we have no id, generate one using hash of s,type,t
return input.id || hashOf([input.source, input.type, input.target])
}
var requireId = function (input) {
if (!input.id) {
throw new Error(u.format("Type '%s' has %s structure and requires the `id` field", input.type, input.structure))
}
return input.id
}
// we define how identifiers are created for each structure here
var ensureIdentifier = {
node: requireId,
arc: requireSourceTarget,
equivalence: requireSourceTarget
}
module.exports = function (types) {
// for faster error reporting
var typeNames = Object.keys(types).join(' ')
// and type to structure name lookup
var structureNames = R.mapObj(R.compose(R.head, Object.keys), types)
return function prepareInput (input) {
// default operation is add
input.operation = input.operation || 'add'
// s, t alias for source/target
input.source = input.source || input.s
input.target = input.target || input.t
// ensure (empty) data field
input.data = input.data || {}
// queries are exceptional!
if (input.query) {
// pretend they are operations on `query` structure
input.structure = 'query'
input.operation = input.query
// and bypass further checks
return input
}
// must pass a dataset
if (!input.dataset) {
throw new Error('Input has to have a dataset field')
}
// for normal documents, everything starts with a type
if (!input.type) {
throw new Error('Input has to have a type field')
}
// it has to be defined in the config
if (!types[input.type]) {
throw new Error(u.format('Unknown type "%s", must be one of: %s', input.type, typeNames))
}
// set structure name
input.structure = structureNames[input.type]
// compute id if missing
input.id = ensureIdentifier[input.structure](input)
return input
}
}