type-crafter 0.16.0 → 0.17.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
@@ -21281,6 +21281,31 @@ function escapeReservedWord(input) {
21281
21281
  }
21282
21282
  return input;
21283
21283
  }
21284
+ function appendUnique(base, additions) {
21285
+ const seen = new Set();
21286
+ const result = [];
21287
+ const add = (item) => {
21288
+ if (typeof item !== 'string') {
21289
+ return;
21290
+ }
21291
+ const trimmed = item.trim();
21292
+ if (trimmed.length === 0 || seen.has(trimmed)) {
21293
+ return;
21294
+ }
21295
+ seen.add(trimmed);
21296
+ result.push(trimmed);
21297
+ };
21298
+ if (typeof base === 'string') {
21299
+ base.split(',').forEach(add);
21300
+ }
21301
+ if (Array.isArray(additions)) {
21302
+ additions.forEach(add);
21303
+ }
21304
+ else {
21305
+ add(additions);
21306
+ }
21307
+ return result.join(', ');
21308
+ }
21284
21309
  function registerTemplateHelpers() {
21285
21310
  Handlebars.registerHelper('getOptionalKeys', getOptionalKeys);
21286
21311
  Handlebars.registerHelper('getRequiredKeys', getRequiredKeys);
@@ -21297,6 +21322,7 @@ function registerTemplateHelpers() {
21297
21322
  Handlebars.registerHelper('variableName', refineVariableName);
21298
21323
  Handlebars.registerHelper('indexKey', refineIndexKey);
21299
21324
  Handlebars.registerHelper('escapeReservedWord', escapeReservedWord);
21325
+ Handlebars.registerHelper('appendUnique', appendUnique);
21300
21326
  Handlebars.registerHelper('stringify', (value) => JSON.stringify(value));
21301
21327
  Handlebars.registerHelper('not', (value) => {
21302
21328
  if (typeof value === 'boolean') {
@@ -22547,7 +22573,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
22547
22573
  }
22548
22574
  }
22549
22575
  greeting();
22550
- const program = new Command().version('0.16.0');
22576
+ const program = new Command().version('0.17.0');
22551
22577
  program
22552
22578
  .command('generate')
22553
22579
  .description('Generate types for your language from a type spec file')
@@ -1,7 +1,7 @@
1
1
  {{#if description}}
2
2
  /// {{{description}}}
3
3
  {{/if}}
4
- #[derive(Debug, Clone, Serialize, Deserialize)]
4
+ #[derive({{{appendUnique "Debug, Clone, Serialize, Deserialize" customAttributes.[x-derive]}}})]
5
5
  pub struct {{typeName}} {
6
6
  {{#each compositions}}
7
7
  {{#if (eq this.source 'referenced')}}
@@ -4,7 +4,7 @@
4
4
  {{#if example}}
5
5
  /// Example: {{{example}}}
6
6
  {{/if}}
7
- #[derive(Debug, Clone, Serialize, Deserialize)]
7
+ #[derive({{{appendUnique "Debug, Clone, Serialize, Deserialize" customAttributes.[x-derive]}}})]
8
8
  pub enum {{typeName}} {
9
9
  {{#each values}}
10
10
  {{#if (eq ../type 'string')}}
@@ -4,7 +4,7 @@
4
4
  {{#if example}}
5
5
  /// Example: {{{example}}}
6
6
  {{/if}}
7
- #[derive(Debug, Clone, Serialize, Deserialize)]
7
+ #[derive({{{appendUnique "Debug, Clone, Serialize, Deserialize" customAttributes.[x-derive]}}})]
8
8
  {{#if customAttributes.renameAll}}
9
9
  #[serde(rename_all = "{{{customAttributes.renameAll}}}")]
10
10
  {{/if}}
@@ -1,7 +1,7 @@
1
1
  {{#if description}}
2
2
  /// {{{description}}}
3
3
  {{/if}}
4
- #[derive(Debug, Clone, Serialize, Deserialize)]
4
+ #[derive({{{appendUnique "Debug, Clone, Serialize, Deserialize" customAttributes.[x-derive]}}})]
5
5
  #[serde(untagged)]
6
6
  pub enum {{typeName}} {
7
7
  {{#each compositions}}
@@ -50,7 +50,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
50
50
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
51
51
  {{~else~}}
52
52
  {{~#if (eq this.primitiveType 'array')~}}
53
- decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}})
53
+ decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}}){{#if this.optional}} ?? undefined{{/if}}
54
54
  {{~else~}}
55
55
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
56
56
  {{~/if~}};
@@ -85,9 +85,11 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
85
85
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
86
86
  {{/if}}
87
87
  {{/if}}
88
+ {{#unless (eq additionalProperties.valuePrimitiveType 'unknown')}}
88
89
  if (decodedValue === null) {
89
90
  return null;
90
91
  }
92
+ {{/unless}}
91
93
  decodedAdditionalProperties[key] = decodedValue;
92
94
  }
93
95
  return decodedAdditionalProperties;
@@ -109,9 +111,11 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
109
111
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
110
112
  {{/if}}
111
113
  {{/if}}
114
+ {{#unless (eq additionalProperties.valuePrimitiveType 'unknown')}}
112
115
  if (decodedValue === null) {
113
116
  return null;
114
117
  }
118
+ {{/unless}}
115
119
  decodedAdditionalProperties[key] = decodedValue;
116
120
  }
117
121
  }
@@ -177,9 +181,11 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
177
181
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
178
182
  {{/if}}
179
183
  {{/if}}
184
+ {{#unless (eq additionalProperties.valuePrimitiveType 'unknown')}}
180
185
  if (decodedValue === null) {
181
186
  return;
182
187
  }
188
+ {{/unless}}
183
189
  decodedAdditionalProperties[key] = decodedValue;
184
190
  }
185
191
  return decodedAdditionalProperties;
@@ -201,9 +207,11 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
201
207
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
202
208
  {{/if}}
203
209
  {{/if}}
210
+ {{#unless (eq additionalProperties.valuePrimitiveType 'unknown')}}
204
211
  if (decodedValue === null) {
205
212
  return;
206
213
  }
214
+ {{/unless}}
207
215
  decodedAdditionalProperties[key] = decodedValue;
208
216
  }
209
217
  }
@@ -1,7 +1,7 @@
1
1
  {{#each (getReferencedTypeModules referencedTypes writtenAt)}}
2
2
  import { {{#each this.referencedTypes}}type {{this}}, decode{{this}}{{#unless @last}}, {{/unless}} {{/each}} } from '{{this.moduleRelativePath}}';
3
3
  {{/each}}
4
- import { isJSON, {{#each primitives}}decode{{{toPascalCase this}}}, _decode{{{toPascalCase this}}} {{#unless @last}}, {{/unless}}{{/each}} } from 'type-decoder';
4
+ import { isJSON, {{#each primitives}}decode{{{toPascalCase this}}}{{#unless (eq this 'unknown')}}, _decode{{{toPascalCase this}}}{{/unless}} {{#unless @last}}, {{/unless}}{{/each}} } from 'type-decoder';
5
5
 
6
6
  {{{typesContent}}}
7
7
 
@@ -17,6 +17,7 @@ export declare function refineJSONKey(input: unknown): unknown;
17
17
  export declare function refineVariableName(input: unknown): unknown;
18
18
  export declare function refineIndexKey(input: unknown): unknown;
19
19
  export declare function escapeReservedWord(input: unknown): unknown;
20
+ export declare function appendUnique(base: unknown, additions: unknown): string;
20
21
  export declare function registerTemplateHelpers(): void;
21
22
  export declare function readNestedValue(json: unknown, keyPath: string[]): JSONObject;
22
23
  export declare function generateRelativePath(fromPath: string, toPath: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.16.0",
3
+ "version": "0.17.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",