type-crafter 0.14.0 → 0.15.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/dist/index.js CHANGED
@@ -21308,6 +21308,7 @@ function registerTemplateHelpers() {
21308
21308
  return value1 || value2;
21309
21309
  }
21310
21310
  });
21311
+ Handlebars.registerHelper('subtract', (a, b) => typeof a === 'number' && typeof b === 'number' ? a - b : 0);
21311
21312
  }
21312
21313
  function readNestedValue(json, keyPath) {
21313
21314
  if (!isJSON(json)) {
@@ -21552,7 +21553,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21552
21553
  valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21553
21554
  valueComposerType: 'composerType' in generatedValueType.templateInput
21554
21555
  ? (generatedValueType.templateInput.composerType ?? null)
21555
- : null
21556
+ : null,
21557
+ valueArrayNestingDepth: 'arrayNestingDepth' in generatedValueType.templateInput
21558
+ ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21559
+ : 0
21556
21560
  },
21557
21561
  primitives: generatedValueType.primitives
21558
21562
  };
@@ -21570,7 +21574,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21570
21574
  valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21571
21575
  valueComposerType: 'composerType' in generatedValueType.templateInput
21572
21576
  ? (generatedValueType.templateInput.composerType ?? null)
21573
- : null
21577
+ : null,
21578
+ valueArrayNestingDepth: 'arrayNestingDepth' in generatedValueType.templateInput
21579
+ ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21580
+ : 0
21574
21581
  },
21575
21582
  primitives: generatedValueType.primitives
21576
21583
  };
@@ -21606,6 +21613,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21606
21613
  }
21607
21614
  const primitiveType = propertyType ?? 'object';
21608
21615
  let composerType = null;
21616
+ let arrayNestingDepth = 0;
21609
21617
  let recursivePropertyName;
21610
21618
  let languageDataType = null;
21611
21619
  let isReferenced = false;
@@ -21627,6 +21635,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21627
21635
  references.push(...arrayDataGenOutput.references);
21628
21636
  languageDataType = arrayDataGenOutput.templateInput.type;
21629
21637
  composerType = arrayDataGenOutput.templateInput.composerType ?? null;
21638
+ arrayNestingDepth = arrayDataGenOutput.templateInput.arrayNestingDepth ?? 0;
21630
21639
  dynamicGeneratedType += arrayDataGenOutput.content;
21631
21640
  }
