@genesislcap/mock-server 15.72.0 → 15.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -5
- package/dist/db/audit.d.ts +12 -0
- package/dist/db/audit.js +98 -0
- package/dist/db/criteriaDialect.js +15 -8
- package/dist/db/criteriaJuxtaposition.d.ts +6 -0
- package/dist/db/criteriaJuxtaposition.js +123 -0
- package/dist/db/groovySyntax.js +4 -1
- package/dist/db/groovyValidator.js +166 -7
- package/dist/db/schema.d.ts +2 -0
- package/dist/db/schema.js +2 -0
- package/dist/db/store.d.ts +14 -2
- package/dist/db/store.js +64 -4
- package/dist/handlers/commitEvent.js +16 -3
- package/dist/handlers/dataLogon.js +8 -4
- package/dist/handlers/jsonSchema.js +1 -1
- package/dist/handlers/meta.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/protocol/fieldTypes.d.ts +5 -1
- package/dist/protocol/fieldTypes.js +9 -2
- package/dist/protocol/msgNack.d.ts +2 -0
- package/dist/protocol/msgNack.js +14 -0
- package/dist/protocol/router.js +12 -1
- package/dist/server.js +21 -2
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
- package/src/db/audit.ts +127 -0
- package/src/db/criteriaDialect.ts +15 -8
- package/src/db/criteriaJuxtaposition.ts +132 -0
- package/src/db/groovySyntax.ts +6 -1
- package/src/db/groovyValidator.ts +176 -11
- package/src/db/schema.ts +4 -0
- package/src/db/store.ts +70 -4
- package/src/handlers/commitEvent.ts +18 -3
- package/src/handlers/dataLogon.ts +8 -4
- package/src/handlers/jsonSchema.ts +1 -1
- package/src/handlers/meta.ts +1 -1
- package/src/index.ts +1 -0
- package/src/protocol/fieldTypes.ts +13 -2
- package/src/protocol/msgNack.ts +16 -0
- package/src/protocol/router.ts +14 -1
- package/src/server.ts +26 -2
- package/src/types.ts +17 -0
package/README.md
CHANGED
|
@@ -498,6 +498,42 @@ warns): `test/commitSemantics.test.ts` follows the Kotlin.
|
|
|
498
498
|
writes stay whatever it replies), warnings always NACK (in `ERROR`), and
|
|
499
499
|
`ctx.ignoreWarnings` is always `false`.
|
|
500
500
|
|
|
501
|
+
## Audit tables (`TableDef.audit`)
|
|
502
|
+
|
|
503
|
+
A table declared with `audit = details(id = …, sequence = "TA")` in the
|
|
504
|
+
dictionary gets a `<TABLE>_AUDIT` table from GSF (genesis-dictionary
|
|
505
|
+
`Audit.kt`, v8.15.29). `audit: true` (or `{ sequencePrefix: 'TA' }`; the
|
|
506
|
+
default sequence is `AU`) does the same here:
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
tables: {
|
|
510
|
+
TRADE: { pkField: 'TRADE_ID', fields: [/* … */], events: ['INSERT', 'MODIFY', 'DELETE'],
|
|
511
|
+
audit: { sequencePrefix: 'TA' } },
|
|
512
|
+
},
|
|
513
|
+
// What Genesis Create generates: requestReply(TRADE_AUDIT) { request(TRADE_AUDIT.BY_UNDERLYING_ID) }
|
|
514
|
+
requestReplies: { TRADE_AUDIT: { source: 'TRADE_AUDIT', requestFields: ['TRADE_ID'] } },
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
- **The table** has the audited table's fields, its own key `<TABLE>_AUDIT_ID`
|
|
518
|
+
from the audit sequence, and `AUDIT_EVENT_TYPE`, `AUDIT_EVENT_DATETIME`,
|
|
519
|
+
`AUDIT_EVENT_TEXT` and `AUDIT_EVENT_USER`. It is a table like any other: a
|
|
520
|
+
query or request server can read it, and `setTable` makes a new, empty one
|
|
521
|
+
for a new definition, as the table's rows are its new ones. GSF serves no
|
|
522
|
+
resource for it by itself. A table declared under that name, at start or
|
|
523
|
+
with `setTable`, is refused, as GSF's dictionary refuses two tables of one
|
|
524
|
+
name. Like any sequence, the audit one is shared by every table on its
|
|
525
|
+
prefix: a table registered, or set again, starts after the values the other
|
|
526
|
+
tables on it have used, so no id repeats across them, while its own count
|
|
527
|
+
restarts from its seed rows (under `fidelity: 'legacy'` only from those).
|
|
528
|
+
- **Every write an event handler makes** to the audited table adds a row: the
|
|
529
|
+
record as written (as it was, for a delete), the event's `MESSAGE_TYPE`, the
|
|
530
|
+
time, its `REASON` (else `null`) and the user, as GSF's event handlers write
|
|
531
|
+
through `entityDb.audited(...)`. The rows commit and roll back with the
|
|
532
|
+
event, and go out live to its queries after the `EVENT_ACK`. A write made
|
|
533
|
+
outside an event (a seed row, `server.store` from a test) is not audited,
|
|
534
|
+
nor is anything under `fidelity: 'legacy'`, which runs events without a
|
|
535
|
+
transaction.
|
|
536
|
+
|
|
501
537
|
## Running several servers in parallel (and one-command dev stacks)
|
|
502
538
|
|
|
503
539
|
Every `createMockServer()` instance is fully isolated — its own store,
|
|
@@ -951,6 +987,7 @@ createMockServer({ ...config, fidelity: 'legacy' });
|
|
|
951
987
|
| A `CRITERIA_MATCH` that isn't valid Groovy, is in this engine's `AND`/`OR`/`NOT`/`<>`/`=` dialect, calls a function GSF's validator doesn't allow or one no overload takes | `LOGON_NACK INVALID_CRITERIA` with GSF's message, no subscription ([Strict criteria](#strict-criteria)) | Malformed or unknown: fails open, every row, logged. The dialect and the refused functions: evaluated |
|
|
952
988
|
| The criteria functions | GSF's, typed by the declared fields ([Criteria functions](#criteria-functions)) | The same, a date compared with also read as ISO-8601 |
|
|
953
989
|
| `REQ_` | As in [Request servers](#request-servers-req_): `PARAMETRIC_TYPE`, criteria-only paging and ordering, `REQUEST` wildcards, ranges and arrays, a reply block, `MSG_NACK` failures | Every matching row in insertion order; `REQUEST` from the top level or `DETAILS`, `'*'`/`null`/`''` match anything, an array value any of its values; `CRITERIA_MATCH` fails open; no `RECORD_ID`/`TIMESTAMP`; errors as a `REP_` with `ERROR` |
|
|
990
|
+
| A message with no `SOURCE_REF` or `MESSAGE_TYPE` (or a blank one) | `MSG_NACK` 400: "No source ref provided" on an empty `SOURCE_REF`, else "No message type provided" | Routed like any other message |
|
|
954
991
|
| Unknown resource | `MSG_NACK` 404 (the client's promise rejects) | A typed reply with `ERROR: [{CODE: 'UNKNOWN_RESOURCE'}]` (the promise resolves) |
|
|
955
992
|
| Login ack | auth 8.15.5's ([Two auth transports](#two-auth-transports)): `HEARTBEAT_INTERVAL_SECONDS`, `UPDATE_QUEUE_DETAILS`, `USER_DETAILS`, `USER: null`, a `SOURCE_REF` on HTTP replies; `event-login-details` answers with the session's own tokens | `HEARTBEAT_INTERVAL_SECS` (default 30), top-level `UPDATE_QUEUE`/`ZERO_MQ_*`, `USER` = `AuthUser.user`, one `PRODUCT` named by `productName`; new tokens on every `event-login-details` |
|
|
956
993
|
| Failed login | `EVENT_LOGIN_AUTH_NACK` with a `LoginError`; HTTP 200 | HTTP 401; over WS `STATUS_CODE: '401'`, "Incorrect username or password" |
|
|
@@ -998,7 +1035,7 @@ eventSchemas: { EVENT_TRADE_BOOK: { className: 'com.acme.BookInput', fields: [/*
|
|
|
998
1035
|
appName: 'showcase', // enum class names: global.genesis.gen.dao.enums.<appName>.<table>.<Field>
|
|
999
1036
|
```
|
|
1000
1037
|
|
|
1001
|
-
- **`FieldDef`** `{name, type, nullable?, values?, default?, maxSize?, minLength?, scale?, description?, title?, optional?}`.
|
|
1038
|
+
- **`FieldDef`** `{name, type, nullable?, values?, default?, maxSize?, minLength?, min?, max?, scale?, description?, title?, optional?}`.
|
|
1002
1039
|
`type` is a dictionary type (`STRING ENUM INT SHORT LONG DOUBLE BIGDECIMAL BOOLEAN DATE
|
|
1003
1040
|
DATETIME NANO_TIMESTAMP RAW`). Fields are nullable unless declared otherwise; key and
|
|
1004
1041
|
generated fields default to not null. `description` replaces the JVM class name the real
|
|
@@ -1006,7 +1043,10 @@ appName: 'showcase', // enum class names: global.genesis.gen.dao.enums.<appName>
|
|
|
1006
1043
|
`org.joda.time.DateTime`). `optional` decides whether an event payload may leave the field
|
|
1007
1044
|
out (see events below); an ENUM with no `values` sends no `VALID_VALUES` and no `enum`.
|
|
1008
1045
|
`scale` is a BIGDECIMAL column's (default 5); values are held and sent by their declared
|
|
1009
|
-
type, see [Value encoding](#value-encoding).
|
|
1046
|
+
type, see [Value encoding](#value-encoding). `min` / `max` are an INT, SHORT, LONG or DOUBLE
|
|
1047
|
+
column's `metadata { min = …; max = … }`: they replace the type's own range in event META
|
|
1048
|
+
(`MIN` / `MAX`) and the event's JSON schema (`minimum` / `maximum`), so an inbound event
|
|
1049
|
+
outside them gets GSF's `VALIDATION_ERROR`.
|
|
1010
1050
|
- **Views** take their base table's fields and `select` aliases, each join's `select` columns
|
|
1011
1051
|
typed from the joined table at any depth (nullable unless reached from the base through INNER
|
|
1012
1052
|
joins only; see [Views: the join model](#views-the-join-model)), and `derivedTypes` for
|
|
@@ -1158,9 +1198,11 @@ v8.15.29: a `DateTime` as epoch millis, a `BigDecimal` as
|
|
|
1158
1198
|
`SOURCE_REF`. foundation-comms' `createMoreRowsMessage` sends
|
|
1159
1199
|
`{MESSAGE_TYPE: 'MORE_ROWS', SOURCE_REF: <new>, DETAILS: {SOURCE_REF: <stream>, VIEW_NUMBER}}`
|
|
1160
1200
|
and doesn't wait for a reply.
|
|
1161
|
-
- When `DETAILS.SOURCE_REF`
|
|
1162
|
-
request's
|
|
1163
|
-
acks while its dataserver is still reading.
|
|
1201
|
+
- When `DETAILS.SOURCE_REF` names another `SOURCE_REF` than the request's
|
|
1202
|
+
own, `MORE_ROWS_ACK` also answers the request's. It is sent before the page:
|
|
1203
|
+
the real router acks while its dataserver is still reading. A request on
|
|
1204
|
+
the logon's own `SOURCE_REF` gets no ack, with or without
|
|
1205
|
+
`DETAILS.SOURCE_REF`, as on GSF.
|
|
1164
1206
|
- After the last page, `MORE_ROWS` still gets an answer:
|
|
1165
1207
|
`{QUERY_UPDATE, MORE_ROWS: false}` with no `ROW`. The real server sends the
|
|
1166
1208
|
same when a read finds nothing.
|
|
@@ -1551,6 +1593,17 @@ replaced by its getter:
|
|
|
1551
1593
|
Error compiling criteria expression: Failed to compile criteria expression Expr.containsIgnoreCase(row.getName(), 'a') AND row.getQty() > 1
|
|
1552
1594
|
```
|
|
1553
1595
|
|
|
1596
|
+
**So are operands side by side**, most often a double quote left unescaped
|
|
1597
|
+
in a double-quoted value: `NAME.equals("say "hi"")` is `"say "`, `hi` and `""`.
|
|
1598
|
+
Groovy reads such a run as a command expression (`a b c` is `this.a(b).c`)
|
|
1599
|
+
where one may start, which GSF's validator then refuses, and as a syntax error
|
|
1600
|
+
anywhere else (`src/db/criteriaJuxtaposition.ts`). The texts match Groovy
|
|
1601
|
+
3.0.25 and genesis-db 8.15.29 for the shapes clients write: a call's only
|
|
1602
|
+
argument, a later argument, after a binary operator. A type beside a name is
|
|
1603
|
+
no such pair: a cast (`(int) QTY`), a declaration (`String x = ...`) or a
|
|
1604
|
+
closure parameter, which GSF's validator refuses with its own text, and so
|
|
1605
|
+
does this engine. Any other shape is refused with an approximate text.
|
|
1606
|
+
|
|
1554
1607
|
**So is a call GSF's validator doesn't allow.** Only the `Expr` functions on
|
|
1555
1608
|
its whitelist (see [Criteria functions](#criteria-functions)) and seven field
|
|
1556
1609
|
methods pass; anything else — `Expr.foo(X)`, `Expr.stringDateTime`, a call on
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Row, TableDef } from '../types.ts';
|
|
2
|
+
export interface AuditContext {
|
|
3
|
+
eventType: string;
|
|
4
|
+
user: string;
|
|
5
|
+
text: string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare const auditTableName: (tableName: string) => string;
|
|
8
|
+
export declare function auditTableDef(tableName: string, def: TableDef): TableDef;
|
|
9
|
+
export declare function withAuditTables(tables: Record<string, TableDef>): Record<string, TableDef>;
|
|
10
|
+
export declare function checkNotDeclared(tables: Record<string, TableDef>, name: string, def: TableDef | undefined): void;
|
|
11
|
+
export declare const isAudited: (def: TableDef) => boolean;
|
|
12
|
+
export declare function auditRecord(record: Row, context: AuditContext, now: number): Row;
|
package/dist/db/audit.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Audit tables, as GSF builds them for a table declared with
|
|
2
|
+
// `audit = details(id = …, sequence = "XA")` (genesis-dictionary
|
|
3
|
+
// reader/model/Audit.kt and Fields.kt, GSF v8.15.29): <TABLE>_AUDIT holds the
|
|
4
|
+
// table's fields, its own sequence key <TABLE>_AUDIT_ID and four audit
|
|
5
|
+
// fields, with a non-unique <TABLE>_AUDIT_BY_UNDERLYING_ID index over the
|
|
6
|
+
// table's key. Every write an event handler makes to the table adds a row to
|
|
7
|
+
// it (TransactionalEventHandlerContainer writes through entityDb.audited):
|
|
8
|
+
// the record as written (as deleted, for a delete), the event's MESSAGE_TYPE,
|
|
9
|
+
// when, its REASON and the user. A write outside an event is not audited.
|
|
10
|
+
// GSF serves no resource for it; an app reads it through its own
|
|
11
|
+
// requestReply(<TABLE>_AUDIT) { request(<TABLE>_AUDIT.BY_UNDERLYING_ID) }.
|
|
12
|
+
import { RECORD_ID, TIMESTAMP } from "./identity.js";
|
|
13
|
+
// What GSF names the audit table's own fields (Fields.kt), and the size of
|
|
14
|
+
// AUDIT_EVENT_TEXT (genesis-dictionary pal DB_MAX_SIZE, Int.MAX_VALUE).
|
|
15
|
+
const AUDIT_EVENT_TYPE = 'AUDIT_EVENT_TYPE';
|
|
16
|
+
const AUDIT_EVENT_DATETIME = 'AUDIT_EVENT_DATETIME';
|
|
17
|
+
const AUDIT_EVENT_TEXT = 'AUDIT_EVENT_TEXT';
|
|
18
|
+
const AUDIT_EVENT_USER = 'AUDIT_EVENT_USER';
|
|
19
|
+
const AUDIT_TYPE_SIZE = 255;
|
|
20
|
+
const DB_MAX_SIZE = 2147483647;
|
|
21
|
+
// The sequence an audit table gets when TableDef.audit names none.
|
|
22
|
+
const DEFAULT_AUDIT_SEQUENCE = 'AU';
|
|
23
|
+
export const auditTableName = (tableName) => `${tableName}_AUDIT`;
|
|
24
|
+
const auditDefOf = (def) => def.audit === true ? {} : def.audit || undefined;
|
|
25
|
+
const pkFieldList = (def) => (Array.isArray(def.pkField) ? def.pkField : [def.pkField]);
|
|
26
|
+
// The audit tables auditTableDef made, so a config that already holds one
|
|
27
|
+
// (a server created twice from it) gets it made again rather than refused.
|
|
28
|
+
const derived = new WeakSet();
|
|
29
|
+
// The audit table GSF derives from an audited table.
|
|
30
|
+
export function auditTableDef(tableName, def) {
|
|
31
|
+
const audit = auditDefOf(def) ?? {};
|
|
32
|
+
const idField = `${auditTableName(tableName)}_ID`;
|
|
33
|
+
const keyFields = pkFieldList(def);
|
|
34
|
+
const auditFields = [
|
|
35
|
+
{ name: idField, type: 'STRING', nullable: false },
|
|
36
|
+
{ name: AUDIT_EVENT_TYPE, type: 'STRING', maxSize: AUDIT_TYPE_SIZE },
|
|
37
|
+
{ name: AUDIT_EVENT_DATETIME, type: 'DATETIME' },
|
|
38
|
+
{ name: AUDIT_EVENT_TEXT, type: 'STRING', maxSize: DB_MAX_SIZE },
|
|
39
|
+
{ name: AUDIT_EVENT_USER, type: 'STRING', maxSize: AUDIT_TYPE_SIZE },
|
|
40
|
+
];
|
|
41
|
+
const table = {
|
|
42
|
+
pkField: idField,
|
|
43
|
+
sequencePrefix: audit.sequencePrefix ?? DEFAULT_AUDIT_SEQUENCE,
|
|
44
|
+
...(def.sequenceWidth !== undefined ? { sequenceWidth: def.sequenceWidth } : {}),
|
|
45
|
+
...(def.sequenceFormat !== undefined ? { sequenceFormat: def.sequenceFormat } : {}),
|
|
46
|
+
...(def.fields
|
|
47
|
+
? {
|
|
48
|
+
fields: [
|
|
49
|
+
// The table's key is no key here, but its index keeps it not null.
|
|
50
|
+
...def.fields.map((field) => keyFields.includes(field.name) ? { ...field, nullable: false } : field),
|
|
51
|
+
...auditFields,
|
|
52
|
+
],
|
|
53
|
+
}
|
|
54
|
+
: {}),
|
|
55
|
+
indexes: [
|
|
56
|
+
{ name: `${auditTableName(tableName)}_BY_ID`, fields: [idField], unique: true },
|
|
57
|
+
{ name: `${auditTableName(tableName)}_BY_UNDERLYING_ID`, fields: keyFields },
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
derived.add(table);
|
|
61
|
+
return table;
|
|
62
|
+
}
|
|
63
|
+
// The tables with each audited one's audit table beside it. A table of that
|
|
64
|
+
// name declared by hand is refused, as GSF's dictionary refuses two tables of
|
|
65
|
+
// one name: the audit table is always the one derived from the table.
|
|
66
|
+
export function withAuditTables(tables) {
|
|
67
|
+
const audits = Object.entries(tables).flatMap(([tableName, def]) => {
|
|
68
|
+
if (!auditDefOf(def))
|
|
69
|
+
return [];
|
|
70
|
+
const name = auditTableName(tableName);
|
|
71
|
+
checkNotDeclared(tables, name, tables[name]);
|
|
72
|
+
return [[name, auditTableDef(tableName, def)]];
|
|
73
|
+
});
|
|
74
|
+
return audits.length > 0 ? { ...tables, ...Object.fromEntries(audits) } : tables;
|
|
75
|
+
}
|
|
76
|
+
// Refuses a definition, at start or set later, of a name that is an audited
|
|
77
|
+
// table's audit table, unless auditTableDef made it.
|
|
78
|
+
export function checkNotDeclared(tables, name, def) {
|
|
79
|
+
if (!def || derived.has(def))
|
|
80
|
+
return;
|
|
81
|
+
const audited = Object.entries(tables).find(([tableName, table]) => auditDefOf(table) && auditTableName(tableName) === name);
|
|
82
|
+
if (audited) {
|
|
83
|
+
throw new Error(`Table ${name} is ${audited[0]}'s audit table, which TableDef.audit derives: don't declare it`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export const isAudited = (def) => auditDefOf(def) !== undefined;
|
|
87
|
+
// The audit row for a record as written: its fields (without the record's
|
|
88
|
+
// own RECORD_ID and TIMESTAMP: the audit row gets its own) and the event's.
|
|
89
|
+
export function auditRecord(record, context, now) {
|
|
90
|
+
const { [RECORD_ID]: _recordId, [TIMESTAMP]: _timestamp, ...fields } = record;
|
|
91
|
+
return {
|
|
92
|
+
...fields,
|
|
93
|
+
[AUDIT_EVENT_TYPE]: context.eventType,
|
|
94
|
+
[AUDIT_EVENT_DATETIME]: now,
|
|
95
|
+
[AUDIT_EVENT_TEXT]: context.text,
|
|
96
|
+
[AUDIT_EVENT_USER]: context.user,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
// Any other shape is still refused, with an approximate text: ANTLR's error
|
|
26
26
|
// recovery differs case by case.
|
|
27
27
|
import { compileFailureText, tokenizeCriteria } from "./criteria.js";
|
|
28
|
+
import { juxtaposition } from "./criteriaJuxtaposition.js";
|
|
28
29
|
import { groovyCompileErrorText, groovySyntaxError } from "./groovySyntax.js";
|
|
29
30
|
import { EXPR_METHODS, FIELD_METHODS, groovyValidation, isClassName } from "./groovyValidator.js";
|
|
30
31
|
const VALIDATION_FAILED = 'GenericCriteriaValidationException : Criteria validation failed: ';
|
|
@@ -423,19 +424,25 @@ export function criteriaDialect(criteria, allowed, types) {
|
|
|
423
424
|
export function dialectRefusal(criteria, allowed, types) {
|
|
424
425
|
return criteriaDialect(criteria, allowed, types)?.text;
|
|
425
426
|
}
|
|
426
|
-
// What strict criteria refuse: a Groovy syntax error (groovySyntax.ts)
|
|
427
|
-
//
|
|
428
|
-
// undefined.
|
|
427
|
+
// What strict criteria refuse: a Groovy syntax error (groovySyntax.ts), the
|
|
428
|
+
// dialect, or operands side by side (criteriaJuxtaposition.ts), whichever
|
|
429
|
+
// Groovy meets first — the INVALID_CRITERIA TEXT, or undefined.
|
|
429
430
|
export function strictCriteriaRefusal(criteria, allowed, types) {
|
|
430
431
|
const syntax = groovySyntaxError(criteria);
|
|
431
|
-
// Groovy this grammar doesn't read (a list, arithmetic, a ternary, `new
|
|
432
|
-
// GSF's validator answers it whole.
|
|
432
|
+
// Groovy this grammar doesn't read (a list, arithmetic, a ternary, `new`,
|
|
433
|
+
// a command expression): GSF's validator answers it whole.
|
|
433
434
|
const groovy = syntax ? undefined : groovyValidation(criteria, allowed);
|
|
434
435
|
if (groovy)
|
|
435
436
|
return groovy.kind === 'refused' ? groovy.text : undefined;
|
|
436
|
-
|
|
437
|
-
|
|
437
|
+
// The syntax check stops at operands side by side, so its error is before them.
|
|
438
|
+
const [first] = [
|
|
439
|
+
criteriaDialect(criteria, allowed, types),
|
|
440
|
+
syntax ? undefined : juxtaposition(criteria),
|
|
441
|
+
]
|
|
442
|
+
.filter((refusal) => refusal !== undefined)
|
|
443
|
+
.sort((a, b) => a.at - b.at);
|
|
444
|
+
if (syntax && (!first || syntax.column - 1 < first.at)) {
|
|
438
445
|
return groovyCompileErrorText(criteria, syntax);
|
|
439
446
|
}
|
|
440
|
-
return
|
|
447
|
+
return first?.text;
|
|
441
448
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// What GSF answers a CRITERIA_MATCH with two operands side by side — most
|
|
2
|
+
// often a double quote left unescaped inside a double-quoted value
|
|
3
|
+
// (`NAME.equals("say "hi"")` is `"say "`, `hi`, `""`). Groovy reads operands
|
|
4
|
+
// side by side as a command expression (`a b c d e` is `this.a(b).c(d).e`)
|
|
5
|
+
// where one may start, and as a syntax error everywhere else, so GSF never
|
|
6
|
+
// runs such a criteria. Which of the two, and the text, depend on where the
|
|
7
|
+
// operands stand (Groovy 3.0.25 and genesis-db 8.15.29, captured by running
|
|
8
|
+
// them; test/criteriaJuxtaposition.test.ts pins them):
|
|
9
|
+
// - the only argument of a call, with no binary operator before the call:
|
|
10
|
+
// a command expression, refused by the validator (groovyValidator.ts);
|
|
11
|
+
// - the only argument of a call after a binary operator: a syntax error
|
|
12
|
+
// naming the statement up to the method;
|
|
13
|
+
// - a later argument of a call: a syntax error at the call's '(';
|
|
14
|
+
// - after a binary operator outside a call: a syntax error naming the
|
|
15
|
+
// statement up to the operand before them.
|
|
16
|
+
// A type beside a name is no such pair: a cast `(int) X`, a declaration
|
|
17
|
+
// `String x = ...` or a closure parameter `{ String s -> ... }` Groovy parses,
|
|
18
|
+
// and groovyValidator.ts gives GSF's text for them first. Any other shape is
|
|
19
|
+
// still refused, as GSF refuses it, but with an approximate text: GSF's may
|
|
20
|
+
// be another syntax error (the text follows the whole statement, where this
|
|
21
|
+
// one looks at the first pair only) or a validator error (`TRADER "x"` is the
|
|
22
|
+
// call `this.TRADER(x)`).
|
|
23
|
+
import { groovyCompileErrorText, groovyTokens } from "./groovySyntax.js";
|
|
24
|
+
// Names that are no operand: Groovy's keyword operators, `new`, and this
|
|
25
|
+
// engine's AND / OR / NOT dialect (criteriaDialect.ts answers those), in any
|
|
26
|
+
// case; and Groovy's other keywords, which start or shape a statement
|
|
27
|
+
// (`return QTY > 1` is valid Groovy), as written.
|
|
28
|
+
const NOT_OPERANDS = new Set(['in', 'instanceof', 'as', 'new', 'and', 'or', 'not']);
|
|
29
|
+
const KEYWORDS = new Set(('abstract assert boolean break byte case catch char class const continue def default do ' +
|
|
30
|
+
'double else enum extends final finally float for goto if implements import int interface ' +
|
|
31
|
+
'long native non-sealed package permits private protected public record return sealed ' +
|
|
32
|
+
'short static strictfp switch synchronized threadsafe throw throws trait transient try var ' +
|
|
33
|
+
'void volatile while yield').split(' '));
|
|
34
|
+
// Operators that end a postfix chain or start an operand: no binary operator.
|
|
35
|
+
const NOT_BINARY = new Set(['.', '?.', '*.', '.&', '.@', '::', '!', '~', '++', '--', '->']);
|
|
36
|
+
const startsOperand = (token) => !!token &&
|
|
37
|
+
(token.kind === 'literal' ||
|
|
38
|
+
(token.kind === 'name' &&
|
|
39
|
+
!NOT_OPERANDS.has(token.text.toLowerCase()) &&
|
|
40
|
+
!KEYWORDS.has(token.text)));
|
|
41
|
+
const endsOperand = (token) => !!token && (startsOperand(token) || (token.kind === 'close' && token.text !== '}'));
|
|
42
|
+
const isBinary = (token) => (token.kind === 'operator' && !NOT_BINARY.has(token.text)) ||
|
|
43
|
+
(token.kind === 'name' && ['in', 'instanceof', 'as'].includes(token.text));
|
|
44
|
+
// The index of the first operand that directly follows another, or -1.
|
|
45
|
+
export function juxtaposedIndex(tokens) {
|
|
46
|
+
return tokens.findIndex((token, index) => index > 0 && startsOperand(token) && endsOperand(tokens[index - 1]));
|
|
47
|
+
}
|
|
48
|
+
// The INVALID_CRITERIA TEXT GSF sends for a criteria with operands side by
|
|
49
|
+
// side, and where the second one is; undefined when it has none. The
|
|
50
|
+
// command expression groovyValidator.ts refuses gets its exact text there
|
|
51
|
+
// first; here it gets the approximate one only when that parser gives up.
|
|
52
|
+
export function juxtaposition(criteria) {
|
|
53
|
+
const tokens = groovyTokens(criteria);
|
|
54
|
+
if (!tokens)
|
|
55
|
+
return undefined;
|
|
56
|
+
const second = juxtaposedIndex(tokens);
|
|
57
|
+
if (second < 0)
|
|
58
|
+
return undefined;
|
|
59
|
+
const at = tokens[second].offset;
|
|
60
|
+
const syntaxError = (input, offset) => ({
|
|
61
|
+
text: groovyCompileErrorText(criteria, {
|
|
62
|
+
message: `Unexpected input: '${input}'`,
|
|
63
|
+
line: 1,
|
|
64
|
+
column: offset + 1,
|
|
65
|
+
}),
|
|
66
|
+
at,
|
|
67
|
+
});
|
|
68
|
+
const span = (from, to) => criteria.slice(from.offset, to.offset + to.text.length);
|
|
69
|
+
// The parentheses still open at the second operand, and where its statement starts.
|
|
70
|
+
const open = [];
|
|
71
|
+
let statement = 0;
|
|
72
|
+
for (let index = 0; index < second; index += 1) {
|
|
73
|
+
const token = tokens[index];
|
|
74
|
+
if (token.kind === 'open')
|
|
75
|
+
open.push(index);
|
|
76
|
+
else if (token.kind === 'close')
|
|
77
|
+
open.pop();
|
|
78
|
+
else if (token.kind === 'semicolon' && open.length === 0)
|
|
79
|
+
statement = index + 1;
|
|
80
|
+
}
|
|
81
|
+
const groupStart = (depth) => (depth > 0 ? open[depth - 1] + 1 : statement);
|
|
82
|
+
const binaryIn = (from, to) => tokens.slice(from, to).some(isBinary);
|
|
83
|
+
const paren = open.at(-1);
|
|
84
|
+
const callee = paren === undefined ? undefined : tokens[paren - 1];
|
|
85
|
+
const inCall = paren !== undefined &&
|
|
86
|
+
tokens[paren].text === '(' &&
|
|
87
|
+
callee?.kind === 'name' &&
|
|
88
|
+
!NOT_OPERANDS.has(callee.text.toLowerCase());
|
|
89
|
+
if (inCall) {
|
|
90
|
+
// A comma of this call's own list before the operands: a later argument.
|
|
91
|
+
let depth = 0;
|
|
92
|
+
let later = false;
|
|
93
|
+
for (let index = paren + 1; index < second; index += 1) {
|
|
94
|
+
const token = tokens[index];
|
|
95
|
+
if (token.kind === 'open')
|
|
96
|
+
depth += 1;
|
|
97
|
+
else if (token.kind === 'close')
|
|
98
|
+
depth -= 1;
|
|
99
|
+
else if (token.kind === 'comma' && depth === 0)
|
|
100
|
+
later = true;
|
|
101
|
+
}
|
|
102
|
+
if (later)
|
|
103
|
+
return syntaxError('(', tokens[paren].offset);
|
|
104
|
+
let end = second;
|
|
105
|
+
while (startsOperand(tokens[end + 1]))
|
|
106
|
+
end += 1;
|
|
107
|
+
const after = tokens[end + 1];
|
|
108
|
+
if (after?.kind === 'close' && after.text === ')') {
|
|
109
|
+
// The call's only argument: after a binary operator, a syntax error
|
|
110
|
+
// naming the statement up to the method.
|
|
111
|
+
const from = groupStart(open.length - 1);
|
|
112
|
+
if (binaryIn(from, paren - 1))
|
|
113
|
+
return syntaxError(span(tokens[from], callee), tokens[from].offset);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const from = groupStart(open.length);
|
|
118
|
+
if (binaryIn(from, second)) {
|
|
119
|
+
return syntaxError(span(tokens[from], tokens[second - 1]), tokens[from].offset);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return syntaxError(tokens[second].text, at);
|
|
123
|
+
}
|
package/dist/db/groovySyntax.js
CHANGED
|
@@ -119,7 +119,10 @@ const BINARY_KEYWORDS = new Set(['in', 'instanceof', 'as']);
|
|
|
119
119
|
const DIALECT_BINARY = new Set(['AND', 'OR']);
|
|
120
120
|
const NAME_START = /[A-Za-z_$]/;
|
|
121
121
|
const NAME_PART = /[A-Za-z0-9_$]/;
|
|
122
|
-
|
|
122
|
+
// Groovy's number literals: hex, binary, decimal (octal too), digits split by
|
|
123
|
+
// underscores, a fraction, an exponent and a type suffix.
|
|
124
|
+
const DIGITS = '\\d(?:[\\d_]*\\d)?';
|
|
125
|
+
const NUMBER = new RegExp(`^(?:0[xX][\\da-fA-F](?:[\\da-fA-F_]*[\\da-fA-F])?|0[bB][01](?:[01_]*[01])?|${DIGITS}(?:\\.${DIGITS})?(?:[eE][+-]?${DIGITS})?)[GgLlDdFfIi]?`);
|
|
123
126
|
// Characters Groovy can't start any token with.
|
|
124
127
|
const NEVER_TOKENS = new Set(['#', '`']);
|
|
125
128
|
const TRIPLE_QUOTE = 3;
|