@goast/kotlin 0.4.18-springwebclient1 → 0.4.19

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.
Files changed (96) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +45 -45
  3. package/assets/client/okhttp3/ApiClient.kt +252 -252
  4. package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
  5. package/esm/src/generators/models/model-generator.js +2 -2
  6. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +28 -28
  7. package/esm/src/generators/services/spring-controllers/spring-controller-generator.js +9 -9
  8. package/esm/src/generators/services/spring-reactive-web-clients/models.d.ts +1 -1
  9. package/esm/src/generators/services/spring-reactive-web-clients/models.d.ts.map +1 -1
  10. package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
  11. package/package.json +2 -2
  12. package/script/src/generators/models/model-generator.js +2 -2
  13. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +28 -28
  14. package/script/src/generators/services/spring-controllers/spring-controller-generator.js +9 -9
  15. package/script/src/generators/services/spring-reactive-web-clients/models.d.ts +1 -1
  16. package/script/src/generators/services/spring-reactive-web-clients/models.d.ts.map +1 -1
  17. package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
  18. package/src/mod.ts +8 -0
  19. package/src/src/assets.ts +9 -0
  20. package/src/src/ast/_index.ts +66 -0
  21. package/src/src/ast/common.ts +1 -0
  22. package/src/src/ast/index.ts +1 -0
  23. package/src/src/ast/node.ts +10 -0
  24. package/src/src/ast/nodes/annotation.ts +79 -0
  25. package/src/src/ast/nodes/argument.ts +62 -0
  26. package/src/src/ast/nodes/call.ts +75 -0
  27. package/src/src/ast/nodes/class.ts +178 -0
  28. package/src/src/ast/nodes/collection-literal.ts +49 -0
  29. package/src/src/ast/nodes/constructor.ts +126 -0
  30. package/src/src/ast/nodes/doc-tag.ts +138 -0
  31. package/src/src/ast/nodes/doc.ts +111 -0
  32. package/src/src/ast/nodes/enum-value.ts +100 -0
  33. package/src/src/ast/nodes/enum.ts +163 -0
  34. package/src/src/ast/nodes/function.ts +178 -0
  35. package/src/src/ast/nodes/generic-parameter.ts +54 -0
  36. package/src/src/ast/nodes/init-block.ts +38 -0
  37. package/src/src/ast/nodes/interface.ts +133 -0
  38. package/src/src/ast/nodes/lambda-type.ts +73 -0
  39. package/src/src/ast/nodes/lambda.ts +74 -0
  40. package/src/src/ast/nodes/object.ts +102 -0
  41. package/src/src/ast/nodes/parameter.ts +118 -0
  42. package/src/src/ast/nodes/property.ts +225 -0
  43. package/src/src/ast/nodes/reference.ts +178 -0
  44. package/src/src/ast/nodes/string.ts +114 -0
  45. package/src/src/ast/nodes/types.ts +23 -0
  46. package/src/src/ast/references/index.ts +10 -0
  47. package/src/src/ast/references/jackson.ts +24 -0
  48. package/src/src/ast/references/jakarta.ts +14 -0
  49. package/src/src/ast/references/java.ts +20 -0
  50. package/src/src/ast/references/kotlin.ts +41 -0
  51. package/src/src/ast/references/kotlinx.ts +14 -0
  52. package/src/src/ast/references/okhttp3.ts +5 -0
  53. package/src/src/ast/references/reactor.ts +5 -0
  54. package/src/src/ast/references/spring-reactive.ts +33 -0
  55. package/src/src/ast/references/spring.ts +86 -0
  56. package/src/src/ast/references/swagger.ts +23 -0
  57. package/src/src/ast/utils/get-kotlin-builder-options.ts +19 -0
  58. package/src/src/ast/utils/to-kt-node.ts +31 -0
  59. package/src/src/ast/utils/write-kt-annotations.ts +15 -0
  60. package/src/src/ast/utils/write-kt-arguments.ts +45 -0
  61. package/src/src/ast/utils/write-kt-enum-values.ts +27 -0
  62. package/src/src/ast/utils/write-kt-generic-parameters.ts +12 -0
  63. package/src/src/ast/utils/write-kt-members.ts +25 -0
  64. package/src/src/ast/utils/write-kt-node.ts +37 -0
  65. package/src/src/ast/utils/write-kt-parameters.ts +25 -0
  66. package/src/src/common-results.ts +4 -0
  67. package/src/src/config.ts +41 -0
  68. package/src/src/file-builder.ts +108 -0
  69. package/src/src/generators/file-generator.ts +29 -0
  70. package/src/src/generators/index.ts +5 -0
  71. package/src/src/generators/models/args.ts +132 -0
  72. package/src/src/generators/models/index.ts +4 -0
  73. package/src/src/generators/models/model-generator.ts +691 -0
  74. package/src/src/generators/models/models-generator.ts +65 -0
  75. package/src/src/generators/models/models.ts +95 -0
  76. package/src/src/generators/services/okhttp3-clients/args.ts +88 -0
  77. package/src/src/generators/services/okhttp3-clients/index.ts +4 -0
  78. package/src/src/generators/services/okhttp3-clients/models.ts +62 -0
  79. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +594 -0
  80. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +130 -0
  81. package/src/src/generators/services/okhttp3-clients/refs.ts +55 -0
  82. package/src/src/generators/services/spring-controllers/args.ts +93 -0
  83. package/src/src/generators/services/spring-controllers/index.ts +4 -0
  84. package/src/src/generators/services/spring-controllers/models.ts +71 -0
  85. package/src/src/generators/services/spring-controllers/refs.ts +17 -0
  86. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +814 -0
  87. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +118 -0
  88. package/src/src/generators/services/spring-reactive-web-clients/args.ts +101 -0
  89. package/src/src/generators/services/spring-reactive-web-clients/index.ts +4 -0
  90. package/src/src/generators/services/spring-reactive-web-clients/models.ts +62 -0
  91. package/src/src/generators/services/spring-reactive-web-clients/refs.ts +11 -0
  92. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +572 -0
  93. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +128 -0
  94. package/src/src/import-collection.ts +98 -0
  95. package/src/src/types.ts +3 -0
  96. package/src/src/utils.ts +39 -0
