type-crafter 0.16.1 → 0.18.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') {
@@ -21534,7 +21560,8 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21534
21560
  valuePrimitiveType: 'unknown',
21535
21561
  valueComposerType: null
21536
21562
  },
21537
- primitives: unknownType.primitives
21563
+ primitives: unknownType.primitives,
21564
+ references: new Set()
21538
21565
  };
21539
21566
  }
21540
21567
  else if (valueIsKeyedAdditionalProperties(typeInfo.additionalProperties)) {
@@ -21558,7 +21585,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21558
21585
  ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21559
21586
  : 0
21560
21587
  },
21561
- primitives: generatedValueType.primitives
21588
+ primitives: generatedValueType.primitives,
21589
+ references: isReferenced
21590
+ ? new Set([generatedValueType.templateInput.typeName])
21591
+ : generatedValueType.references
21562
21592
  };
21563
21593
  }
21564
21594
  else if (valueIsTypeInfo(typeInfo.additionalProperties)) {
@@ -21579,7 +21609,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21579
21609
  ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21580
21610
  : 0
21581
21611
  },
21582
- primitives: generatedValueType.primitives
21612
+ primitives: generatedValueType.primitives,
21613
+ references: isReferenced
21614
+ ? new Set([generatedValueType.templateInput.typeName])
21615
+ : generatedValueType.references
21583
21616
  };
21584
21617
  }
21585
21618
  return null;
@@ -21678,6 +21711,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21678
21711
  if (additionalPropertiesResult !== null) {
21679
21712
  templateInput.additionalProperties = additionalPropertiesResult.templateInput;
21680
21713
  primitives.push(...additionalPropertiesResult.primitives);
21714
+ references.push(...additionalPropertiesResult.references);
21681
21715
  }
21682
21716
  const result = {
21683
21717
  content: Runtime.getObjectTemplate()(templateInput) +
@@ -22547,7 +22581,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
22547
22581
  }
22548
22582
  }
22549
22583
  greeting();
22550
- const program = new Command().version('0.16.1');
22584
+ const program = new Command().version('0.18.0');
22551
22585
  program
22552
22586
  .command('generate')
22553
22587
  .description('Generate types for your language from a type spec file')
@@ -1,7 +1,10 @@
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
+ {{#each customAttributes.[x-attributes]}}
6
+ #[{{{this}}}]
7
+ {{/each}}
5
8
  pub struct {{typeName}} {
6
9
  {{#each compositions}}
7
10
  {{#if (eq this.source 'referenced')}}
@@ -4,7 +4,10 @@
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
+ {{#each customAttributes.[x-attributes]}}
9
+ #[{{{this}}}]
10
+ {{/each}}
8
11
  pub enum {{typeName}} {
9
12
  {{#each values}}
10
13
  {{#if (eq ../type 'string')}}
@@ -4,7 +4,10 @@
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
+ {{#each customAttributes.[x-attributes]}}
9
+ #[{{{this}}}]
10
+ {{/each}}
8
11
  {{#if customAttributes.renameAll}}
9
12
  #[serde(rename_all = "{{{customAttributes.renameAll}}}")]
10
13
  {{/if}}
@@ -1,7 +1,10 @@
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
+ {{#each customAttributes.[x-attributes]}}
6
+ #[{{{this}}}]
7
+ {{/each}}
5
8
  #[serde(untagged)]
6
9
  pub enum {{typeName}} {
7
10
  {{#each compositions}}
@@ -144,6 +144,7 @@ export type AdditionalPropertiesTemplateInput = {
144
144
  export type AdditionalPropertiesGenerationResult = {
145
145
  templateInput: AdditionalPropertiesTemplateInput;
146
146
  primitives: Set<string>;
147
+ references: Set<string>;
147
148
  };
148
149
  export type ExporterModuleTemplateInput = {
149
150
  modules: string[];
@@ -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.1",
3
+ "version": "0.18.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",