@nova-energy/openamx 0.19.0 → 0.22.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 +12 -3
- package/dist/cli.js +61 -0
- package/dist/index.js +1 -0
- package/dist/parser/parseDocument.js +26 -13
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/runtime/evaluateDocument.js +9 -14
- package/dist/runtime/evaluateExpression.js +247 -12
- package/dist/runtime/inputData.js +73 -20
- package/dist/runtime/moduleLoader.js +29 -14
- package/dist/runtime/typescriptBridge.js +171 -0
- package/dist/transpiler/emit.js +987 -0
- package/dist/transpiler/filesystemGraph.js +267 -0
- package/dist/transpiler/graph.js +201 -0
- package/dist/transpiler/index.js +52 -0
- package/dist/transpiler/outputWriter.js +378 -0
- package/dist/transpiler/source.js +16 -0
- package/dist/transpiler/types.js +1 -0
- package/dist/transpilerRuntime.js +684 -0
- package/dist/typechecker/checkDocument.js +65 -3
- package/dist/typechecker/typescriptAnalysis.js +372 -0
- package/package.json +3 -2
|
@@ -4,8 +4,9 @@ import { evaluateStandardLibraryCall, isMathematicalFunction } from './standardL
|
|
|
4
4
|
import { fieldRegistry } from '../typechecker/declarationRegistry';
|
|
5
5
|
import { canonicalUnit, composeUnits, isAffineUnit, isMeasurement, measurement, measurementDisplayValue, measurementPhysicalValue, powerUnit, sqrtMeasurement, unitFromMetadata } from './measurement';
|
|
6
6
|
import { createDiagramModel } from './diagramModel';
|
|
7
|
-
function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
7
|
+
export function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
8
8
|
const diagnostics = [];
|
|
9
|
+
const activeValues = new WeakSet();
|
|
9
10
|
const report = (message, target, actual, dataPath, fieldSource) => {
|
|
10
11
|
const diagnostic = inputError('AMX4003', message, {
|
|
11
12
|
file,
|
|
@@ -32,11 +33,72 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
if (target.kind === 'list') {
|
|
35
|
-
|
|
36
|
+
let isArray;
|
|
37
|
+
try {
|
|
38
|
+
isArray = Array.isArray(actual);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
report('Expected a list value', target, actual, dataPath, fieldSource);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!isArray) {
|
|
36
45
|
report('Expected a list value', target, actual, dataPath, fieldSource);
|
|
37
46
|
return;
|
|
38
47
|
}
|
|
39
|
-
|
|
48
|
+
const array = actual;
|
|
49
|
+
let prototype;
|
|
50
|
+
try {
|
|
51
|
+
prototype = Object.getPrototypeOf(array);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (prototype !== Array.prototype) {
|
|
58
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (activeValues.has(array)) {
|
|
62
|
+
report('Cyclic values are not supported', target, actual, dataPath, fieldSource);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
activeValues.add(array);
|
|
66
|
+
try {
|
|
67
|
+
for (let index = 0; index < array.length; index++) {
|
|
68
|
+
let descriptor;
|
|
69
|
+
try {
|
|
70
|
+
descriptor = Object.getOwnPropertyDescriptor(array, String(index));
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
report('Expected a plain list value', target, actual, `${dataPath}[${index}]`, fieldSource);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (!descriptor || !('value' in descriptor) || descriptor.enumerable !== true) {
|
|
77
|
+
report('Expected a dense data list', target.element, descriptor ? 'accessor' : 'missing', `${dataPath}[${index}]`, fieldSource);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
visit(descriptor.value, target.element, `${dataPath}[${index}]`, fieldSource);
|
|
81
|
+
}
|
|
82
|
+
let keys;
|
|
83
|
+
try {
|
|
84
|
+
keys = Reflect.ownKeys(array);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
report('Expected a plain list value', target, actual, dataPath, fieldSource);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
for (const key of keys) {
|
|
91
|
+
if (key === 'length')
|
|
92
|
+
continue;
|
|
93
|
+
const index = typeof key === 'string' ? Number(key) : Number.NaN;
|
|
94
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= array.length || String(index) !== key) {
|
|
95
|
+
report('Unsupported list property', target, actual, `${dataPath}.${String(key)}`, fieldSource);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
activeValues.delete(array);
|
|
101
|
+
}
|
|
40
102
|
return;
|
|
41
103
|
}
|
|
42
104
|
if (target.kind === 'measurement') {
|
|
@@ -73,24 +135,92 @@ function validateRuntimeValue(value, expected, env, source, file, valuePath) {
|
|
|
73
135
|
report('Expected a Boolean value', target, actual, dataPath, fieldSource);
|
|
74
136
|
return;
|
|
75
137
|
}
|
|
138
|
+
const enumeration = env.enumDeclarations.get(target.name);
|
|
139
|
+
if (enumeration) {
|
|
140
|
+
const values = enumeration.resolvedValues ?? [];
|
|
141
|
+
if ((typeof actual !== 'number' && typeof actual !== 'string') || !values.includes(actual)) {
|
|
142
|
+
report(`Expected a member value of enum '${target.name}'`, target, actual, dataPath, fieldSource);
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
76
146
|
const declaration = env.recordTypes.get(target.name);
|
|
77
|
-
|
|
147
|
+
let isArray;
|
|
148
|
+
try {
|
|
149
|
+
isArray = Array.isArray(actual);
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!declaration || typeof actual !== 'object' || actual === null || isArray) {
|
|
78
156
|
report(`Expected a ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
79
157
|
return;
|
|
80
158
|
}
|
|
159
|
+
let prototype;
|
|
160
|
+
try {
|
|
161
|
+
prototype = Object.getPrototypeOf(actual);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
168
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (activeValues.has(actual)) {
|
|
172
|
+
report('Cyclic values are not supported', target, actual, dataPath, fieldSource);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
activeValues.add(actual);
|
|
81
176
|
const record = actual;
|
|
82
177
|
const fieldNames = new Set(declaration.fields.map(field => field.name));
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
178
|
+
try {
|
|
179
|
+
let keys;
|
|
180
|
+
try {
|
|
181
|
+
keys = Reflect.ownKeys(record);
|
|
87
182
|
}
|
|
88
|
-
|
|
89
|
-
|
|
183
|
+
catch {
|
|
184
|
+
report(`Expected a plain ${target.name} record`, target, actual, dataPath, fieldSource);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
for (const field of declaration.fields) {
|
|
188
|
+
const fieldScope = fieldRegistry(field, env.dimensionRegistry);
|
|
189
|
+
let descriptor;
|
|
190
|
+
try {
|
|
191
|
+
descriptor = Object.getOwnPropertyDescriptor(record, field.name);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
report(`Expected a plain data field '${field.name}'`, checkedType(field.annotation, fieldScope), 'accessor', `${dataPath}.${field.name}`, field.source);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (!descriptor) {
|
|
198
|
+
report(`Missing computed field '${field.name}'`, checkedType(field.annotation, fieldScope), 'missing', `${dataPath}.${field.name}`, field.source);
|
|
199
|
+
}
|
|
200
|
+
else if (!('value' in descriptor) || descriptor.enumerable !== true) {
|
|
201
|
+
report(`Expected a plain data field '${field.name}'`, checkedType(field.annotation, fieldScope), 'accessor', `${dataPath}.${field.name}`, field.source);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
visit(descriptor.value, checkedType(field.annotation, fieldScope), `${dataPath}.${field.name}`, field.source);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const key of keys) {
|
|
208
|
+
if (typeof key !== 'string' || !fieldNames.has(key)) {
|
|
209
|
+
let actualValue = 'unsupported property';
|
|
210
|
+
try {
|
|
211
|
+
const descriptor = Object.getOwnPropertyDescriptor(record, key);
|
|
212
|
+
if (descriptor && 'value' in descriptor)
|
|
213
|
+
actualValue = descriptor.value;
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
actualValue = 'unavailable property';
|
|
217
|
+
}
|
|
218
|
+
report(`Unknown computed field '${String(key)}'`, target, actualValue, `${dataPath}.${String(key)}`, fieldSource);
|
|
219
|
+
}
|
|
90
220
|
}
|
|
91
221
|
}
|
|
92
|
-
|
|
93
|
-
|
|
222
|
+
finally {
|
|
223
|
+
activeValues.delete(actual);
|
|
94
224
|
}
|
|
95
225
|
};
|
|
96
226
|
visit(value, expected, valuePath);
|
|
@@ -460,6 +590,111 @@ function evalUnary(node, env, file) {
|
|
|
460
590
|
}
|
|
461
591
|
throw new Error(`Unsupported unary operator: ${node.operator}`);
|
|
462
592
|
}
|
|
593
|
+
export function evaluateBinaryValues(operator, leftValue, rightValue, source, file) {
|
|
594
|
+
const environment = new Environment();
|
|
595
|
+
const leftName = '__amxRuntimeLeftValue';
|
|
596
|
+
const rightName = '__amxRuntimeRightValue';
|
|
597
|
+
environment.set(leftName, leftValue);
|
|
598
|
+
environment.set(rightName, rightValue);
|
|
599
|
+
return evalBinary({
|
|
600
|
+
type: 'binaryExpression',
|
|
601
|
+
operator,
|
|
602
|
+
left: { type: 'identifier', name: leftName, source },
|
|
603
|
+
right: { type: 'identifier', name: rightName, source },
|
|
604
|
+
operatorSource: source,
|
|
605
|
+
source
|
|
606
|
+
}, environment, file);
|
|
607
|
+
}
|
|
608
|
+
export function evaluateUnaryValue(operator, value, source, file) {
|
|
609
|
+
const environment = new Environment();
|
|
610
|
+
const name = '__amxRuntimeUnaryValue';
|
|
611
|
+
environment.set(name, value);
|
|
612
|
+
return evalUnary({
|
|
613
|
+
type: 'unaryExpression',
|
|
614
|
+
operator,
|
|
615
|
+
argument: { type: 'identifier', name, source },
|
|
616
|
+
source
|
|
617
|
+
}, environment, file);
|
|
618
|
+
}
|
|
619
|
+
export function evaluateBuiltinValues(callee, values, source, file, dimensionRegistry) {
|
|
620
|
+
const environment = new Environment();
|
|
621
|
+
environment.dimensionRegistry = dimensionRegistry;
|
|
622
|
+
const argumentsList = values.map((value, index) => {
|
|
623
|
+
const name = `__amxRuntimeArgument${index}`;
|
|
624
|
+
environment.set(name, value);
|
|
625
|
+
return { type: 'identifier', name, source };
|
|
626
|
+
});
|
|
627
|
+
return evalFunctionCall({
|
|
628
|
+
type: 'functionCall',
|
|
629
|
+
callee,
|
|
630
|
+
arguments: argumentsList,
|
|
631
|
+
source
|
|
632
|
+
}, environment, file);
|
|
633
|
+
}
|
|
634
|
+
export function evaluateListItem(listValue, indexValue, source, file) {
|
|
635
|
+
const environment = new Environment();
|
|
636
|
+
const listName = '__amxRuntimeListValue';
|
|
637
|
+
const indexName = '__amxRuntimeIndexValue';
|
|
638
|
+
environment.set(listName, listValue);
|
|
639
|
+
environment.set(indexName, indexValue);
|
|
640
|
+
return evalListAccess({
|
|
641
|
+
type: 'listAccess',
|
|
642
|
+
receiver: { type: 'identifier', name: listName, source },
|
|
643
|
+
index: { type: 'identifier', name: indexName, source },
|
|
644
|
+
source
|
|
645
|
+
}, environment, file);
|
|
646
|
+
}
|
|
647
|
+
export function evaluateRangeValues(startValue, endValue, source, file) {
|
|
648
|
+
const environment = new Environment();
|
|
649
|
+
const startName = '__amxRuntimeRangeStart';
|
|
650
|
+
const endName = '__amxRuntimeRangeEnd';
|
|
651
|
+
environment.set(startName, startValue);
|
|
652
|
+
environment.set(endName, endValue);
|
|
653
|
+
return evalRange({
|
|
654
|
+
type: 'rangeExpression',
|
|
655
|
+
start: { type: 'identifier', name: startName, source },
|
|
656
|
+
end: { type: 'identifier', name: endName, source },
|
|
657
|
+
source
|
|
658
|
+
}, environment, file);
|
|
659
|
+
}
|
|
660
|
+
export function evaluateListAddition(list, value, position, mutable, targetName, source, file) {
|
|
661
|
+
const environment = new Environment();
|
|
662
|
+
environment.set(targetName, list);
|
|
663
|
+
if (!mutable)
|
|
664
|
+
environment.markImmutable(list);
|
|
665
|
+
const valueName = '__amxRuntimeListAddValue';
|
|
666
|
+
const positionName = '__amxRuntimeListAddPosition';
|
|
667
|
+
environment.set(valueName, value);
|
|
668
|
+
environment.set(positionName, position);
|
|
669
|
+
evaluateAddStatement({
|
|
670
|
+
type: 'addStatement', name: targetName,
|
|
671
|
+
value: { type: 'identifier', name: valueName, source },
|
|
672
|
+
...(position === undefined ? {} : { index: { type: 'identifier', name: positionName, source } }),
|
|
673
|
+
source
|
|
674
|
+
}, environment, file);
|
|
675
|
+
}
|
|
676
|
+
export function evaluateListRemoval(list, count, position, mutable, targetName, source, file) {
|
|
677
|
+
const environment = new Environment();
|
|
678
|
+
environment.set(targetName, list);
|
|
679
|
+
if (!mutable)
|
|
680
|
+
environment.markImmutable(list);
|
|
681
|
+
const countName = '__amxRuntimeListRemoveCount';
|
|
682
|
+
const positionName = '__amxRuntimeListRemovePosition';
|
|
683
|
+
environment.set(countName, count);
|
|
684
|
+
environment.set(positionName, position);
|
|
685
|
+
evaluateRemoveStatement({
|
|
686
|
+
type: 'removeStatement', name: targetName,
|
|
687
|
+
count: { type: 'identifier', name: countName, source },
|
|
688
|
+
...(position === undefined ? {} : { index: { type: 'identifier', name: positionName, source } }),
|
|
689
|
+
source
|
|
690
|
+
}, environment, file);
|
|
691
|
+
}
|
|
692
|
+
export function evaluateIterableValue(value, source, file) {
|
|
693
|
+
const environment = new Environment();
|
|
694
|
+
const name = '__amxRuntimeIterableValue';
|
|
695
|
+
environment.set(name, value);
|
|
696
|
+
return evaluateLoopIterable({ type: 'identifier', name, source }, environment, file);
|
|
697
|
+
}
|
|
463
698
|
function evalConditional(node, env, file) {
|
|
464
699
|
const testVal = evaluateExpression(node.test, env, file);
|
|
465
700
|
return toBoolean(testVal)
|
|
@@ -15,12 +15,12 @@ export { parseStrictCsvText, parseStrictJsonText } from './dataText';
|
|
|
15
15
|
export { serializeCsvText } from './csvTextSerialization';
|
|
16
16
|
class StopValidation {
|
|
17
17
|
}
|
|
18
|
-
export function describeInputSchema(declaration, types, registry) {
|
|
18
|
+
export function describeInputSchema(declaration, types, registry, enums) {
|
|
19
19
|
const recordType = declaration.annotation.type === 'listType'
|
|
20
20
|
&& declaration.annotation.element?.type === 'namedType'
|
|
21
21
|
? types.get(declaration.annotation.element.name)
|
|
22
22
|
: undefined;
|
|
23
|
-
const csvCompatible = !!recordType && recordType.fields.every(field => isCsvScalar(field.annotation, fieldRegistry(field, registry)));
|
|
23
|
+
const csvCompatible = !!recordType && recordType.fields.every(field => isCsvScalar(field.annotation, fieldRegistry(field, registry), enums));
|
|
24
24
|
return {
|
|
25
25
|
name: declaration.name,
|
|
26
26
|
type: typeName(declaration.annotation),
|
|
@@ -38,7 +38,7 @@ export function describeInputSchema(declaration, types, registry) {
|
|
|
38
38
|
}) } : {})
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
-
export function validateInputText(text, format, declaration, types, mode = 'aggregate', context = {}, registry) {
|
|
41
|
+
export function validateInputText(text, format, declaration, types, mode = 'aggregate', context = {}, registry, enums) {
|
|
42
42
|
if (mode !== 'aggregate' && mode !== 'fail-fast') {
|
|
43
43
|
return { diagnostics: [inputError('AMX4001', `Unsupported validation mode '${safeText(String(mode))}'`, { ...context, inputName: declaration.name })] };
|
|
44
44
|
}
|
|
@@ -49,7 +49,7 @@ export function validateInputText(text, format, declaration, types, mode = 'aggr
|
|
|
49
49
|
throw new StopValidation();
|
|
50
50
|
};
|
|
51
51
|
const validationContext = { ...context, inputName: declaration.name, declarationSource: declaration.source };
|
|
52
|
-
const environment =
|
|
52
|
+
const environment = inputEnvironment(types, enums);
|
|
53
53
|
let value;
|
|
54
54
|
try {
|
|
55
55
|
if (format === 'json') {
|
|
@@ -65,10 +65,10 @@ export function validateInputText(text, format, declaration, types, mode = 'aggr
|
|
|
65
65
|
}));
|
|
66
66
|
return { value: undefined, diagnostics };
|
|
67
67
|
}
|
|
68
|
-
value = convertJson(parsed.value, declaration.annotation, '', declaration.source, types, environment, report, validationContext, undefined, registry);
|
|
68
|
+
value = convertJson(parsed.value, declaration.annotation, '', declaration.source, types, environment, report, validationContext, undefined, registry, false, enums);
|
|
69
69
|
}
|
|
70
70
|
else {
|
|
71
|
-
value = convertCsv(text, declaration.annotation, declaration.source, types, environment, report, validationContext, registry);
|
|
71
|
+
value = convertCsv(text, declaration.annotation, declaration.source, types, environment, report, validationContext, registry, enums);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
catch (error) {
|
|
@@ -77,7 +77,7 @@ export function validateInputText(text, format, declaration, types, mode = 'aggr
|
|
|
77
77
|
}
|
|
78
78
|
return { value, diagnostics };
|
|
79
79
|
}
|
|
80
|
-
export async function loadInputValues(declarations, types, rawMappings = [], mode = 'aggregate', entryFile, registry) {
|
|
80
|
+
export async function loadInputValues(declarations, types, rawMappings = [], mode = 'aggregate', entryFile, registry, enums) {
|
|
81
81
|
if (mode !== 'aggregate' && mode !== 'fail-fast') {
|
|
82
82
|
throwInputErrors([inputError('AMX4001', `Unsupported validation mode '${safeText(String(mode))}'`, { file: entryFile })]);
|
|
83
83
|
}
|
|
@@ -145,7 +145,7 @@ export async function loadInputValues(declarations, types, rawMappings = [], mod
|
|
|
145
145
|
}));
|
|
146
146
|
continue;
|
|
147
147
|
}
|
|
148
|
-
const validated = validateInputText(text, extension.slice(1), declaration, types, mode, { file: entryFile, dataFile: filePath }, registry);
|
|
148
|
+
const validated = validateInputText(text, extension.slice(1), declaration, types, mode, { file: entryFile, dataFile: filePath }, registry, enums);
|
|
149
149
|
diagnostics.push(...validated.diagnostics);
|
|
150
150
|
if (validated.diagnostics.length > 0 && mode === 'fail-fast')
|
|
151
151
|
throw new StopValidation();
|
|
@@ -161,11 +161,11 @@ export async function loadInputValues(declarations, types, rawMappings = [], mod
|
|
|
161
161
|
throw error;
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
function convertJson(value, annotation, pointer, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault = false) {
|
|
164
|
+
function convertJson(value, annotation, pointer, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault = false, enums) {
|
|
165
165
|
if (annotation.type === 'nullableType') {
|
|
166
166
|
if (value === null)
|
|
167
167
|
return null;
|
|
168
|
-
return convertJson(value, annotation.element, pointer, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault);
|
|
168
|
+
return convertJson(value, annotation.element, pointer, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault, enums);
|
|
169
169
|
}
|
|
170
170
|
if (value === null) {
|
|
171
171
|
report(shapeDiagnostic('Null is not allowed for this input type', typeName(annotation), 'null', pointer, context, declarationSource, fieldSource));
|
|
@@ -176,7 +176,7 @@ function convertJson(value, annotation, pointer, declarationSource, types, envir
|
|
|
176
176
|
report(shapeDiagnostic('Expected a JSON array', typeName(annotation), actualType(value), pointer, context, declarationSource, fieldSource));
|
|
177
177
|
return [];
|
|
178
178
|
}
|
|
179
|
-
return value.map((item, index) => convertJson(item, annotation.element, `${pointer}/${index}`, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault));
|
|
179
|
+
return value.map((item, index) => convertJson(item, annotation.element, `${pointer}/${index}`, declarationSource, types, environment, report, context, fieldSource, registry, internalDefault, enums));
|
|
180
180
|
}
|
|
181
181
|
const name = annotation.name;
|
|
182
182
|
const dimension = registry?.dimensions.get(name);
|
|
@@ -188,6 +188,14 @@ function convertJson(value, annotation, pointer, declarationSource, types, envir
|
|
|
188
188
|
}
|
|
189
189
|
if (dimension)
|
|
190
190
|
return convertJsonMeasurement(value, dimension.vector, pointer, declarationSource, fieldSource, context, report, registry);
|
|
191
|
+
const enumeration = enums?.get(name);
|
|
192
|
+
if (enumeration) {
|
|
193
|
+
const values = resolvedEnumValues(enumeration);
|
|
194
|
+
if (!values.includes(value)) {
|
|
195
|
+
report(shapeDiagnostic(`Value is not a member of enum '${name}'`, name, describe(value), pointer, context, declarationSource, fieldSource));
|
|
196
|
+
}
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
191
199
|
if (name === 'Number' || name === 'String' || name === 'Boolean' || name === 'DateTime') {
|
|
192
200
|
const valid = name === 'Number' ? typeof value === 'number' && Number.isFinite(value)
|
|
193
201
|
: name === 'String' || name === 'DateTime' ? typeof value === 'string'
|
|
@@ -221,11 +229,11 @@ function convertJson(value, annotation, pointer, declarationSource, types, envir
|
|
|
221
229
|
const fieldScope = fieldRegistry(field, registry);
|
|
222
230
|
const propertyPointer = `${pointer}/${escapePointer(field.name)}`;
|
|
223
231
|
if (Object.prototype.hasOwnProperty.call(object, field.name)) {
|
|
224
|
-
materialized[field.name] = convertJson(object[field.name], field.annotation, propertyPointer, declarationSource, types, environment, report, context, field.source, fieldScope, internalDefault);
|
|
232
|
+
materialized[field.name] = convertJson(object[field.name], field.annotation, propertyPointer, declarationSource, types, environment, report, context, field.source, fieldScope, internalDefault, enums);
|
|
225
233
|
}
|
|
226
234
|
else if (field.defaultExpression) {
|
|
227
235
|
const value = evaluateExpression(field.defaultExpression, defaultsEnvironment(environment, fieldScope), context.file);
|
|
228
|
-
materialized[field.name] = convertJson(value, field.annotation, propertyPointer, declarationSource, types, environment, report, context, field.source, fieldScope, true);
|
|
236
|
+
materialized[field.name] = convertJson(value, field.annotation, propertyPointer, declarationSource, types, environment, report, context, field.source, fieldScope, true, enums);
|
|
229
237
|
}
|
|
230
238
|
else if (field.optional) {
|
|
231
239
|
materialized[field.name] = null;
|
|
@@ -236,13 +244,13 @@ function convertJson(value, annotation, pointer, declarationSource, types, envir
|
|
|
236
244
|
}
|
|
237
245
|
return materialized;
|
|
238
246
|
}
|
|
239
|
-
function convertCsv(text, annotation, declarationSource, types, environment, report, context, registry) {
|
|
247
|
+
function convertCsv(text, annotation, declarationSource, types, environment, report, context, registry, enums) {
|
|
240
248
|
if (annotation.type !== 'listType' || annotation.element?.type !== 'namedType' || !types.has(annotation.element.name)) {
|
|
241
249
|
report(shapeDiagnostic('CSV input requires a list of one record type', 'RecordType[]', typeName(annotation), '', context, declarationSource));
|
|
242
250
|
return [];
|
|
243
251
|
}
|
|
244
252
|
const recordType = types.get(annotation.element.name);
|
|
245
|
-
const scalarFields = recordType.fields.every(field => isCsvScalar(field.annotation, fieldRegistry(field, registry)));
|
|
253
|
+
const scalarFields = recordType.fields.every(field => isCsvScalar(field.annotation, fieldRegistry(field, registry), enums));
|
|
246
254
|
if (!scalarFields) {
|
|
247
255
|
report(shapeDiagnostic('CSV records may contain only scalar fields', 'scalar-field RecordType[]', recordType.name, '', context, declarationSource));
|
|
248
256
|
return [];
|
|
@@ -294,7 +302,7 @@ function convertCsv(text, annotation, declarationSource, types, environment, rep
|
|
|
294
302
|
if (field.defaultExpression) {
|
|
295
303
|
const fieldScope = fieldRegistry(field, registry);
|
|
296
304
|
const value = evaluateExpression(field.defaultExpression, defaultsEnvironment(environment, fieldScope), context.file);
|
|
297
|
-
materialized[field.name] = convertJson(value, field.annotation, propertyPath, context.declarationSource, types, environment, report, context, field.source, fieldScope, true);
|
|
305
|
+
materialized[field.name] = convertJson(value, field.annotation, propertyPath, context.declarationSource, types, environment, report, context, field.source, fieldScope, true, enums);
|
|
298
306
|
}
|
|
299
307
|
else if (field.optional)
|
|
300
308
|
materialized[field.name] = null;
|
|
@@ -303,13 +311,13 @@ function convertCsv(text, annotation, declarationSource, types, environment, rep
|
|
|
303
311
|
const cell = row[column];
|
|
304
312
|
if (!cell)
|
|
305
313
|
continue;
|
|
306
|
-
materialized[field.name] = convertCsvCell(cell, field.annotation, propertyPath, recordNumber, column + 1, field.source, report, context, fieldRegistry(field, registry));
|
|
314
|
+
materialized[field.name] = convertCsvCell(cell, field.annotation, propertyPath, recordNumber, column + 1, field.source, report, context, fieldRegistry(field, registry), enums);
|
|
307
315
|
}
|
|
308
316
|
result.push(materialized);
|
|
309
317
|
}
|
|
310
318
|
return result;
|
|
311
319
|
}
|
|
312
|
-
function convertCsvCell(cell, annotation, dataPath, recordNumber, columnNumber, fieldSource, report, context, registry) {
|
|
320
|
+
function convertCsvCell(cell, annotation, dataPath, recordNumber, columnNumber, fieldSource, report, context, registry, enums) {
|
|
313
321
|
const base = annotation.type === 'nullableType' ? annotation.element : annotation;
|
|
314
322
|
const nullable = annotation.type === 'nullableType';
|
|
315
323
|
const location = { ...context, recordNumber, dataLine: recordNumber + 1, dataColumn: columnNumber };
|
|
@@ -320,6 +328,11 @@ function convertCsvCell(cell, annotation, dataPath, recordNumber, columnNumber,
|
|
|
320
328
|
return null;
|
|
321
329
|
}
|
|
322
330
|
if (cell.value === '' && cell.quoted) {
|
|
331
|
+
if (base.type === 'namedType' && enums?.has(base.name)) {
|
|
332
|
+
const value = '';
|
|
333
|
+
validateCsvEnumValue(value, base.name, enums.get(base.name), dataPath, location, fieldSource, report);
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
323
336
|
if (base.type === 'namedType' && base.name === 'String')
|
|
324
337
|
return '';
|
|
325
338
|
report(shapeDiagnostic('Quoted empty CSV cell is valid only for String fields', typeName(annotation), 'empty String', dataPath, location, undefined, fieldSource));
|
|
@@ -327,6 +340,20 @@ function convertCsvCell(cell, annotation, dataPath, recordNumber, columnNumber,
|
|
|
327
340
|
}
|
|
328
341
|
if (base.type !== 'namedType')
|
|
329
342
|
return cell.value;
|
|
343
|
+
const enumeration = enums?.get(base.name);
|
|
344
|
+
if (enumeration) {
|
|
345
|
+
const valueType = typeof resolvedEnumValues(enumeration)[0];
|
|
346
|
+
let value = cell.value;
|
|
347
|
+
if (valueType === 'number') {
|
|
348
|
+
if (!/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(cell.value) || !Number.isFinite(Number(cell.value))) {
|
|
349
|
+
report(shapeDiagnostic('Invalid finite numeric enum input', base.name, cell.value, dataPath, location, undefined, fieldSource));
|
|
350
|
+
return cell.value;
|
|
351
|
+
}
|
|
352
|
+
value = Number(cell.value);
|
|
353
|
+
}
|
|
354
|
+
validateCsvEnumValue(value, base.name, enumeration, dataPath, location, fieldSource, report);
|
|
355
|
+
return value;
|
|
356
|
+
}
|
|
330
357
|
const dimension = registry?.dimensions.get(base.name);
|
|
331
358
|
if (dimension) {
|
|
332
359
|
const match = cell.value.match(/^\s*([+-]?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)\s+(.+?)\s*$/);
|
|
@@ -464,10 +491,36 @@ function typeName(annotation) {
|
|
|
464
491
|
return annotation.name;
|
|
465
492
|
return annotation.type === 'listType' ? `${typeName(annotation.element)}[]` : `${typeName(annotation.element)}?`;
|
|
466
493
|
}
|
|
467
|
-
function isCsvScalar(annotation, registry) {
|
|
494
|
+
function isCsvScalar(annotation, registry, enums) {
|
|
468
495
|
const base = annotation.type === 'nullableType' ? annotation.element : annotation;
|
|
469
496
|
return base.type === 'namedType' && (['String', 'Number', 'Boolean', 'DateTime'].includes(base.name)
|
|
470
|
-
|| !!registry?.dimensions.has(base.name)
|
|
497
|
+
|| !!registry?.dimensions.has(base.name)
|
|
498
|
+
|| !!enums?.has(base.name));
|
|
499
|
+
}
|
|
500
|
+
function inputEnvironment(types, enums) {
|
|
501
|
+
const environment = new Environment(types);
|
|
502
|
+
for (const [name, declaration] of enums ?? []) {
|
|
503
|
+
const values = resolvedEnumValues(declaration);
|
|
504
|
+
const members = {};
|
|
505
|
+
declaration.members.forEach((member, index) => { members[member.name] = values[index]; });
|
|
506
|
+
environment.enumDeclarations.set(name, declaration);
|
|
507
|
+
environment.enumValues.set(name, members);
|
|
508
|
+
}
|
|
509
|
+
return environment;
|
|
510
|
+
}
|
|
511
|
+
function resolvedEnumValues(declaration) {
|
|
512
|
+
return declaration.resolvedValues ?? declaration.members.map((member, index) => {
|
|
513
|
+
if (!member.value)
|
|
514
|
+
return index + 1;
|
|
515
|
+
if (member.value.type === 'numberLiteral' || member.value.type === 'stringLiteral')
|
|
516
|
+
return member.value.value;
|
|
517
|
+
return NaN;
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
function validateCsvEnumValue(value, name, declaration, dataPath, context, fieldSource, report) {
|
|
521
|
+
if (!resolvedEnumValues(declaration).includes(value)) {
|
|
522
|
+
report(shapeDiagnostic(`Value is not a member of enum '${name}'`, name, String(value), dataPath, context, undefined, fieldSource));
|
|
523
|
+
}
|
|
471
524
|
}
|
|
472
525
|
function isDateTime(value) {
|
|
473
526
|
const parts = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
3
|
import { parseDocument, parseDocumentText } from '../parser/parseDocument';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { checkDocumentWithRuntimePlan } from '../typechecker/checkDocument';
|
|
5
|
+
import { evaluateDocumentNodes, prepareTypeScriptBlocks } from './typescriptBridge';
|
|
6
6
|
import { Environment } from './environment';
|
|
7
7
|
import { AmxError, moduleError, staticError, throwInputErrors } from '../diagnostics/errors';
|
|
8
8
|
import { describeInputSchema, loadInputValues, validateInputText } from './inputData';
|
|
@@ -253,7 +253,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
-
const
|
|
256
|
+
const checked = checkDocumentWithRuntimePlan(doc, canonicalPath, {
|
|
257
257
|
types: importedTypes,
|
|
258
258
|
enums: importedEnums,
|
|
259
259
|
functions: importedFunctions,
|
|
@@ -265,7 +265,20 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
265
265
|
isEntryModule: canonicalPath === realEntry
|
|
266
266
|
});
|
|
267
267
|
visiting.pop();
|
|
268
|
-
const record = {
|
|
268
|
+
const record = {
|
|
269
|
+
canonicalPath,
|
|
270
|
+
doc,
|
|
271
|
+
checkResult: checked.result,
|
|
272
|
+
typescriptBlocks: checked.typescriptBlocks,
|
|
273
|
+
importedTypes,
|
|
274
|
+
importedEnums,
|
|
275
|
+
importedEnumSources,
|
|
276
|
+
importedFunctions,
|
|
277
|
+
importedBindings,
|
|
278
|
+
importedBindingSources,
|
|
279
|
+
importedDimensions,
|
|
280
|
+
importedUnits
|
|
281
|
+
};
|
|
269
282
|
resolved.set(canonicalPath, record);
|
|
270
283
|
evaluationOrder.push(canonicalPath);
|
|
271
284
|
return record;
|
|
@@ -281,9 +294,12 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
281
294
|
const entryStatements = flattenStatements(entryRecord.doc);
|
|
282
295
|
const inputDeclarations = entryStatements.filter(statement => statement.type === 'inputDeclaration');
|
|
283
296
|
const entryTypes = new Map(entryRecord.importedTypes);
|
|
297
|
+
const entryEnums = new Map(entryRecord.importedEnums);
|
|
284
298
|
for (const statement of entryStatements) {
|
|
285
299
|
if (statement.type === 'typeDeclaration')
|
|
286
300
|
entryTypes.set(statement.name, statement);
|
|
301
|
+
else if (statement.type === 'enumDeclaration')
|
|
302
|
+
entryEnums.set(statement.name, statement);
|
|
287
303
|
}
|
|
288
304
|
const outputs = prepareOutputs(options.outputMappings, entryRecord.checkResult.exportedBindings, entryTypes, options.reservedOutputPath, registry);
|
|
289
305
|
const outputSchemas = options.inputInspection || options.outputInspection
|
|
@@ -299,7 +315,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
299
315
|
inputName: options.inputInspection.name
|
|
300
316
|
}]);
|
|
301
317
|
}
|
|
302
|
-
const validation = validateInputText(options.inputInspection.text, options.inputInspection.format, declaration, entryTypes, options.validation ?? 'aggregate', { file: entryPath }, registry);
|
|
318
|
+
const validation = validateInputText(options.inputInspection.text, options.inputInspection.format, declaration, entryTypes, options.validation ?? 'aggregate', { file: entryPath }, registry, entryEnums);
|
|
303
319
|
return {
|
|
304
320
|
doc: entryRecord.doc,
|
|
305
321
|
env: new Environment(entryTypes, new Map(), registry),
|
|
@@ -308,7 +324,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
308
324
|
diagramEmissions: [],
|
|
309
325
|
registry,
|
|
310
326
|
inputInspection: {
|
|
311
|
-
schema: describeInputSchema(declaration, entryTypes, registry),
|
|
327
|
+
schema: describeInputSchema(declaration, entryTypes, registry, entryEnums),
|
|
312
328
|
valid: validation.diagnostics.length === 0,
|
|
313
329
|
diagnostics: validation.diagnostics,
|
|
314
330
|
outputs: outputSchemas ?? []
|
|
@@ -326,7 +342,12 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
326
342
|
registry
|
|
327
343
|
};
|
|
328
344
|
}
|
|
329
|
-
const inputValues = await loadInputValues(inputDeclarations, entryTypes, options.inputMappings, options.validation, entryPath, registry);
|
|
345
|
+
const inputValues = await loadInputValues(inputDeclarations, entryTypes, options.inputMappings, options.validation, entryPath, registry, entryEnums);
|
|
346
|
+
const preparedTypeScript = new Map();
|
|
347
|
+
for (const canonicalPath of evaluationOrder) {
|
|
348
|
+
const record = resolved.get(canonicalPath);
|
|
349
|
+
preparedTypeScript.set(canonicalPath, prepareTypeScriptBlocks(record.typescriptBlocks, canonicalPath));
|
|
350
|
+
}
|
|
330
351
|
const evaluatedEnvironments = new Map();
|
|
331
352
|
for (const canonicalPath of evaluationOrder) {
|
|
332
353
|
const record = resolved.get(canonicalPath);
|
|
@@ -359,13 +380,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
359
380
|
for (const [name, value] of inputValues)
|
|
360
381
|
env.set(name, value);
|
|
361
382
|
}
|
|
362
|
-
|
|
363
|
-
const node = record.doc.nodes[nodeIndex];
|
|
364
|
-
if (node.type === 'executableCodeBlock') {
|
|
365
|
-
env.currentDocumentNodeIndex = nodeIndex;
|
|
366
|
-
evaluateStatements(node.statements, env, canonicalPath);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
383
|
+
evaluateDocumentNodes(record.doc, env, canonicalPath, record.typescriptBlocks, preparedTypeScript.get(canonicalPath));
|
|
369
384
|
evaluatedEnvironments.set(canonicalPath, env);
|
|
370
385
|
}
|
|
371
386
|
const entryEnv = evaluatedEnvironments.get(realEntry);
|