@nova-energy/openamx 0.19.0 → 0.21.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 +7 -2
- package/dist/cli.js +61 -0
- package/dist/index.js +1 -0
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/runtime/evaluateExpression.js +105 -0
- package/dist/runtime/inputData.js +73 -20
- package/dist/runtime/moduleLoader.js +6 -3
- package/dist/transpiler/emit.js +842 -0
- package/dist/transpiler/filesystemGraph.js +267 -0
- package/dist/transpiler/graph.js +200 -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 +1 -1
- package/package.json +1 -1
|
@@ -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})$/);
|
|
@@ -281,9 +281,12 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
281
281
|
const entryStatements = flattenStatements(entryRecord.doc);
|
|
282
282
|
const inputDeclarations = entryStatements.filter(statement => statement.type === 'inputDeclaration');
|
|
283
283
|
const entryTypes = new Map(entryRecord.importedTypes);
|
|
284
|
+
const entryEnums = new Map(entryRecord.importedEnums);
|
|
284
285
|
for (const statement of entryStatements) {
|
|
285
286
|
if (statement.type === 'typeDeclaration')
|
|
286
287
|
entryTypes.set(statement.name, statement);
|
|
288
|
+
else if (statement.type === 'enumDeclaration')
|
|
289
|
+
entryEnums.set(statement.name, statement);
|
|
287
290
|
}
|
|
288
291
|
const outputs = prepareOutputs(options.outputMappings, entryRecord.checkResult.exportedBindings, entryTypes, options.reservedOutputPath, registry);
|
|
289
292
|
const outputSchemas = options.inputInspection || options.outputInspection
|
|
@@ -299,7 +302,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
299
302
|
inputName: options.inputInspection.name
|
|
300
303
|
}]);
|
|
301
304
|
}
|
|
302
|
-
const validation = validateInputText(options.inputInspection.text, options.inputInspection.format, declaration, entryTypes, options.validation ?? 'aggregate', { file: entryPath }, registry);
|
|
305
|
+
const validation = validateInputText(options.inputInspection.text, options.inputInspection.format, declaration, entryTypes, options.validation ?? 'aggregate', { file: entryPath }, registry, entryEnums);
|
|
303
306
|
return {
|
|
304
307
|
doc: entryRecord.doc,
|
|
305
308
|
env: new Environment(entryTypes, new Map(), registry),
|
|
@@ -308,7 +311,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
308
311
|
diagramEmissions: [],
|
|
309
312
|
registry,
|
|
310
313
|
inputInspection: {
|
|
311
|
-
schema: describeInputSchema(declaration, entryTypes, registry),
|
|
314
|
+
schema: describeInputSchema(declaration, entryTypes, registry, entryEnums),
|
|
312
315
|
valid: validation.diagnostics.length === 0,
|
|
313
316
|
diagnostics: validation.diagnostics,
|
|
314
317
|
outputs: outputSchemas ?? []
|
|
@@ -326,7 +329,7 @@ export async function loadEntryModule(entryPath, options = {}) {
|
|
|
326
329
|
registry
|
|
327
330
|
};
|
|
328
331
|
}
|
|
329
|
-
const inputValues = await loadInputValues(inputDeclarations, entryTypes, options.inputMappings, options.validation, entryPath, registry);
|
|
332
|
+
const inputValues = await loadInputValues(inputDeclarations, entryTypes, options.inputMappings, options.validation, entryPath, registry, entryEnums);
|
|
330
333
|
const evaluatedEnvironments = new Map();
|
|
331
334
|
for (const canonicalPath of evaluationOrder) {
|
|
332
335
|
const record = resolved.get(canonicalPath);
|