-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtl.js
266 lines (224 loc) · 5.67 KB
/
vtl.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const velocity = require('velocityjs');
const assert = require('assert');
const log = require('logdown')('appsync-emulator:vtl');
const javaify = value => {
// eslint-disable-next-line
if (value instanceof JavaMap) return value;
// eslint-disable-next-line
if (Array.isArray(value) && !(value instanceof JavaArray)) {
// eslint-disable-next-line
return new JavaArray(value.map(x => javaify(x)));
}
if (
value != null &&
typeof value === 'object' &&
value.constructor === Object
) {
// eslint-disable-next-line
return createMapProxy(
// eslint-disable-next-line
new JavaMap(
Object.entries(value).reduce(
(sum, [k, v]) => ({
...sum,
[k]: javaify(v),
}),
{},
),
),
);
}
// eslint-disable-next-line
if (typeof value === 'string' && !(value instanceof JavaString)) {
// eslint-disable-next-line
return new JavaString(value);
}
// for now we don't handle number.
return value;
};
const toJSON = value => {
if (typeof value === 'object' && value != null && 'toJSON' in value) {
return value.toJSON();
}
return value;
};
class JavaString {
constructor(str) {
this.strVal = str;
}
replaceAll(find, replace) {
const rep = this.strVal.replace(new RegExp(find, 'g'), replace);
return new JavaString(rep);
}
split(regexString, limit = undefined) {
// WARNING: this assumes Java and JavaScript regular expressions are identical, according to
// https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines#Language_features
// this should be the case except for look-behind which is not implemented in JavaScript
// java.util.String.split does not to include the separatpr in the result. JS does splice any capturing group
// in the regex into the result. To remove the groups from the result we need the count of capturing groups in
// the provided regex, the only way in JS seems to be via a match to an empty string
const testRe = new RegExp(
`${typeof regexString === 'object' ? regexString.strVal : regexString}|`,
);
const ngroups = ''.match(testRe).length; // actually num of groups plus one, ie "" and the (empty) groups
const re = new RegExp(
typeof regexString === 'object' ? regexString.strVal : regexString,
);
const result = this.strVal.split(re, limit);
return result
.filter((v, ii) => !(ii % ngroups))
.map(v => new JavaString(v));
}
toJSON() {
return this.toString();
}
toString() {
return this.strVal;
}
}
class JavaArray extends Array {
// required so array starts with zero elements
// eslint-disable-next-line
constructor(values = []) {
super();
values.forEach(value => this.add(value));
}
add(value) {
this.push(javaify(value));
return value;
}
addAll(value) {
const self = this;
value.forEach(val => self.push(javaify(val)));
}
clear() {
this.length = 0;
}
contains(value) {
return this.indexOf(value) !== -1;
}
containsAll(value = []) {
const self = this;
return value.every(v => self.contains(v));
}
isEmpty() {
return this.length === 0;
}
remove(value) {
const idx = this.indexOf(value);
if (idx === -1) return;
this.splice(idx, 1);
}
removeAll(value) {
const self = this;
value.forEach(val => self.remove(val));
}
// eslint-disable-next-line
retainAll() {
throw new Error('no support for retain all');
}
size() {
return this.length;
}
toJSON() {
return Array.from(this).map(toJSON);
}
}
const createMapProxy = map =>
new Proxy(map, {
get(obj, prop) {
if (map.map.has(prop)) {
return map.get(prop);
}
return map[prop];
},
});
class JavaMap {
constructor(obj) {
this.map = new Map();
this.toJSON = this.toJSON.bind(this);
Object.entries(obj).forEach(([key, value]) => {
this.map.set(key, value);
});
}
clear() {
this.map.clear();
}
containsKey(key) {
return this.map.has(key);
}
containsValue(value) {
return this.map.values().indexOf(value) !== -1;
}
entrySet() {
const entries = Array.from(this.map.entries()).map(([key, value]) =>
createMapProxy(
new JavaMap({
key,
value,
}),
),
);
return new JavaArray(entries);
}
equals(value) {
assert(value instanceof JavaMap);
return this.map.entries.every(([key, v]) => value.get(key) === v);
}
get(key) {
if (this.map.has(key.toString())) {
return this.map.get(key);
}
return null;
}
isEmpty() {
return this.map.size === 0;
}
keySet() {
return new JavaArray(Array.from(this.map.keys()));
}
put(key, value) {
const saveValue = javaify(value);
this.map.set(key, saveValue);
return saveValue;
}
putAll(map) {
assert(map instanceof JavaMap);
Array.from(map.map.entries()).forEach(([key, value]) => {
this.put(key, value);
});
}
remove(key) {
if (!this.map.has(key)) {
return null;
}
const value = this.map.get(key);
this.map.delete(key);
return value;
}
size() {
return this.map.size;
}
values() {
return new JavaArray(Array.from(this.map.values()));
}
toJSON() {
return Array.from(this.map.entries()).reduce(
(sum, [key, value]) => ({
...sum,
[key]: toJSON(value),
}),
{},
);
}
}
const vtl = (str, context, macros = {}) => {
log.info('render\n', str);
const output = velocity.render(str, context, macros, {
valueMapper: javaify,
escape: false,
});
log.info('render output\n', output);
return output;
};
module.exports = { vtl, javaify, toJSON };