forked from dashjoin/jsonata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignature.java
More file actions
524 lines (495 loc) · 22.2 KB
/
Signature.java
File metadata and controls
524 lines (495 loc) · 22.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* jsonata-java is the JSONata Java reference port
*
* Copyright Dashjoin GmbH. https://dashjoin.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Derived from original JSONata4Java Signature code under this license:
/*
* (c) Copyright 2018, 2019 IBM Corporation
* 1 New Orchard Road,
* Armonk, New York, 10504-1722
* United States
* +1 914 499 1900
* support: Nathaniel Mills wnm3@us.ibm.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.dashjoin.jsonata.utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.lang.model.type.NullType;
import com.dashjoin.jsonata.Functions;
import com.dashjoin.jsonata.JException;
import com.dashjoin.jsonata.Utils;
/**
* Manages signature related functions
*/
public class Signature implements Serializable {
private static final long serialVersionUID = -450755246855587271L;
static class Param {
String type;
String regex;
boolean context;
boolean array;
String subtype;
String contextRegex;
@Override
public String toString() {
return "Param "+type+" regex="+regex+" ctx="+context+" array="+array;
}
}
Param _param = new Param();
List<Param> _params = new ArrayList<>();
Param _prevParam = _param;
Pattern _regex = null;
String _signature = "";
String functionName;
public Signature(String signature, String function) {
this.functionName = function;
parseSignature(signature);
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public static void main(String[] args) {
Signature s = new Signature("<s-:s>", "test");//<s-(sf)(sf)n?:s>");
System.out.println(s._params);
}
int findClosingBracket(String str, int start, char openSymbol, char closeSymbol) {
// returns the position of the closing symbol (e.g. bracket) in a string
// that balances the opening symbol at position start
int depth = 1;
int position = start;
while (position < str.length()) {
position++;
char symbol = str.charAt(position);
if (symbol == closeSymbol) {
depth--;
if (depth == 0) {
// we're done
break; // out of while loop
}
} else if (symbol == openSymbol) {
depth++;
}
}
return position;
};
String getSymbol(Object value) {
String symbol;
if (value == null) {
symbol = "m";
} else {
// first check to see if this is a function
if (Utils.isFunction(value) || Functions.isLambda(value) || (value instanceof Pattern)) { //} instanceof JFunction || value instanceof Function) {
symbol = "f";
} else if (value instanceof String)
symbol = "s";
else if (value instanceof Number)
symbol = "n";
else if (value instanceof Boolean)
symbol = "b";
else if (value instanceof List)
symbol = "a";
else if (value instanceof Map)
symbol = "o";
else if (value instanceof NullType) // Uli: is this used???
symbol = "l";
else
// any value can be undefined, but should be allowed to match
symbol = "m"; // m for missing
}
return symbol;
};
void next() {
_params.add(_param);
_prevParam = _param;
_param = new Param();
}
/**
* Parses a function signature definition and returns a validation function
*
* @param {string}
* signature - the signature between the <angle brackets>
* @returns validation pattern
*/
Pattern parseSignature(String signature) {
// create a Regex that represents this signature and return a function that when
// invoked,
// returns the validated (possibly fixed-up) arguments, or throws a validation
// error
// step through the signature, one symbol at a time
int position = 1;
while (position < signature.length()) {
char symbol = signature.charAt(position);
if (symbol == ':') {
// TODO figure out what to do with the return type
// ignore it for now
break;
}
switch (symbol) {
case 's': // string
case 'n': // number
case 'b': // boolean
case 'l': // not so sure about expecting null?
case 'o': { // object
_param.regex = ( "[" + symbol + "m]");
_param.type = ( ""+symbol);
next();
break;
}
case 'a': { // array
// normally treat any value as singleton array
_param.regex = ( "[asnblfom]");
_param.type = ( ""+symbol);
_param.array = ( true);
next();
break;
}
case 'f': { // function
_param.regex = ( "f");
_param.type = ( ""+symbol);
next();
break;
}
case 'j': { // any JSON type
_param.regex = ( "[asnblom]");
_param.type = ( ""+symbol);
next();
break;
}
case 'x': { // any type
_param.regex = ( "[asnblfom]");
_param.type = ( ""+symbol);
next();
break;
}
case '-': { // use context if _param not supplied
_prevParam.context = true;
_prevParam.regex += "?";
break;
}
case '?': // optional _param
case '+': { // one or more
_prevParam.regex += symbol;
break;
}
case '(': { // choice of types
// search forward for matching ')'
int endParen = findClosingBracket(signature, position, '(', ')');
String choice = signature.substring(position + 1, endParen);
if (choice.indexOf("<") == -1) {
// no _parameterized types, simple regex
_param.regex = ( "[" + choice + "m]");
} else {
// TODO harder
throw new RuntimeException("Choice groups containing parameterized types are not supported");
}
_param.type = ( "(" + choice + ")");
position = endParen;
next();
break;
}
case '<': { // type _parameter - can only be applied to 'a' and 'f'
String test = _prevParam.type;
if (test != null) {
String type = test;//.asText();
if (type.equals("a") || type.equals("f")) {
// search forward for matching '>'
int endPos = findClosingBracket(signature, position, '<', '>');
_prevParam.subtype = signature.substring(position + 1, endPos);
position = endPos;
} else {
throw new RuntimeException("Type parameters can only be applied to functions and arrays");
}
} else {
throw new RuntimeException("Type parameters can only be applied to functions and arrays");
}
break;
}
}
position++;
} // end while processing symbols in signature
String regexStr = "^";
for (Param param : _params) {
regexStr += "(" + param.regex + ")";
}
regexStr += "$";
_regex = null;
try {
_regex = Pattern.compile(regexStr);
_signature = regexStr;
} catch (PatternSyntaxException pse) {
throw new RuntimeException(pse.getLocalizedMessage());
}
return _regex;
}
void throwValidationError(List<?> badArgs, String badSig, String functionName) {
// to figure out where this went wrong we need apply each component of the
// regex to each argument until we get to the one that fails to match
String partialPattern = "^";
int goodTo = 0;
for (int index = 0; index < _params.size(); index++) {
partialPattern += _params.get(index).regex;
Pattern tester = Pattern.compile(partialPattern);
Matcher match = tester.matcher(badSig);
if (match.matches() == false) {
// failed here
throw new JException("T0410", -1, (goodTo+1), functionName);
}
goodTo = match.end();
}
// if it got this far, it's probably because of extraneous arguments (we
// haven't added the trailing '$' in the regex yet.
throw new JException("T0410", -1, (goodTo+1), functionName);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public Object validate(Object _args, Object context) {
var result = new ArrayList<>();
var args = (List)_args;
String suppliedSig = "";
for (Object arg : args)
suppliedSig += getSymbol(arg);
Matcher isValid = _regex.matcher(suppliedSig);
if (isValid != null && isValid.matches()) {
var validatedArgs = new ArrayList<>();
var argIndex = 0;
int index = 0;
for (Object _param : _params) {
Param param = (Param)_param;
var arg = argIndex<args.size() ? args.get(argIndex) : null;
String match = isValid.group(index + 1);
if ("".equals(match)) {
if (param.context && param.regex!=null) {
// substitute context value for missing arg
// first check that the context value is the right type
var contextType = getSymbol(context);
// test contextType against the regex for this arg (without the trailing ?)
if (Pattern.matches(param.regex, contextType)) {
//if (param.contextRegex.test(contextType)) {
validatedArgs.add(context);
} else {
// context value not compatible with this argument
throw new JException("T0411",-1,
context,
argIndex + 1
);
}
} else {
validatedArgs.add(arg);
argIndex++;
}
} else {
// may have matched multiple args (if the regex ends with a '+'
// split into single tokens
String[] singles = match.split("");
for (String single : singles) {
//match.split('').forEach(function (single) {
if (param.type.equals("a")) {
if (single.equals("m")) {
// missing (undefined)
arg = null;
} else {
arg = argIndex <args.size() ? args.get(argIndex) : null;
var arrayOK = true;
// is there type information on the contents of the array?
if (param.subtype != null) {
if (!single.equals("a") && !match.equals(param.subtype)) {
arrayOK = false;
} else if (single.equals("a")) {
List argArr = (List)arg;
if (argArr.size() > 0) {
var itemType = getSymbol(argArr.get(0));
if (!itemType.equals(""+param.subtype.charAt(0))) { // TODO recurse further
arrayOK = false;
} else {
// make sure every item in the array is this type
for (Object o : argArr) {
if (!getSymbol(o).equals(itemType)) {
arrayOK = false;
break;
}
}
}
}
}
}
if (!arrayOK) {
throw new JException("T0412", -1,
arg,
//argIndex + 1,
param.subtype//arraySignatureMapping[param.subtype]
);
}
// the function expects an array. If it's not one, make it so
if (!single.equals("a")) {
List _arg = new ArrayList<>(); _arg.add(arg);
arg = _arg;
}
}
validatedArgs.add(arg);
argIndex++;
} else {
arg = argIndex<args.size() ? args.get(argIndex) : null;
validatedArgs.add(arg);
argIndex++;
}
}
}
index++;
}
return validatedArgs;
}
throwValidationError(args, suppliedSig, functionName);
return null; // dead code -> compiler happy
}
public int getNumberOfArgs() {
return _params.size();
}
/**
* Returns the minimum # of arguments.
* I.e. the # of all non-optional arguments.
*/
public int getMinNumberOfArgs() {
int res = 0;
for (Param p : _params)
if (!p.regex.contains("?"))
res++;
return res;
}
/*
ArrayNode validate(String functionName, ExprListContext args, ExpressionsVisitor expressionVisitor) {
ArrayNode result = JsonNodeFactory.instance.arrayNode();
String suppliedSig = "";
for (Iterator<ExprContext> it = args.expr().iterator(); it.hasNext();) {
ExprContext arg = it.next();
suppliedSig += getSymbol(arg);
}
Matcher isValid = _regex.matcher(suppliedSig);
if (isValid != null) {
ArrayNode validatedArgs = JsonNodeFactory.instance.arrayNode();
int argIndex = 0;
int index = 0;
for (Iterator<JsonNode> it = _params.iterator(); it.hasNext();) {
ObjectNode param = (ObjectNode) it.next();
JsonNode arg = expressionVisitor.visit(args.expr(argIndex));
String match = isValid.group(index + 1);
if ("".equals(match)) {
boolean useContext = (param.get("context") != null && param.get("context").asBoolean());
if (useContext) {
// substitute context value for missing arg
// first check that the context value is the right type
JsonNode context = expressionVisitor.getVariable("$");
String contextType = getSymbol(context);
// test contextType against the regex for this arg (without the trailing ?)
if (Pattern.matches(param.get("regex").asText(), contextType)) {
validatedArgs.add(context);
} else {
// context value not compatible with this argument
throw new EvaluateRuntimeException("Context value is not a compatible type with argument \""
+ argIndex + 1 + "\" of function \"" + functionName + "\"");
}
} else {
validatedArgs.add(arg);
argIndex++;
}
} else {
// may have matched multiple args (if the regex ends with a '+'
// split into single tokens
String[] singles = match.split("");
for (String single : singles) {
if ("a".equals(param.get("type").asText())) {
if ("m".equals(single)) {
// missing (undefined)
arg = null;
} else {
arg = expressionVisitor.visit(args.expr(argIndex));
boolean arrayOK = true;
// is there type information on the contents of the array?
String subtype = "undefined";
JsonNode testSubType = param.get("subtype");
if (testSubType != null) {
subtype = testSubType.asText();
if ("a".equals(single) == false && match.equals(subtype) == false) {
arrayOK = false;
} else if ("a".equals(single)) {
ArrayNode argArray = (ArrayNode) arg;
if (argArray.size() > 0) {
String itemType = getSymbol(argArray.get(0));
if (itemType.equals(subtype) == false) { // TODO recurse further
arrayOK = false;
} else {
// make sure every item in the array is this type
ArrayNode differentItems = JsonNodeFactory.instance.arrayNode();
for (Object val : argArray) {
if (itemType.equals(getSymbol(val)) == false) {
differentItems.add(expressionVisitor.visit((ExprListContext) val));
}
}
;
arrayOK = (differentItems.size() == 0);
}
}
}
}
if (!arrayOK) {
JsonNode type = s_arraySignatureMapping.get(subtype);
if (type == null) {
type = JsonNodeFactory.instance.nullNode();
}
throw new EvaluateRuntimeException("Argument \"" + (argIndex + 1) + "\" of function \""
+ functionName + "\" must be an array of \"" + type.asText() + "\"");
}
// the function expects an array. If it's not one, make it so
if ("a".equals(single) == false) {
ArrayNode wrappedArg = JsonNodeFactory.instance.arrayNode();
wrappedArg.add(arg);
arg = wrappedArg;
}
}
validatedArgs.add(arg);
argIndex++;
} else {
validatedArgs.add(arg);
argIndex++;
}
}
}
index++;
}
return validatedArgs;
}
throwValidationError(args, suppliedSig, functionName);
// below just for the compiler as a runtime exception is thrown above
return result;
};
*/
}