21632
21641
  else if (propertyType === 'object') {
@@ -21655,6 +21664,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21655
21664
  referenced: isReferenced,
21656
21665
  primitiveType,
21657
21666
  composerType,
21667
+ arrayNestingDepth,
21658
21668
  example: propertyDetails.example,
21659
21669
  description: propertyDetails.description,
21660
21670
  summary: propertyDetails.summary,
@@ -21756,6 +21766,13 @@ async function generateArrayType(typeName, typeInfo, parentTypes) {
21756
21766
  throw new InvalidSpecFileError('Invalid array type for: ' + typeName);
21757
21767
  }
21758
21768
  const dataType = fillPatterns(arrayTypeMap, fillerPatterns);
21769
+ const innerComposerType = 'composerType' in arrayItemsType.templateInput
21770
+ ? (arrayItemsType.templateInput.composerType ?? arrayItemsType.templateInput.type)
21771
+ : arrayItemsType.templateInput.type;
21772
+ const innerNestingDepth = 'arrayNestingDepth' in arrayItemsType.templateInput
21773
+ ? (arrayItemsType.templateInput.arrayNestingDepth ?? 0)
21774
+ : 0;
21775
+ const arrayNestingDepth = typeInfo.items?.type === 'array' ? innerNestingDepth + 1 : 0;
21759
21776
  const result = {
21760
21777
  content: dynamicGeneratedType,
21761
21778
  references: arrayItemsType.references,
@@ -21763,7 +21780,8 @@ async function generateArrayType(typeName, typeInfo, parentTypes) {
21763
21780
  templateInput: {
21764
21781
  typeName,
21765
21782
  type: dataType,
21766
- composerType: arrayItemsType.templateInput.type,
21783
+ composerType: innerComposerType,
21784
+ arrayNestingDepth,
21767
21785
  description: typeInfo.description,
21768
21786
  example: typeInfo.example,
21769
21787
  summary: typeInfo.summary
@@ -22526,7 +22544,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
22526
22544
  }
22527
22545
  }
22528
22546
  greeting();
22529
- const program = new Command().version('0.14.0');
22547
+ const program = new Command().version('0.15.0');
22530
22548
  program
22531
22549
  .command('generate')
22532
22550
  .description('Generate types for your language from a type spec file')
@@ -1,3 +1,10 @@
1
+ {{#*inline "nestedArrayDecoder"}}
2
+ {{~#if depth~}}
3
+ (item: unknown) => decodeArray(item, {{~> nestedArrayDecoder depth=(subtract depth 1) composerType=composerType~}})
4
+ {{~else~}}
5
+ decode{{{toPascalCase composerType}}}
6
+ {{~/if~}}
7
+ {{/inline}}
1
8
  /**
2
9
  * @type { {{typeName}} }
3
10
  {{#if description}}
@@ -42,8 +49,8 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
42
49
  const decoded{{variableName @key}} = {{#if this.referenced ~}}
43
50
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
44
51
  {{~else~}}
45
- {{~#if (eq this.primitiveType 'array')~}}
46
- decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}})
52
+ {{~#if (eq this.primitiveType 'array')~}}
53
+ decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}})
47
54
  {{~else~}}
48
55
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
49
56
  {{~/if~}};
@@ -67,13 +74,13 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
67
74
  for (const key in rawInput) {
68
75
  {{#if additionalProperties.valueTypeReferenced}}
69
76
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
70
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
77
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
71
78
  {{~else~}}
72
79
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
73
80
  {{~/if}}
74
81
  {{else}}
75
82
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
76
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
83
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
77
84
  {{else}}
78
85
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
79
86
  {{/if}}
@@ -91,13 +98,13 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
91
98
  if (!knownKeys.has(key)) {
92
99
  {{#if additionalProperties.valueTypeReferenced}}
93
100
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
94
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
101
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
95
102
  {{~else~}}
96
103
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
97
104
  {{~/if}}
98
105
  {{else}}
99
106
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
100
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
107
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
101
108
  {{else}}
102
109
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
103
110
  {{/if}}
@@ -135,7 +142,7 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
135
142
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
136
143
  {{~else~}}
137
144
  {{~#if (eq this.primitiveType 'array')~}}
138
- decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}})
145
+ decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}})
139
146
  {{~else~}}
140
147
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
141
148
  {{~/if~}};
@@ -159,13 +166,13 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
159
166
  for (const key in rawInput) {
160
167
  {{#if additionalProperties.valueTypeReferenced}}
161
168
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
162
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
169
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
163
170
  {{~else~}}
164
171
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
165
172
  {{~/if}}
166
173
  {{else}}
167
174
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
168
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
175
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
169
176
  {{else}}
170
177
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
171
178
  {{/if}}
@@ -183,13 +190,13 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
183
190
  if (!knownKeys.has(key)) {
184
191
  {{#if additionalProperties.valueTypeReferenced}}
185
192
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
186
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
193
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
187
194
  {{~else~}}
188
195
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
189
196
  {{~/if}}
190
197
  {{else}}
191
198
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
192
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
199
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
193
200
  {{else}}
194
201
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
195
202
  {{/if}}
@@ -1,3 +1,10 @@
1
+ {{#*inline "nestedArrayDecoder"}}
2
+ {{~#if depth~}}
3
+ (item: unknown) => decodeArray(item, {{~> nestedArrayDecoder depth=(subtract depth 1) composerType=composerType~}})
4
+ {{~else~}}
5
+ decode{{{toPascalCase composerType}}}
6
+ {{~/if~}}
7
+ {{/inline}}
1
8
  export type {{typeName}} =
2
9
  {{#each compositions}}
3
10
  | {{#if (eq this.source 'referenced')}}C{{../typeName}}{{referencedType}}
@@ -20,7 +27,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
20
27
  {{else if (eq this.dataType 'object')}}
21
28
  decodeC{{this.templateInput.typeName}}(rawInput)
22
29
  {{else if (eq this.dataType 'array')}}
23
- decodeArray(rawInput, decode{{{toPascalCase this.templateInput.composerType}}})
30
+ decodeArray(rawInput, {{~> nestedArrayDecoder depth=this.templateInput.arrayNestingDepth composerType=this.templateInput.composerType~}})
24
31
  {{else if this.templateInput.values}}
25
32
  decode{{this.templateInput.typeName}}(rawInput)
26
33
  {{else}}
@@ -129,6 +129,7 @@ export type ObjectTemplateInputProperty = TypeDescriptors & {
129
129
  referenced: boolean;
130
130
  primitiveType: string;
131
131
  composerType: string | null;
132
+ arrayNestingDepth?: number;
132
133
  optional: boolean;
133
134
  customAttributes?: Record<string, unknown> | null;
134
135
  };
@@ -138,6 +139,7 @@ export type AdditionalPropertiesTemplateInput = {
138
139
  valueTypeReferenced: boolean;
139
140
  valuePrimitiveType: string;
140
141
  valueComposerType: string | null;
142
+ valueArrayNestingDepth?: number;
141
143
  };
142
144
  export type AdditionalPropertiesGenerationResult = {
143
145
  templateInput: AdditionalPropertiesTemplateInput;
@@ -192,6 +194,7 @@ export type VariableTemplateInput = TypeDescriptors & {
192
194
  typeName: string;
193
195
  type: string;
194
196
  composerType?: string;
197
+ arrayNestingDepth?: number;
195
198
  };
196
199
  export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | AllOfTemplateInput | VariableTemplateInput;
197
200
  export type GenerationResult = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "A tool to generate types from a yaml schema for any language",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",