Skip to content

Commit a48d676

Browse files
committed
Fixed null based kotlin union type into optionals
1 parent 6744a0d commit a48d676

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

rollup.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const handlebarsPlugin = () => ({
3434
notEquals: true,
3535
containsSpaces: true,
3636
union: true,
37+
kotlinUnion: true,
3738
intersection: true,
3839
enumerator: true,
3940
escapeComment: true,

src/templates/partials/typeKotlin.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
{{else equals export 'dictionary'}}
1010
{{>typeKotlinDictionary}}
1111
{{else equals export 'one-of'}}
12-
typeUnion {{>typeUnion}}
12+
typeUnionOneOf {{>typeKotlinUnion}}
1313
{{else equals export 'any-of'}}
14-
typeUnion {{>typeUnion}}
14+
{{>typeKotlinUnion}}
1515
{{else equals export 'all-of'}}
1616
typeIntersection {{>typeIntersection}}
1717
{{else}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{~#kotlinUnion properties parent}}{{this}}{{~/kotlinUnion}}{{>isNullable}}

src/utils/registerHandlebarHelpers.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('registerHandlebarHelpers', () => {
1616
expect(helpers).toContain('notEquals');
1717
expect(helpers).toContain('containsSpaces');
1818
expect(helpers).toContain('union');
19+
expect(helpers).toContain('kotlinUnion');
1920
expect(helpers).toContain('intersection');
2021
expect(helpers).toContain('enumerator');
2122
expect(helpers).toContain('escapeComment');

src/utils/registerHandlebarHelpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ export const registerHandlebarHelpers = (root: {
5555
}
5656
);
5757

58+
Handlebars.registerHelper(
59+
'kotlinUnion',
60+
function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
61+
const type = Handlebars.partials['typeKotlin'];
62+
const types = properties.map(property => type({ ...root, ...property, parent }));
63+
const uniqueTypes = types.filter(unique);
64+
let uniqueTypesString = uniqueTypes.join(' | ');
65+
if (uniqueTypes.includes('null') && uniqueTypes.length === 2) {
66+
uniqueTypesString = `${uniqueTypes.filter(type => type !== 'null')[0]}?`;
67+
} else {
68+
uniqueTypesString = uniqueTypes.join(' | ');
69+
if (uniqueTypes.length > 1) {
70+
uniqueTypesString = `(${uniqueTypesString})`;
71+
}
72+
}
73+
return options.fn(uniqueTypesString);
74+
}
75+
);
76+
5877
Handlebars.registerHelper(
5978
'intersection',
6079
function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import partialTypeKotlinEnum from '../templates/partials/typeKotlinEnum.hbs';
9393
import partialTypeKotlinReference from '../templates/partials/typeKotlinReference.hbs';
9494
import partialTypeReference from '../templates/partials/typeReference.hbs';
9595
import partialTypeUnion from '../templates/partials/typeUnion.hbs';
96+
import partialTypeKotlinUnion from '../templates/partials/typeKotlinUnion.hbs';
9697
import { registerHandlebarHelpers } from './registerHandlebarHelpers';
9798

9899
export interface Templates {
@@ -183,6 +184,7 @@ export const registerHandlebarTemplates = (root: {
183184
Handlebars.registerPartial('typeReference', Handlebars.template(partialTypeReference));
184185
Handlebars.registerPartial('typeKotlinReference', Handlebars.template(partialTypeKotlinReference));
185186
Handlebars.registerPartial('typeUnion', Handlebars.template(partialTypeUnion));
187+
Handlebars.registerPartial('typeKotlinUnion', Handlebars.template(partialTypeKotlinUnion));
186188
Handlebars.registerPartial('typeIntersection', Handlebars.template(partialTypeIntersection));
187189
Handlebars.registerPartial('base', Handlebars.template(partialBase));
188190
Handlebars.registerPartial('baseKotlin', Handlebars.template(partialBaseKotlin));

0 commit comments

Comments
 (0)