@goast/kotlin 0.1.2 → 0.1.4

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.
@@ -50,19 +50,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
50
50
  }
51
51
  }
52
52
  generateFileContent(ctx, builder) {
53
- let schema = ctx.schema;
54
- if (schema.kind === 'oneOf') {
55
- schema =
56
- ctx.config.oneOfBehavior === 'treat-as-any-of'
57
- ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
58
- ctx.schema = schema;
59
- }
60
- if (schema.kind === 'object' || schema.kind === 'combined') {
61
- const mergedSchema = (0, core_1.resolveAnyOfAndAllOf)(schema, true);
62
- if (mergedSchema) {
63
- schema = mergedSchema;
64
- }
65
- }
53
+ const schema = this.normalizeSchema(ctx, ctx.schema);
66
54
  if (schema.kind === 'object') {
67
55
  this.generateObjectType(ctx, builder, schema);
68
56
  }
@@ -158,7 +146,8 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
158
146
  .appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
159
147
  .append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
160
148
  .append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
161
- .if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')), { separator: ',\n' }), { multiline: true })
149
+ .if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?'))
150
+ .appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema)), { separator: ',\n' }), { multiline: true })
162
151
  .if(inheritedSchemas.length > 0, (builder) => builder
163
152
  .append(' : ')
164
153
  .forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
@@ -187,6 +176,28 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
187
176
  .append(' ')
188
177
  .parenthesize('{}', 'return this.additionalProperties', { multiline: true })), { multiline: true });
189
178
  }
179
+ generateDefaultValue(ctx, builder, schema) {
180
+ if (schema.default === null || schema.default === undefined) {
181
+ builder.append('null');
182
+ }
183
+ else {
184
+ switch (schema.kind) {
185
+ case 'boolean':
186
+ builder.append(Boolean(schema.default) || String(schema.default).toLowerCase() === 'true' ? 'true' : 'false');
187
+ break;
188
+ case 'integer':
189
+ case 'number':
190
+ builder.append(String(schema.default));
191
+ break;
192
+ case 'string':
193
+ builder.append(this.toStringLiteral(ctx, String(schema.default)));
194
+ break;
195
+ default:
196
+ builder.append('null');
197
+ break;
198
+ }
199
+ }
200
+ }
190
201
  generateObjectDataClassParameterAnnotations(ctx, builder, schema, property) {
191
202
  this.generatePropertyValidationAnnotations(ctx, builder, schema, property);
192
203
  this.generatePropertySchemaAnnotation(ctx, builder, schema, property);
@@ -267,7 +278,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
267
278
  }
268
279
  break;
269
280
  case 'null':
270
- builder.append('null');
281
+ builder.append('Nothing');
271
282
  break;
272
283
  case 'unknown':
273
284
  builder.append('Any');
@@ -370,7 +381,11 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
370
381
  return false;
371
382
  }
372
383
  // Too complex types cannot be represented in Kotlin, so they fallback to Any
373
- if (schema.kind === 'combined' || schema.kind === 'multi-type' || schema.kind === 'oneOf') {
384
+ if (schema.kind === 'multi-type') {
385
+ return false;
386
+ }
387
+ schema = this.normalizeSchema(ctx, schema);
388
+ if (schema.kind === 'combined' || schema.kind === 'oneOf') {
374
389
  return false;
375
390
  }
376
391
  // Only object types with properties should have its own type declaration
@@ -392,6 +407,21 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
392
407
  return bRequired - aRequired;
393
408
  });
394
409
  }
410
+ normalizeSchema(ctx, schema) {
411
+ if (schema.kind === 'oneOf') {
412
+ schema =
413
+ ctx.config.oneOfBehavior === 'treat-as-any-of'
414
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
415
+ ctx.schema = schema;
416
+ }
417
+ if (schema.kind === 'object' || schema.kind === 'combined') {
418
+ const mergedSchema = (0, core_1.resolveAnyOfAndAllOf)(schema, true);
419
+ if (mergedSchema) {
420
+ schema = mergedSchema;
421
+ }
422
+ }
423
+ return schema;
424
+ }
395
425
  hasProperty(ctx, schema, name) {
396
426
  return (('properties' in schema && schema.properties.has(name)) ||
397
427
  ('anyOf' in schema && schema.anyOf.some((x) => this.hasProperty(ctx, x, name))) ||
@@ -47,19 +47,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
47
47
  }
48
48
  }
49
49
  generateFileContent(ctx, builder) {
50
- let schema = ctx.schema;
51
- if (schema.kind === 'oneOf') {
52
- schema =
53
- ctx.config.oneOfBehavior === 'treat-as-any-of'
54
- ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
55
- ctx.schema = schema;
56
- }
57
- if (schema.kind === 'object' || schema.kind === 'combined') {
58
- const mergedSchema = resolveAnyOfAndAllOf(schema, true);
59
- if (mergedSchema) {
60
- schema = mergedSchema;
61
- }
62
- }
50
+ const schema = this.normalizeSchema(ctx, ctx.schema);
63
51
  if (schema.kind === 'object') {
64
52
  this.generateObjectType(ctx, builder, schema);
65
53
  }
@@ -155,7 +143,8 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
155
143
  .appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
156
144
  .append(`val ${toCasing(property.name, 'camel')}: `)
157
145
  .append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
158
- .if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')), { separator: ',\n' }), { multiline: true })
146
+ .if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?'))
147
+ .appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema)), { separator: ',\n' }), { multiline: true })
159
148
  .if(inheritedSchemas.length > 0, (builder) => builder
160
149
  .append(' : ')
161
150
  .forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
@@ -184,6 +173,28 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
184
173
  .append(' ')
185
174
  .parenthesize('{}', 'return this.additionalProperties', { multiline: true })), { multiline: true });
