cddl2py 0.1.2 → 0.2.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/build/index.d.ts.map +1 -1
- package/build/index.js +61 -1
- package/package.json +2 -2
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKH,KAAK,UAAU,EAGlB,MAAM,MAAM,CAAA;AAKb,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACrB;AAkBD,wBAAgB,SAAS,CAAE,WAAW,EAAE,UAAU,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAuBxF"}
|
package/build/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isCDDLArray, isGroup, isNamedGroupReference, isLiteralWithValue, isNativeTypeWithOperator, isUnNamedProperty, isPropertyReference, isRange, isVariable, pascalCase } from 'cddl';
|
|
1
|
+
import { getRegexpPattern, isCDDLArray, isGroup, isNamedGroupReference, isLiteralWithValue, isNativeTypeWithOperator, isUnNamedProperty, isPropertyReference, isRange, isVariable, pascalCase } from 'cddl';
|
|
2
2
|
import { snakeCase } from './utils.js';
|
|
3
3
|
import { pkg, NATIVE_TYPE_MAP } from './constants.js';
|
|
4
4
|
const STRING_RECORD_KEY_TYPES = new Set(['str', 'text', 'tstr']);
|
|
@@ -416,6 +416,59 @@ function getExtraItemsType(props, ctx) {
|
|
|
416
416
|
ctx.typingImports.add('Union');
|
|
417
417
|
return `Union[${uniqueTypes.join(', ')}]`;
|
|
418
418
|
}
|
|
419
|
+
function stringifyPythonLiteral(value) {
|
|
420
|
+
return JSON.stringify(value);
|
|
421
|
+
}
|
|
422
|
+
function getTemplateAnnotatedPattern(regexpPattern) {
|
|
423
|
+
const wildcard = '.+';
|
|
424
|
+
if (!regexpPattern.includes(wildcard) || /[\\()[\]{}|?*^$]/.test(regexpPattern.replaceAll(wildcard, ''))) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const segments = regexpPattern.split(wildcard);
|
|
428
|
+
const parts = [];
|
|
429
|
+
for (let i = 0; i < segments.length; i++) {
|
|
430
|
+
const segment = segments[i];
|
|
431
|
+
if (segment.length > 0) {
|
|
432
|
+
parts.push(stringifyPythonLiteral(segment));
|
|
433
|
+
}
|
|
434
|
+
if (i < segments.length - 1) {
|
|
435
|
+
parts.push('str');
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (parts.length === 0 || !parts.includes('str')) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
return `Annotated[str, ${parts.join(' + ')}]`;
|
|
442
|
+
}
|
|
443
|
+
function resolveNativeTypeWithOperator(t, ctx) {
|
|
444
|
+
if (typeof t.Type !== 'string') {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const mapped = NATIVE_TYPE_MAP[t.Type];
|
|
448
|
+
if (!mapped) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const regexpPattern = getRegexpPattern(t);
|
|
452
|
+
if (!regexpPattern) {
|
|
453
|
+
if (mapped === 'Any') {
|
|
454
|
+
ctx.typingImports.add('Any');
|
|
455
|
+
}
|
|
456
|
+
return mapped;
|
|
457
|
+
}
|
|
458
|
+
const templateAnnotatedPattern = getTemplateAnnotatedPattern(regexpPattern);
|
|
459
|
+
if (!templateAnnotatedPattern) {
|
|
460
|
+
if (mapped === 'Any') {
|
|
461
|
+
ctx.typingImports.add('Any');
|
|
462
|
+
}
|
|
463
|
+
return mapped;
|
|
464
|
+
}
|
|
465
|
+
ctx.typingImports.add('Annotated');
|
|
466
|
+
if (ctx.pydantic) {
|
|
467
|
+
ctx.pydanticImports.add('StringConstraints');
|
|
468
|
+
return `Annotated[${mapped}, StringConstraints(pattern=${JSON.stringify(regexpPattern)})]`;
|
|
469
|
+
}
|
|
470
|
+
return templateAnnotatedPattern;
|
|
471
|
+
}
|
|
419
472
|
// ---------------------------------------------------------------------------
|
|
420
473
|
// Type resolution
|
|
421
474
|
// ---------------------------------------------------------------------------
|
|
@@ -430,6 +483,13 @@ function resolveType(t, ctx, options = {}) {
|
|
|
430
483
|
}
|
|
431
484
|
throw new Error(`Unknown native type: "${t}"`);
|
|
432
485
|
}
|
|
486
|
+
if (isNativeTypeWithOperator(t) && typeof t.Type === 'string') {
|
|
487
|
+
const resolved = resolveNativeTypeWithOperator(t, ctx);
|
|
488
|
+
if (resolved) {
|
|
489
|
+
return resolved;
|
|
490
|
+
}
|
|
491
|
+
throw new Error(`Unknown native type with operator: ${JSON.stringify(t)}`);
|
|
492
|
+
}
|
|
433
493
|
if (t.Type && typeof t.Type === 'string' && NATIVE_TYPE_MAP[t.Type]) {
|
|
434
494
|
const mapped = NATIVE_TYPE_MAP[t.Type];
|
|
435
495
|
if (mapped === 'Any') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cddl2py",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A Node.js package that can generate Python type definitions (with optional Pydantic support) based on a CDDL file",
|
|
5
5
|
"author": "Christian Bromann <mail@bromann.dev>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"yargs": "^18.0.0",
|
|
37
|
-
"cddl": "0.
|
|
37
|
+
"cddl": "0.20.0"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"release": "release-it --config .release-it.ts --VV",
|