cddl2py 0.2.0 → 0.2.2
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.js +119 -35
- package/package.json +2 -2
package/build/index.js
CHANGED
|
@@ -416,30 +416,6 @@ 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
419
|
function resolveNativeTypeWithOperator(t, ctx) {
|
|
444
420
|
if (typeof t.Type !== 'string') {
|
|
445
421
|
return;
|
|
@@ -455,19 +431,12 @@ function resolveNativeTypeWithOperator(t, ctx) {
|
|
|
455
431
|
}
|
|
456
432
|
return mapped;
|
|
457
433
|
}
|
|
458
|
-
const templateAnnotatedPattern = getTemplateAnnotatedPattern(regexpPattern);
|
|
459
|
-
if (!templateAnnotatedPattern) {
|
|
460
|
-
if (mapped === 'Any') {
|
|
461
|
-
ctx.typingImports.add('Any');
|
|
462
|
-
}
|
|
463
|
-
return mapped;
|
|
464
|
-
}
|
|
465
434
|
ctx.typingImports.add('Annotated');
|
|
466
435
|
if (ctx.pydantic) {
|
|
467
436
|
ctx.pydanticImports.add('StringConstraints');
|
|
468
437
|
return `Annotated[${mapped}, StringConstraints(pattern=${JSON.stringify(regexpPattern)})]`;
|
|
469
438
|
}
|
|
470
|
-
return
|
|
439
|
+
return `Annotated[${mapped}, ${JSON.stringify(regexpPattern)}]`;
|
|
471
440
|
}
|
|
472
441
|
// ---------------------------------------------------------------------------
|
|
473
442
|
// Type resolution
|
|
@@ -692,13 +661,47 @@ function orderAssignments(assignments) {
|
|
|
692
661
|
return ordered;
|
|
693
662
|
}
|
|
694
663
|
function getHardDependencies(assignment, assignmentsByName) {
|
|
664
|
+
if (isVariable(assignment)) {
|
|
665
|
+
const propTypes = Array.isArray(assignment.PropertyType)
|
|
666
|
+
? assignment.PropertyType
|
|
667
|
+
: [assignment.PropertyType];
|
|
668
|
+
const deps = new Set();
|
|
669
|
+
for (const t of propTypes) {
|
|
670
|
+
for (const ref of getTypeReferences(t)) {
|
|
671
|
+
if (assignmentsByName.has(ref)) {
|
|
672
|
+
deps.add(ref);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return [...deps];
|
|
677
|
+
}
|
|
678
|
+
if (isCDDLArray(assignment)) {
|
|
679
|
+
const arr = assignment;
|
|
680
|
+
const deps = new Set();
|
|
681
|
+
for (const val of arr.Values) {
|
|
682
|
+
const properties = Array.isArray(val) ? val : [val];
|
|
683
|
+
for (const prop of properties) {
|
|
684
|
+
const types = Array.isArray(prop.Type) ? prop.Type : [prop.Type];
|
|
685
|
+
for (const t of types) {
|
|
686
|
+
for (const ref of getTypeReferences(t)) {
|
|
687
|
+
if (assignmentsByName.has(ref)) {
|
|
688
|
+
deps.add(ref);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return [...deps];
|
|
695
|
+
}
|
|
695
696
|
if (!isGroup(assignment)) {
|
|
696
697
|
return [];
|
|
697
698
|
}
|
|
698
699
|
const deps = new Set();
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
700
|
+
const properties = assignment.Properties;
|
|
701
|
+
const hasChoices = properties.some(p => Array.isArray(p));
|
|
702
|
+
for (const propertyOrChoice of properties) {
|
|
703
|
+
const props = Array.isArray(propertyOrChoice) ? propertyOrChoice : [propertyOrChoice];
|
|
704
|
+
for (const property of props) {
|
|
702
705
|
if (!isUnNamedProperty(property)) {
|
|
703
706
|
continue;
|
|
704
707
|
}
|
|
@@ -707,8 +710,89 @@ function getHardDependencies(assignment, assignmentsByName) {
|
|
|
707
710
|
}
|
|
708
711
|
}
|
|
709
712
|
}
|
|
713
|
+
if (!hasChoices) {
|
|
714
|
+
const props = properties;
|
|
715
|
+
if (props.length === 1 && Object.keys(NATIVE_TYPE_MAP).includes(props[0].Name)) {
|
|
716
|
+
const propType = Array.isArray(props[0].Type) ? props[0].Type : [props[0].Type];
|
|
717
|
+
for (const t of propType) {
|
|
718
|
+
for (const ref of getTypeReferences(t)) {
|
|
719
|
+
if (assignmentsByName.has(ref)) {
|
|
720
|
+
deps.add(ref);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
for (const prop of props) {
|
|
726
|
+
if (isExtensibleRecordProperty(prop)) {
|
|
727
|
+
const cddlTypes = Array.isArray(prop.Type) ? prop.Type : [prop.Type];
|
|
728
|
+
for (const t of cddlTypes) {
|
|
729
|
+
for (const ref of getTypeReferences(t)) {
|
|
730
|
+
if (assignmentsByName.has(ref)) {
|
|
731
|
+
deps.add(ref);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
710
738
|
return [...deps];
|
|
711
739
|
}
|
|
740
|
+
function getTypeReferences(t) {
|
|
741
|
+
if (typeof t === 'string') {
|
|
742
|
+
return [];
|
|
743
|
+
}
|
|
744
|
+
if (isNamedGroupReference(t)) {
|
|
745
|
+
return [pascalCase(t.Value)];
|
|
746
|
+
}
|
|
747
|
+
if (isPropertyReference(t)) {
|
|
748
|
+
const ref = t;
|
|
749
|
+
if ((ref.Type === 'group' || ref.Type === 'group_array') && typeof ref.Value === 'string') {
|
|
750
|
+
return [pascalCase(ref.Value)];
|
|
751
|
+
}
|
|
752
|
+
if (ref.Type === 'tag') {
|
|
753
|
+
const tag = ref.Value;
|
|
754
|
+
if (!NATIVE_TYPE_MAP[tag.TypePart]) {
|
|
755
|
+
return [pascalCase(tag.TypePart)];
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return [];
|
|
759
|
+
}
|
|
760
|
+
if (isNativeTypeWithOperator(t)) {
|
|
761
|
+
if (isNamedGroupReference(t.Type)) {
|
|
762
|
+
return [pascalCase(t.Type.Value)];
|
|
763
|
+
}
|
|
764
|
+
return [];
|
|
765
|
+
}
|
|
766
|
+
if (isGroup(t) && !isNamedGroupReference(t) && t.Properties) {
|
|
767
|
+
const refs = [];
|
|
768
|
+
const group = t;
|
|
769
|
+
for (const prop of group.Properties) {
|
|
770
|
+
const subProps = Array.isArray(prop) ? prop : [prop];
|
|
771
|
+
for (const p of subProps) {
|
|
772
|
+
const types = Array.isArray(p.Type) ? p.Type : [p.Type];
|
|
773
|
+
for (const subType of types) {
|
|
774
|
+
refs.push(...getTypeReferences(subType));
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return refs;
|
|
779
|
+
}
|
|
780
|
+
if (isCDDLArray(t)) {
|
|
781
|
+
const refs = [];
|
|
782
|
+
const arr = t;
|
|
783
|
+
for (const val of arr.Values) {
|
|
784
|
+
const subProps = Array.isArray(val) ? val : [val];
|
|
785
|
+
for (const prop of subProps) {
|
|
786
|
+
const types = Array.isArray(prop.Type) ? prop.Type : [prop.Type];
|
|
787
|
+
for (const subType of types) {
|
|
788
|
+
refs.push(...getTypeReferences(subType));
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return refs;
|
|
793
|
+
}
|
|
794
|
+
return [];
|
|
795
|
+
}
|
|
712
796
|
function getMixinDependencies(type, assignmentsByName) {
|
|
713
797
|
const deps = new Set();
|
|
714
798
|
const values = Array.isArray(type) ? type : [type];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cddl2py",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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.20.
|
|
37
|
+
"cddl": "0.20.1"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"release": "release-it --config .release-it.ts --VV",
|