186
175
  }
176
+ generateDefaultValue(ctx, builder, schema) {
177
+ if (schema.default === null || schema.default === undefined) {
178
+ builder.append('null');
179
+ }
180
+ else {
181
+ switch (schema.kind) {
182
+ case 'boolean':
183
+ builder.append(Boolean(schema.default) || String(schema.default).toLowerCase() === 'true' ? 'true' : 'false');
184
+ break;
185
+ case 'integer':
186
+ case 'number':
187
+ builder.append(String(schema.default));
188
+ break;
189
+ case 'string':
190
+ builder.append(this.toStringLiteral(ctx, String(schema.default)));
191
+ break;
192
+ default:
193
+ builder.append('null');
194
+ break;
195
+ }
196
+ }
197
+ }
187
198
  generateObjectDataClassParameterAnnotations(ctx, builder, schema, property) {
188
199
  this.generatePropertyValidationAnnotations(ctx, builder, schema, property);
189
200
  this.generatePropertySchemaAnnotation(ctx, builder, schema, property);
@@ -264,7 +275,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
264
275
  }
265
276
  break;
266
277
  case 'null':
267
- builder.append('null');
278
+ builder.append('Nothing');
268
279
  break;
269
280
  case 'unknown':
270
281
  builder.append('Any');
@@ -367,7 +378,11 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
367
378
  return false;
368
379
  }
369
380
  // Too complex types cannot be represented in Kotlin, so they fallback to Any
370
- if (schema.kind === 'combined' || schema.kind === 'multi-type' || schema.kind === 'oneOf') {
381
+ if (schema.kind === 'multi-type') {
382
+ return false;
383
+ }
384
+ schema = this.normalizeSchema(ctx, schema);
385
+ if (schema.kind === 'combined' || schema.kind === 'oneOf') {
371
386
  return false;
372
387
  }
373
388
  // Only object types with properties should have its own type declaration
@@ -389,6 +404,21 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
389
404
  return bRequired - aRequired;
390
405
  });
391
406
  }
407
+ normalizeSchema(ctx, schema) {
408
+ if (schema.kind === 'oneOf') {
409
+ schema =
410
+ ctx.config.oneOfBehavior === 'treat-as-any-of'
411
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
412
+ ctx.schema = schema;
413
+ }
414
+ if (schema.kind === 'object' || schema.kind === 'combined') {
415
+ const mergedSchema = resolveAnyOfAndAllOf(schema, true);
416
+ if (mergedSchema) {
417
+ schema = mergedSchema;
418
+ }
419
+ }
420
+ return schema;
421
+ }
392
422
  hasProperty(ctx, schema, name) {
393
423
  return (('properties' in schema && schema.properties.has(name)) ||
394
424
  ('anyOf' in schema && schema.anyOf.some((x) => this.hasProperty(ctx, x, name))) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/kotlin",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -12,7 +12,7 @@
12
12
  "fs-extra": "^11.1.1",
13
13
  "node-fetch": "^2.6.11",
14
14
  "yaml": "^2.3.1",
15
- "@goast/core": "0.1.0",
15
+ "@goast/core": "0.1.1",
16
16
  "tslib": "^2.3.0"
17
17
  },
18
18
  "module": "./esm/index.js",
@@ -19,6 +19,7 @@ export declare class DefaultKotlinModelGenerator extends KotlinFileGenerator<Con
19
19
  protected generateObjectInterfaceSignature(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
20
20
  protected generateObjectInterfaceMembers(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
21
21
  protected generateObjectDataClass(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
22
+ protected generateDefaultValue(ctx: Context, builder: Builder, schema: ApiSchema): void;
22
23
  protected generateObjectDataClassParameterAnnotations(ctx: Context, builder: Builder, schema: ApiSchema, property: ApiSchemaProperty): void;
23
24
  protected generatePropertyValidationAnnotations(ctx: Context, builder: Builder, schema: ApiSchema, property: ApiSchemaProperty): void;
24
25
  protected generatePropertySchemaAnnotation(ctx: Context, builder: Builder, schema: ApiSchema, property: ApiSchemaProperty): void;
@@ -33,6 +34,7 @@ export declare class DefaultKotlinModelGenerator extends KotlinFileGenerator<Con
33
34
  protected shouldGenerateTypeDeclaration(ctx: Context, schema: ApiSchema): boolean;
34
35
  protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
35
36
  protected sortProperties(ctx: Context, schema: ApiSchema, properties: Iterable<ApiSchemaProperty>): ApiSchemaProperty[];
37
+ private normalizeSchema;
36
38
  private hasProperty;
37
39
  }
38
40
  export {};