-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapper.js
127 lines (114 loc) · 3.34 KB
/
Mapper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const Map = require('immutable').Map;
const List = require('immutable').List;
const fromJS = require('immutable').fromJS;
const PathNotFound = require('./exceptions').PathNotFound;
class Mapper {
constructor (scheme) {
this.orignal = scheme;
this.scheme = this.analyzescheme(scheme).toJS();
this.errors = [];
}
get error() {
return this.errors;
}
set error(error) {
this.errors.push(error)
}
/**
Creates a scheme used to map the Data in to a Model.
@param scheme JSON
@return analysedscheme
*/
analyzescheme(scheme) {
const Mapscheme = fromJS(scheme);
return Mapscheme.map((v, k) => {
if(typeof v === 'object') {
return Map({
type: 'ARRAY',
model: List(k.split('.')),
values: this.analyzescheme(v),
});
} else {
return Map({
type: 'OBJECT',
model: List(k.split('.')),
values: List(v.split('.'))
});
}
}).toList();
}
/**
interface for JSON-to-JSON Mapping
@param input JSON the data which will be mapped
@return JSON the DataObject wich is genarated
*/
map(input) {
try {
this.validateMap(input);
} catch (e) {
this.error = e.toString()
console.error(e.toString());
}
return this._mapWithImmutable(fromJS(input), this.scheme).toJS();
}
/**
interface for immutable -immutable Mapping
@param input immutable Map an immutable Map which will be mapped
@param scheme immutable JSON the scheme, this is optional
*/
_mapWithImmutable(input, scheme = this.scheme) {
let object = Map();
scheme.forEach(p => {
if(p.type === 'ARRAY') {
object = object.setIn(p.model, this._mapWithImmutable(input, p.values).toList());
return
}
if(p.values.indexOf('[]') > -1) {
const pathBefore = p.values.slice(0, p.values.indexOf('[]')),
pathBehind = p.values.slice(p.values.indexOf('[]') + 1),
size = input.getIn(pathBefore).size;
for(let i = 0; i < size; i++) {
const partialData = input.getIn(pathBefore.concat(i)),
sub = this._mapWithImmutable(partialData, [{
model: [],
values: pathBehind,
type: 'OBJECT'
}]);
if(Map.isMap(sub) && sub.keySeq().get(0) === 0) {
object = object.setIn([i].concat(p.model), sub.toList());
} else {
object = object.setIn([i].concat(p.model), sub);
}
}
return;
}
object = object.setIn(p.model, input.getIn(p.values));
});
return object;
}
validateMap(data, scheme = this.scheme) {
scheme.forEach(p => {
if(typeof p.values[0] === 'string') {
this.validatePath(data, p.values, 1);
} else {
this.validateMap(data, p.values);
}
})
}
validatePath(data, paths, end) {
const map = fromJS(data);
const path = List(paths);
if(paths.length >= end) {
if(path.slice(0, end).last() === '[]') {
this.validatePath(map.getIn(path.slice(0, end - 1)),path.slice(end + 1), 1);
return;
}
if(map.hasIn(path.slice(0, end).toJS())) {
this.validatePath(data, paths, end + 1)
} else {
throw new PathNotFound(path.slice(0, end).toJS(), 'input data');
}
}
}
}
module.exports = Mapper;