@@ -0,0 +1,5 @@
1
+ import { type KtGenericReferenceFactory, ktReference } from '../nodes/reference.js';
2
+
3
+ // reactor.core.publisher
4
+ export const flux: KtGenericReferenceFactory<1> = ktReference.genericFactory<1>('Flux', 'reactor.core.publisher');
5
+ export const mono: KtGenericReferenceFactory<1> = ktReference.genericFactory<1>('Mono', 'reactor.core.publisher');
@@ -0,0 +1,33 @@
1
+ import { type KtGenericReferenceFactory, ktReference, type KtReferenceFactory } from '../nodes/reference.js';
2
+
3
+ // org.springframework.web.reactive.function
4
+ export const bodyInserters: KtReferenceFactory = ktReference.factory(
5
+ 'BodyInserters',
6
+ 'org.springframework.web.reactive.function',
7
+ );
8
+
9
+ // org.springframework.web.reactive.function.client
10
+ export const webClient: KtReferenceFactory = ktReference.factory(
11
+ 'WebClient',
12
+ 'org.springframework.web.reactive.function.client',
13
+ );
14
+ export const clientResponse: KtReferenceFactory = ktReference.factory(
15
+ 'ClientResponse',
16
+ 'org.springframework.web.reactive.function.client',
17
+ );
18
+ export const requestHeadersSpec: KtGenericReferenceFactory<1> = ktReference.genericFactory(
19
+ 'RequestHeadersSpec',
20
+ 'org.springframework.web.reactive.function.client.WebClient',
21
+ );
22
+ export const toEntity: KtGenericReferenceFactory<1> = ktReference.genericFactory(
23
+ 'toEntity',
24
+ 'org.springframework.web.reactive.function.client',
25
+ );
26
+ export const awaitBody: KtGenericReferenceFactory<1> = ktReference.genericFactory(
27
+ 'awaitBody',
28
+ 'org.springframework.web.reactive.function.client',
29
+ );
30
+ export const awaitBodilessEntity: KtReferenceFactory = ktReference.factory(
31
+ 'awaitBodilessEntity',
32
+ 'org.springframework.web.reactive.function.client',
33
+ );
@@ -0,0 +1,86 @@
1
+ import { type KtGenericReferenceFactory, ktReference, type KtReferenceFactory } from '../nodes/reference.js';
2
+
3
+ // org.springframework.beans.factory.annotation
4
+ export const autowired: KtReferenceFactory = ktReference.factory(
5
+ 'Autowired',
6
+ 'org.springframework.beans.factory.annotation',
7
+ );
8
+
9
+ // org.springframework.http
10
+ export const httpStatus: KtReferenceFactory = ktReference.factory('HttpStatus', 'org.springframework.http');
11
+ export const httpMethod: KtReferenceFactory = ktReference.factory('HttpMethod', 'org.springframework.http');
12
+ export const responseEntity: KtGenericReferenceFactory<1> = ktReference.genericFactory<1>(
13
+ 'ResponseEntity',
14
+ 'org.springframework.http',
15
+ );
16
+ export const mediaType: KtReferenceFactory = ktReference.factory('MediaType', 'org.springframework.http');
17
+
18
+ // org.springframework.http.client
19
+ export const multipartBodyBuilder: KtReferenceFactory = ktReference.factory(
20
+ 'MultipartBodyBuilder',
21
+ 'org.springframework.http.client',
22
+ );
23
+
24
+ // org.springframework.http.codec.multipart
25
+ export const filePart: KtReferenceFactory = ktReference.factory('FilePart', 'org.springframework.http.codec.multipart');
26
+
27
+ // org.springframework.stereotype
28
+ export const controller: KtReferenceFactory = ktReference.factory('Controller', 'org.springframework.stereotype');
29
+
30
+ // org.springframework.util
31
+ export const multiValueMap: KtGenericReferenceFactory<2> = ktReference.genericFactory<2>(
32
+ 'MultiValueMap',
33
+ 'org.springframework.util',
34
+ );
35
+ export const linkedMultiValueMap: KtGenericReferenceFactory<2> = ktReference.genericFactory<2>(
36
+ 'LinkedMultiValueMap',
37
+ 'org.springframework.util',
38
+ );
39
+
40
+ // org.springframework.validation.annotation
41
+ export const validated: KtReferenceFactory = ktReference.factory(
42
+ 'Validated',
43
+ 'org.springframework.validation.annotation',
44
+ );
45
+
46
+ // org.springframework.web.bind.annotation
47
+ export const pathVariable: KtReferenceFactory = ktReference.factory(
48
+ 'PathVariable',
49
+ 'org.springframework.web.bind.annotation',
50
+ );
51
+ export const requestBody: KtReferenceFactory = ktReference.factory(
52
+ 'RequestBody',
53
+ 'org.springframework.web.bind.annotation',
54
+ );
55
+ export const requestMapping: KtReferenceFactory = ktReference.factory(
56
+ 'RequestMapping',
57
+ 'org.springframework.web.bind.annotation',
58
+ );
59
+ export const requestMethod: KtReferenceFactory = ktReference.factory(
60
+ 'RequestMethod',
61
+ 'org.springframework.web.bind.annotation',
62
+ );
63
+ export const requestParam: KtReferenceFactory = ktReference.factory(
64
+ 'RequestParam',
65
+ 'org.springframework.web.bind.annotation',
66
+ );
67
+ export const requestPart: KtReferenceFactory = ktReference.factory(
68
+ 'RequestPart',
69
+ 'org.springframework.web.bind.annotation',
70
+ );
71
+ export const requestHeader: KtReferenceFactory = ktReference.factory(
72
+ 'RequestHeader',
73
+ 'org.springframework.web.bind.annotation',
74
+ );
75
+
76
+ // org.springframework.web.context.request
77
+ export const nativeWebRequest: KtReferenceFactory = ktReference.factory(
78
+ 'NativeWebRequest',
79
+ 'org.springframework.web.context.request',
80
+ );
81
+
82
+ // org.springframework.web.util
83
+ export const uriComponentsBuilder: KtReferenceFactory = ktReference.factory(
84
+ 'UriComponentsBuilder',
85
+ 'org.springframework.web.util',
86
+ );
@@ -0,0 +1,23 @@
1
+ import { ktReference, type KtReferenceFactory } from '../nodes/reference.js';
2
+
3
+ // io.swagger.v3.oas.annotations
4
+ export const parameter: KtReferenceFactory = ktReference.factory('Parameter', 'io.swagger.v3.oas.annotations');
5
+ export const operation: KtReferenceFactory = ktReference.factory('Operation', 'io.swagger.v3.oas.annotations');
6
+
7
+ // io.swagger.v3.oas.annotations.media
8
+ export const schema: KtReferenceFactory = ktReference.factory('Schema', 'io.swagger.v3.oas.annotations.media');
9
+ export const arraySchema: KtReferenceFactory = ktReference.factory(
10
+ 'ArraySchema',
11
+ 'io.swagger.v3.oas.annotations.media',
12
+ );
13
+ export const content: KtReferenceFactory = ktReference.factory('Content', 'io.swagger.v3.oas.annotations.media');
14
+
15
+ // io.swagger.v3.oas.annotations.responses
16
+ export const apiResponse: KtReferenceFactory = ktReference.factory(
17
+ 'ApiResponse',
18
+ 'io.swagger.v3.oas.annotations.responses',
19
+ );
20
+ export const apiResponses: KtReferenceFactory = ktReference.factory(
21
+ 'ApiResponses',
22
+ 'io.swagger.v3.oas.annotations.responses',
23
+ );
@@ -0,0 +1,19 @@
1
+ import { defaultOpenApiGeneratorConfig, type SourceBuilder } from '@goast/core';
2
+
3
+ import { defaultKotlinGeneratorConfig, type KotlinGeneratorConfig } from '../../config.js';
4
+ import { KotlinFileBuilder } from '../../file-builder.js';
5
+
6
+ const ktConfigSymbol = Symbol();
7
+ type _BuilderWithConfig = SourceBuilder & { [ktConfigSymbol]?: KotlinGeneratorConfig };
8
+
9
+ export function getKotlinBuilderOptions(builder: SourceBuilder): KotlinGeneratorConfig {
10
+ if (builder instanceof KotlinFileBuilder) {
11
+ return builder.options;
12
+ }
13
+ if (ktConfigSymbol in builder.options) {
14
+ return builder.options[ktConfigSymbol] as KotlinGeneratorConfig;
15
+ }
16
+ const options = { ...defaultOpenApiGeneratorConfig, ...defaultKotlinGeneratorConfig, ...builder.options };
17
+ (builder as _BuilderWithConfig)[ktConfigSymbol] = options;
18
+ return options;
19
+ }
@@ -0,0 +1,31 @@
1
+ import type { BasicAppendValue, SourceBuilder } from '@goast/core';
2
+
3
+ import { type KtCall, ktCall } from '../nodes/call.js';
4
+ import { type KtString, ktString } from '../nodes/string.js';
5
+ import { listOf, mapOf } from '../references/index.js';
6
+
7
+ export function toKtNode<TBuilder extends SourceBuilder>(object: Record<string | number, unknown>): KtCall<TBuilder>;
8
+ export function toKtNode<TBuilder extends SourceBuilder>(list: unknown[]): KtCall<TBuilder>;
9
+ export function toKtNode<TBuilder extends SourceBuilder>(string: string): KtString<TBuilder>;
10
+ export function toKtNode<TBuilder extends SourceBuilder>(value: unknown): BasicAppendValue<TBuilder>;
11
+ export function toKtNode<TBuilder extends SourceBuilder>(value: unknown): unknown {
12
+ if (Array.isArray(value)) {
13
+ return ktCall(
14
+ [listOf.infer()],
15
+ value.map((x) => toKtNode<TBuilder>(x)),
16
+ );
17
+ }
18
+ if (typeof value === 'object' && value !== null) {
19
+ return ktCall(
20
+ [mapOf.infer()],
21
+ Object.entries(value).map(([k, v]) => ktCall([toKtNode(k), 'to'], [toKtNode(v)])),
22
+ );
23
+ }
24
+ if (value === null || value === undefined) {
25
+ return 'null';
26
+ }
27
+ if (typeof value === 'boolean' || typeof value === 'number') {
28
+ return String(value);
29
+ }
30
+ return ktString(String(value));
31
+ }
@@ -0,0 +1,15 @@
1
+ import { notNullish, type Nullable, type SingleOrMultiple, type SourceBuilder, toArray } from '@goast/core';
2
+
3
+ import { type KtAppendValue, writeKtNode } from './write-kt-node.js';
4
+
5
+ export function writeKtAnnotations<TBuilder extends SourceBuilder>(
6
+ builder: TBuilder,
7
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
8
+ options?: { multiline: boolean },
9
+ ) {
10
+ const filteredNodes = toArray(nodes).filter(notNullish);
11
+ builder.forEach(filteredNodes, (b, a) => {
12
+ writeKtNode(b, a);
13
+ b.append(options?.multiline ? '\n' : ' ');
14
+ });
15
+ }
@@ -0,0 +1,45 @@
1
+ import { notNullish, type Nullable, type SingleOrMultiple, type SourceBuilder, toArray } from '@goast/core';
2
+
3
+ import { KtLambda } from '../nodes/lambda.js';
4
+ import { type KtAppendValue, writeKtNode, writeKtNodes } from './write-kt-node.js';
5
+
6
+ export function writeKtArguments<TBuilder extends SourceBuilder>(
7
+ builder: TBuilder,
8
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
9
+ ) {
10
+ const filteredNodes = toArray(nodes).filter(notNullish);
11
+
12
+ if (filteredNodes.length === 0) {
13
+ builder.append('()');
14
+ return;
15
+ }
16
+
17
+ const lastNode = filteredNodes[filteredNodes.length - 1];
18
+ let lambda: KtAppendValue<TBuilder> | null = null;
19
+ if (lastNode instanceof KtLambda) {
20
+ lambda = lastNode;
21
+ filteredNodes.pop();
22
+ } else if (
23
+ typeof lastNode === 'object' &&
24
+ (!('name' in lastNode) || !lastNode.name) &&
25
+ 'value' in lastNode &&
26
+ lastNode.value instanceof KtLambda
27
+ ) {
28
+ lambda = lastNode.value;
29
+ filteredNodes.pop();
30
+ }
31
+
32
+ const multiline = filteredNodes.length > 3;
33
+
34
+ builder.parenthesizeIf(
35
+ filteredNodes.length > 0,
36
+ '()',
37
+ (b) => writeKtNodes(b, filteredNodes, { separator: multiline ? ',\n' : ', ' }),
38
+ { multiline },
39
+ );
40
+
41
+ if (lambda) {
42
+ builder.append(' ');
43
+ writeKtNode(builder, lambda);
44
+ }
45
+ }
@@ -0,0 +1,27 @@
1
+ import { notNullish, type Nullable, type SingleOrMultiple, type SourceBuilder, toArray } from '@goast/core';
2
+
3
+ import { type KtAppendValue, writeKtNode } from './write-kt-node.js';
4
+
5
+ export function writeKtEnumValues<TBuilder extends SourceBuilder>(
6
+ builder: TBuilder,
7
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
8
+ ) {
9
+ const filteredNodes = toArray(nodes).filter(notNullish);
10
+ const spacing = filteredNodes.some(
11
+ (v) =>
12
+ typeof v === 'object' &&
13
+ (('annotations' in v && Array.isArray(v.annotations) && v.annotations.length > 0) ||
14
+ ('doc' in v && v.doc) ||
15
+ ('members' in v && Array.isArray(v.members) && v.members.some(notNullish))),
16
+ );
17
+ const multiline = spacing ||
18
+ filteredNodes.length > 4 ||
19
+ filteredNodes.some(
20
+ (v) => typeof v === 'object' && 'arguments' in v && Array.isArray(v.arguments) && v.arguments.length > 0,
21
+ );
22
+ builder.forEach(
23
+ filteredNodes,
24
+ (b, v, i) => b.if(spacing && i > 0, (b) => b.ensurePreviousLineEmpty()).append((b) => writeKtNode(b, v)),
25
+ { separator: multiline ? ',\n' : ', ' },
26
+ );
27
+ }
@@ -0,0 +1,12 @@
1
+ import { notNullish, type Nullable, type SingleOrMultiple, type SourceBuilder, toArray } from '@goast/core';
2
+
3
+ import { type KtAppendValue, writeKtNode } from './write-kt-node.js';
4
+
5
+ export function writeKtGenericParameters<TBuilder extends SourceBuilder>(
6
+ builder: TBuilder,
7
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
8
+ ) {
9
+ const filteredNodes = toArray(nodes).filter(notNullish);
10
+ if (filteredNodes.length === 0) return;
11
+ builder.parenthesize('<>', (b) => b.forEach(filteredNodes, writeKtNode, { separator: ', ' }));
12
+ }
@@ -0,0 +1,25 @@
1
+ import { isAppendValue, isAppendValueGroup, notNullish, type Nullable, type SourceBuilder } from '@goast/core';
2
+
3
+ import { KtProperty } from '../nodes/property.js';
4
+ import { type KtAppendValue, writeKtNode } from './write-kt-node.js';
5
+
6
+ export function writeKtMembers<TBuilder extends SourceBuilder>(
7
+ builder: TBuilder,
8
+ members: Nullable<KtAppendValue<TBuilder>>[],
9
+ options?: { alreadyHasMembers?: boolean },
10
+ ): void {
11
+ const filteredMembers = members.filter(notNullish);
12
+ builder.forEach(filteredMembers, (b, m, i) =>
13
+ b.if(
14
+ () =>
15
+ !(m instanceof KtProperty && !m.doc && m.annotations.length === 0) &&
16
+ !isAppendValue(m) &&
17
+ !isAppendValueGroup(m),
18
+ (b) =>
19
+ b
20
+ .if(i > 0 || !!options?.alreadyHasMembers, (b) => b.ensurePreviousLineEmpty())
21
+ .append((b) => writeKtNode(b, m))
22
+ .if(i < filteredMembers.length - 1, (b) => b.ensurePreviousLineEmpty()),
23
+ (b) => b.append((b) => writeKtNode(b, m)).ensureCurrentLineEmpty(),
24
+ ));
25
+ }
@@ -0,0 +1,37 @@
1
+ import {
2
+ type AppendValue,
3
+ notNullish,
4
+ type Nullable,
5
+ type Separator,
6
+ type SingleOrMultiple,
7
+ type SourceBuilder,
8
+ toArray,
9
+ } from '@goast/core';
10
+
11
+ import { KtNode } from '../node.js';
12
+
13
+ export type KtAppendValue<TBuilder extends SourceBuilder> = AppendValue<TBuilder> | KtNode<TBuilder>;
14
+
15
+ export function writeKtNode<TBuilder extends SourceBuilder>(
16
+ builder: TBuilder,
17
+ node: Nullable<KtAppendValue<TBuilder>>,
18
+ ): void {
19
+ if (node instanceof KtNode) {
20
+ node.write(builder);
21
+ } else {
22
+ builder.append(node);
23
+ }
24
+ }
25
+
26
+ export function writeKtNodes<TBuilder extends SourceBuilder>(
27
+ builder: TBuilder,
28
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
29
+ options?: { separator?: Separator<TBuilder, KtAppendValue<TBuilder>> },
30
+ ): void {
31
+ const filteredNodes = toArray(nodes).filter(notNullish);
32
+ if (filteredNodes.length === 1) {
33
+ writeKtNode(builder, filteredNodes[0]);
34
+ } else if (filteredNodes.length > 1) {
35
+ builder.forEach(filteredNodes, writeKtNode, { separator: options?.separator });
36
+ }
37
+ }
@@ -0,0 +1,25 @@
1
+ import { notNullish, type Nullable, type SingleOrMultiple, type SourceBuilder, toArray } from '@goast/core';
2
+
3
+ import { type KtAppendValue, writeKtNode } from './write-kt-node.js';
4
+
5
+ export function writeKtParameters<TBuilder extends SourceBuilder>(
6
+ builder: TBuilder,
7
+ nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>,
8
+ ) {
9
+ const filteredNodes = toArray(nodes).filter(notNullish);
10
+ const hasAnnotations = filteredNodes.some(
11
+ (p) => typeof p === 'object' && 'annotations' in p && Array.isArray(p.annotations) && p.annotations.length > 0,
12
+ );
13
+ const multiline = filteredNodes.length > 2 || hasAnnotations;
14
+ const spacing = multiline && hasAnnotations;
15
+ builder.parenthesize(
16
+ '()',
17
+ (b) =>
18
+ b.forEach(
19
+ filteredNodes,
20
+ (b, p, i) => b.if(i > 0 && spacing, (b) => b.ensurePreviousLineEmpty()).append((b) => writeKtNode(b, p)),
21
+ { separator: multiline ? ',\n' : ', ' },
22
+ ),
23
+ { multiline },
24
+ );
25
+ }
@@ -0,0 +1,4 @@
1
+ export type KotlinImport = {
2
+ packageName?: string;
3
+ typeName: string;
4
+ };
@@ -0,0 +1,41 @@
1
+ import type {
2
+ DefaultGenerationProviderConfig,
3
+ OpenApiGeneratorConfig,
4
+ StringCasing,
5
+ StringCasingWithOptions,
6
+ } from '@goast/core';
7
+
8
+ export type KotlinGeneratorConfig = OpenApiGeneratorConfig & {
9
+ typeNameCasing: StringCasing | StringCasingWithOptions;
10
+ parameterNameCasing: StringCasing | StringCasingWithOptions;
11
+ functionNameCasing: StringCasing | StringCasingWithOptions;
12
+ propertyNameCasing: StringCasing | StringCasingWithOptions;
13
+ enumValueNameCasing: StringCasing | StringCasingWithOptions;
14
+ constantNameCasing: StringCasing | StringCasingWithOptions;
15
+
16
+ globalImports: string[];
17
+ };
18
+
19
+ export const defaultKotlinGeneratorConfig: DefaultGenerationProviderConfig<KotlinGeneratorConfig> = {
20
+ indent: { type: 'spaces', count: 4 },
21
+
22
+ typeNameCasing: 'pascal',
23
+ parameterNameCasing: 'camel',
24
+ functionNameCasing: 'camel',
25
+ propertyNameCasing: 'camel',
26
+ enumValueNameCasing: 'snake',
27
+ constantNameCasing: 'snake',
28
+
29
+ globalImports: [
30
+ 'kotlin.*',
31
+ 'kotlin.annotation.*',
32
+ 'kotlin.collections.*',
33
+ 'kotlin.comparisons.*',
34
+ 'kotlin.io.*',
35
+ 'kotlin.ranges.*',
36
+ 'kotlin.sequences.*',
37
+ 'kotlin.text.*',
38
+ 'java.lang.*',
39
+ 'kotlin.jvm.*',
40
+ ],
41
+ };
@@ -0,0 +1,108 @@
1
+ import { type AppendParam, type AppendValue, isAppendValue, SourceBuilder } from '@goast/core';
2
+
3
+ import { KtNode } from './ast/node.js';
4
+ import { defaultKotlinGeneratorConfig, type KotlinGeneratorConfig } from './config.js';
5
+ import { ImportCollection } from './import-collection.js';
6
+
7
+ type AnnotationArgumentValue<T extends SourceBuilder> = string | ((builder: T) => void);
8
+ type AnnotationArgument<T extends SourceBuilder> =
9
+ | AnnotationArgumentValue<T>
10
+ | [key: string, value: AnnotationArgumentValue<T>]
11
+ | [value: AnnotationArgumentValue<T>, condition: boolean]
12
+ | [key: string, value: AnnotationArgumentValue<T>, condition: boolean];
13
+
14
+ export type KotlinAppends<TAdditionalAppends> = KtNode<KotlinFileBuilder> | TAdditionalAppends;
15
+ export type KotlinAppendParam<TBuilder extends KotlinFileBuilder, TAdditionalAppends> = AppendParam<
16
+ TBuilder,
17
+ KotlinAppends<TAdditionalAppends>
18
+ >;
19
+
20
+ export function isKotlinAppendValue<TBuilder extends KotlinFileBuilder>(
21
+ value: unknown,
22
+ ): value is AppendValue<TBuilder> {
23
+ return isAppendValue(value) || value instanceof KtNode;
24
+ }
25
+
26
+ export class KotlinFileBuilder<TAdditionalAppends = never> extends SourceBuilder<KotlinAppends<TAdditionalAppends>> {
27
+ public readonly packageName: string | undefined;
28
+ public readonly imports: ImportCollection = new ImportCollection();
29
+
30
+ public override get options(): KotlinGeneratorConfig {
31
+ return super.options as KotlinGeneratorConfig;
32
+ }
33
+
34
+ constructor(packageName?: string, options?: KotlinGeneratorConfig) {
35
+ super(options ?? defaultKotlinGeneratorConfig);
36
+ this.packageName = packageName;
37
+ }
38
+
39
+ public addImport(name: string, packageName?: string): this {
40
+ if (packageName !== undefined && packageName !== this.packageName) {
41
+ this.imports.addImport(name, packageName);
42
+ }
43
+ return this;
44
+ }
45
+
46
+ public appendAnnotation(name: string, packageName?: string, args?: AnnotationArgument<this>[]): this {
47
+ const allArgs: [key: string | undefined, value: AnnotationArgumentValue<this>][] = [];
48
+ if (args) {
49
+ for (const a of args) {
50
+ if (!Array.isArray(a)) {
51
+ allArgs.push([undefined, a]);
52
+ } else if (a.length === 2 && a[1] === true) {
53
+ allArgs.push([undefined, a[0]]);
54
+ }
55
+ }
56
+ for (const a of args) {
57
+ if (Array.isArray(a) && typeof a[1] !== 'boolean' && a[2] !== false) {
58
+ allArgs.push(a.length === 2 ? a : [a[0], a[1]]);
59
+ }
60
+ }
61
+ }
62
+
63
+ this.append(`@${name}`).addImport(name, packageName);
64
+
65
+ if (allArgs.length > 0) {
66
+ const multiline = allArgs.some((x) => typeof x[1] !== 'string') ||
67
+ allArgs.reduce((c, [key, value]) => c + (key ? key.length + 3 : 0) + (value?.length ?? 0), 0) > 80;
68
+ this.parenthesize('()', (builder) =>
69
+ builder
70
+ .appendLineIf(multiline)
71
+ .forEach(
72
+ allArgs,
73
+ (builder, [name, value]) =>
74
+ builder
75
+ .append(name ? `${name} = ` : '')
76
+ .append((builder) => (typeof value === 'string' ? builder.append(value) : value(builder))),
77
+ { separator: multiline ? ',\n' : ', ' },
78
+ )
79
+ .appendLineIf(multiline));
80
+ }
81
+
82
+ this.appendLine();
83
+
84
+ return this;
85
+ }
86
+
87
+ public override clear(): void {
88
+ super.clear();
89
+ this.imports.clear();
90
+ }
91
+
92
+ public override toString(addPadding: boolean = true): string {
93
+ return new SourceBuilder(this.options)
94
+ .if(this.packageName !== undefined, (builder) => builder.appendLine(`package ${this.packageName}`).appendLine())
95
+ .append((builder) => this.imports.writeTo(builder))
96
+ .if(addPadding, (builder) => builder.ensurePreviousLineEmpty())
97
+ .append(super.toString())
98
+ .if(addPadding, (builder) => builder.ensureCurrentLineEmpty())
99
+ .toString();
100
+ }
101
+
102
+ protected override appendSingle(value: KotlinAppendParam<this, TAdditionalAppends>): void {
103
+ super.appendSingle(value);
104
+ if (value instanceof KtNode) {
105
+ value.write(this);
106
+ }
107
+ }
108
+ }
@@ -0,0 +1,29 @@
1
+ import {
2
+ type MaybePromise,
3
+ type Nullable,
4
+ type OpenApiGenerationProviderContext,
5
+ type OpenApiGeneratorInput,
6
+ toCasing,
7
+ } from '@goast/core';
8
+
9
+ import type { KotlinGeneratorConfig } from '../config.js';
10
+ import { toKotlinPropertyName, toKotlinStringLiteral } from '../utils.js';
11
+
12
+ export abstract class KotlinFileGenerator<
13
+ TContext extends OpenApiGenerationProviderContext<OpenApiGeneratorInput, KotlinGeneratorConfig>,
14
+ TOutput,
15
+ > {
16
+ public abstract generate(context: TContext): MaybePromise<TOutput>;
17
+
18
+ protected toPropertyName(context: TContext, name: string): string {
19
+ return toKotlinPropertyName(toCasing(name, context.config.propertyNameCasing));
20
+ }
21
+
22
+ protected toEnumValueName(context: TContext, name: string): string {
23
+ return toKotlinPropertyName(toCasing(name, context.config.enumValueNameCasing));
24
+ }
25
+
26
+ protected toStringLiteral(_context: TContext, text: Nullable<string>): string {
27
+ return toKotlinStringLiteral(text);
28
+ }
29
+ }
@@ -0,0 +1,5 @@
1
+ export * from './file-generator.js';
2
+ export * from './models/index.js';
3
+ export * from './services/okhttp3-clients/index.js';
4
+ export * from './services/spring-controllers/index.js';
5
+ export * from './services/spring-reactive-web-clients/index.js';