@goast/kotlin 0.6.0-beta.1 → 0.6.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.
Files changed (147) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +45 -45
  3. package/assets/client/okhttp3/ApiAbstractions.kt +30 -30
  4. package/assets/client/okhttp3/ApiClient.kt +252 -252
  5. package/assets/client/okhttp3/ApiResponse.kt +43 -43
  6. package/assets/client/okhttp3/Errors.kt +21 -21
  7. package/assets/client/okhttp3/PartConfig.kt +11 -11
  8. package/assets/client/okhttp3/RequestConfig.kt +18 -18
  9. package/assets/client/okhttp3/RequestMethod.kt +8 -8
  10. package/assets/client/okhttp3/ResponseExtensions.kt +24 -24
  11. package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
  12. package/esm/src/ast/references/jackson.d.ts +8 -0
  13. package/esm/src/ast/references/jackson.d.ts.map +1 -1
  14. package/esm/src/ast/references/jackson.js +8 -0
  15. package/esm/src/generators/file-generator.d.ts +21 -1
  16. package/esm/src/generators/file-generator.d.ts.map +1 -1
  17. package/esm/src/generators/file-generator.js +41 -2
  18. package/esm/src/generators/models/args.d.ts +5 -0
  19. package/esm/src/generators/models/args.d.ts.map +1 -1
  20. package/esm/src/generators/models/model-generator.d.ts +23 -0
  21. package/esm/src/generators/models/model-generator.d.ts.map +1 -1
  22. package/esm/src/generators/models/model-generator.js +130 -13
  23. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +9 -0
  24. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts.map +1 -1
  25. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +59 -41
  26. package/esm/src/generators/services/okhttp3-clients/okhttp3-clients-generator.d.ts.map +1 -1
  27. package/esm/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +11 -9
  28. package/esm/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  29. package/esm/src/generators/services/spring-controllers/spring-controller-generator.js +53 -35
  30. package/esm/src/generators/services/spring-reactive-web-clients/args.d.ts +3 -0
  31. package/esm/src/generators/services/spring-reactive-web-clients/args.d.ts.map +1 -1
  32. package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts +7 -0
  33. package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
  34. package/esm/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.js +41 -20
  35. package/esm/src/import-collection.d.ts.map +1 -1
  36. package/esm/src/import-collection.js +2 -2
  37. package/esm/src/utils.d.ts +3 -1
  38. package/esm/src/utils.d.ts.map +1 -1
  39. package/esm/src/utils.js +7 -0
  40. package/package.json +2 -2
  41. package/script/src/ast/references/jackson.d.ts +8 -0
  42. package/script/src/ast/references/jackson.d.ts.map +1 -1
  43. package/script/src/ast/references/jackson.js +9 -1
  44. package/script/src/generators/file-generator.d.ts +21 -1
  45. package/script/src/generators/file-generator.d.ts.map +1 -1
  46. package/script/src/generators/file-generator.js +41 -2
  47. package/script/src/generators/models/args.d.ts +5 -0
  48. package/script/src/generators/models/args.d.ts.map +1 -1
  49. package/script/src/generators/models/model-generator.d.ts +23 -0
  50. package/script/src/generators/models/model-generator.d.ts.map +1 -1
  51. package/script/src/generators/models/model-generator.js +129 -12
  52. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +9 -0
  53. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts.map +1 -1
  54. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +58 -40
  55. package/script/src/generators/services/okhttp3-clients/okhttp3-clients-generator.d.ts.map +1 -1
  56. package/script/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +11 -9
  57. package/script/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  58. package/script/src/generators/services/spring-controllers/spring-controller-generator.js +52 -34
  59. package/script/src/generators/services/spring-reactive-web-clients/args.d.ts +3 -0
  60. package/script/src/generators/services/spring-reactive-web-clients/args.d.ts.map +1 -1
  61. package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts +7 -0
  62. package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts.map +1 -1
  63. package/script/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.js +40 -19
  64. package/script/src/import-collection.d.ts.map +1 -1
  65. package/script/src/import-collection.js +1 -1
  66. package/script/src/utils.d.ts +3 -1
  67. package/script/src/utils.d.ts.map +1 -1
  68. package/script/src/utils.js +9 -0
  69. package/src/mod.ts +8 -0
  70. package/src/src/assets.ts +9 -0
  71. package/src/src/ast/_index.ts +66 -0
  72. package/src/src/ast/common.ts +1 -0
  73. package/src/src/ast/index.ts +1 -0
  74. package/src/src/ast/node.ts +10 -0
  75. package/src/src/ast/nodes/annotation.ts +79 -0
  76. package/src/src/ast/nodes/argument.ts +62 -0
  77. package/src/src/ast/nodes/call.ts +75 -0
  78. package/src/src/ast/nodes/class.ts +178 -0
  79. package/src/src/ast/nodes/collection-literal.ts +49 -0
  80. package/src/src/ast/nodes/constructor.ts +126 -0
  81. package/src/src/ast/nodes/doc-tag.ts +138 -0
  82. package/src/src/ast/nodes/doc.ts +111 -0
  83. package/src/src/ast/nodes/enum-value.ts +100 -0
  84. package/src/src/ast/nodes/enum.ts +163 -0
  85. package/src/src/ast/nodes/function.ts +178 -0
  86. package/src/src/ast/nodes/generic-parameter.ts +54 -0
  87. package/src/src/ast/nodes/init-block.ts +38 -0
  88. package/src/src/ast/nodes/interface.ts +133 -0
  89. package/src/src/ast/nodes/lambda-type.ts +73 -0
  90. package/src/src/ast/nodes/lambda.ts +74 -0
  91. package/src/src/ast/nodes/object.ts +102 -0
  92. package/src/src/ast/nodes/parameter.ts +118 -0
  93. package/src/src/ast/nodes/property.ts +225 -0
  94. package/src/src/ast/nodes/reference.ts +178 -0
  95. package/src/src/ast/nodes/string.ts +114 -0
  96. package/src/src/ast/nodes/types.ts +23 -0
  97. package/src/src/ast/references/index.ts +10 -0
  98. package/src/src/ast/references/jackson.ts +70 -0
  99. package/src/src/ast/references/jakarta.ts +14 -0
  100. package/src/src/ast/references/java.ts +27 -0
  101. package/src/src/ast/references/kotlin.ts +41 -0
  102. package/src/src/ast/references/kotlinx.ts +14 -0
  103. package/src/src/ast/references/okhttp3.ts +5 -0
  104. package/src/src/ast/references/reactor.ts +5 -0
  105. package/src/src/ast/references/spring-reactive.ts +37 -0
  106. package/src/src/ast/references/spring.ts +86 -0
  107. package/src/src/ast/references/swagger.ts +23 -0
  108. package/src/src/ast/utils/get-kotlin-builder-options.ts +19 -0
  109. package/src/src/ast/utils/to-kt-node.ts +31 -0
  110. package/src/src/ast/utils/write-kt-annotations.ts +15 -0
  111. package/src/src/ast/utils/write-kt-arguments.ts +45 -0
  112. package/src/src/ast/utils/write-kt-enum-values.ts +27 -0
  113. package/src/src/ast/utils/write-kt-generic-parameters.ts +12 -0
  114. package/src/src/ast/utils/write-kt-members.ts +25 -0
  115. package/src/src/ast/utils/write-kt-node.ts +37 -0
  116. package/src/src/ast/utils/write-kt-parameters.ts +25 -0
  117. package/src/src/common-results.ts +4 -0
  118. package/src/src/config.ts +61 -0
  119. package/src/src/file-builder.ts +112 -0
  120. package/src/src/generators/file-generator.ts +71 -0
  121. package/src/src/generators/index.ts +5 -0
  122. package/src/src/generators/models/args.ts +142 -0
  123. package/src/src/generators/models/index.ts +4 -0
  124. package/src/src/generators/models/model-generator.ts +850 -0
  125. package/src/src/generators/models/models-generator.ts +65 -0
  126. package/src/src/generators/models/models.ts +95 -0
  127. package/src/src/generators/services/okhttp3-clients/args.ts +90 -0
  128. package/src/src/generators/services/okhttp3-clients/index.ts +4 -0
  129. package/src/src/generators/services/okhttp3-clients/models.ts +73 -0
  130. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +639 -0
  131. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +190 -0
  132. package/src/src/generators/services/okhttp3-clients/refs.ts +59 -0
  133. package/src/src/generators/services/spring-controllers/args.ts +95 -0
  134. package/src/src/generators/services/spring-controllers/index.ts +4 -0
  135. package/src/src/generators/services/spring-controllers/models.ts +76 -0
  136. package/src/src/generators/services/spring-controllers/refs.ts +17 -0
  137. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +1129 -0
  138. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +140 -0
  139. package/src/src/generators/services/spring-reactive-web-clients/args.ts +114 -0
  140. package/src/src/generators/services/spring-reactive-web-clients/index.ts +4 -0
  141. package/src/src/generators/services/spring-reactive-web-clients/models.ts +78 -0
  142. package/src/src/generators/services/spring-reactive-web-clients/refs.ts +11 -0
  143. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +657 -0
  144. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +125 -0
  145. package/src/src/import-collection.ts +98 -0
  146. package/src/src/types.ts +3 -0
  147. package/src/src/utils.ts +55 -0
@@ -0,0 +1,112 @@
1
+ import { type AppendParam, type AppendValue, isAppendValue, SourceBuilder, writeGeneratedFile } 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
+ public writeToFile(filePath: string) {
103
+ writeGeneratedFile(this.options, filePath, this.toString());
104
+ }
105
+
106
+ protected override appendSingle(value: KotlinAppendParam<this, TAdditionalAppends>): void {
107
+ super.appendSingle(value);
108
+ if (value instanceof KtNode) {
109
+ value.write(this);
110
+ }
111
+ }
112
+ }
@@ -0,0 +1,71 @@
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
+ /**
23
+ * The Kotlin constant names for the values of an enum, index-aligned with `values`.
24
+ *
25
+ * `toCasing` cannot be relied on alone: `getWords` strips leading digits and every non-alphanumeric
26
+ * character, so a purely numeric value (`1`, `1.1`), the empty string, or a value written only in
27
+ * punctuation or non-ASCII letters all case to `''` — which renders as a nameless enum constant and an
28
+ * empty `when` branch, neither of which is valid Kotlin. Such a value gets an identifier derived from its
29
+ * raw text instead, always prefixed with `_`. That prefix cannot collide with anything `toCasing`
30
+ * produces, because `getWords` strips leading non-alphanumerics.
31
+ *
32
+ * Names are then de-duplicated in declaration order (`_2`, `_2_2`, `_2_3`, ...), so two distinct raw
33
+ * values — `2` and `'2'`, say — can never share one constant.
34
+ */
35
+ protected toEnumValueNames(context: TContext, values: Nullable<readonly unknown[]>): string[] {
36
+ const names: string[] = [];
37
+ const used = new Set<string>();
38
+
39
+ for (const value of values ?? []) {
40
+ const raw = String(value);
41
+ let base = toCasing(raw, context.config.enumValueNameCasing);
42
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(base) || /^_+$/.test(base)) {
43
+ const sanitized = raw.replace(/[^A-Za-z0-9]+/g, '_').replace(/^_+|_+$/g, '');
44
+ base = sanitized.length > 0 ? `_${sanitized}` : raw.length === 0 ? '_EMPTY' : '_VALUE';
45
+ }
46
+
47
+ let name = base;
48
+ for (let i = 2; used.has(name); i++) name = `${base}_${i}`;
49
+ used.add(name);
50
+ names.push(name);
51
+ }
52
+
53
+ return names;
54
+ }
55
+
56
+ /**
57
+ * The Kotlin constant name `value` is given among the enum's `values`, so that a reference to an enum
58
+ * constant always agrees with the constant `toEnumValueNames` declared for it. Values are matched by
59
+ * their string form, because that is what the generated enum stores. Falls back to the name `value`
60
+ * would get on its own if it is not among `values`.
61
+ */
62
+ protected toEnumValueName(context: TContext, values: Nullable<readonly unknown[]>, value: unknown): string {
63
+ const all = values ?? [];
64
+ const index = all.findIndex((x) => String(x) === String(value));
65
+ return index < 0 ? this.toEnumValueNames(context, [value])[0] : this.toEnumValueNames(context, all)[index];
66
+ }
67
+
68
+ protected toStringLiteral(_context: TContext, text: Nullable<string>): string {
69
+ return toKotlinStringLiteral(text);
70
+ }
71
+ }
@@ -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';
@@ -0,0 +1,142 @@
1
+ import type { ApiSchema, ApiSchemaProperty } from '@goast/core';
2
+
3
+ export type GetFileContent = object;
4
+
5
+ export type GetSchemaDeclaration = {
6
+ schema: ApiSchema;
7
+ };
8
+
9
+ export type GetClass = {
10
+ schema: ApiSchema<'object'>;
11
+ };
12
+
13
+ export type GetInterface = {
14
+ schema: ApiSchema<'object'>;
15
+ };
16
+
17
+ export type GetEnum = {
18
+ schema: ApiSchema;
19
+ };
20
+
21
+ export type GetSchemaDocDescription = {
22
+ schema: ApiSchema;
23
+ };
24
+
25
+ export type GetType = {
26
+ schema: ApiSchema;
27
+ nullable?: boolean;
28
+ };
29
+
30
+ export type GetGeneratedType = {
31
+ schema: ApiSchema;
32
+ nullable?: boolean;
33
+ };
34
+
35
+ export type GetAdditionalPropertiesType = {
36
+ schema: ApiSchema<'object'>;
37
+ };
38
+
39
+ export type GetDefaultValue = {
40
+ schema: ApiSchema;
41
+ };
42
+
43
+ export type GetClassParameter = {
44
+ schema: ApiSchema<'object'>;
45
+ inheritedSchemas: ApiSchema[];
46
+ parameters: ApiSchemaProperty[];
47
+ property: ApiSchemaProperty;
48
+ };
49
+
50
+ export type GetInterfaceProperty = {
51
+ schema: ApiSchema<'object'>;
52
+ property: ApiSchemaProperty;
53
+ };
54
+
55
+ export type GetAdditionalPropertiesProperty = {
56
+ schema: ApiSchema<'object'>;
57
+ };
58
+
59
+ export type GetAdditionalPropertiesSetter = {
60
+ schema: ApiSchema<'object'>;
61
+ };
62
+
63
+ export type GetAdditionalPropertiesGetter = {
64
+ schema: ApiSchema<'object'>;
65
+ };
66
+
67
+ export type GetJacksonJsonTypeInfoAnnotation = {
68
+ schema: ApiSchema;
69
+ };
70
+
71
+ export type GetJacksonJsonSubTypesAnnotation = {
72
+ schema: ApiSchema;
73
+ };
74
+
75
+ export type GetJacksonJsonClassDescriptionAnnotation = {
76
+ schema: ApiSchema;
77
+ };
78
+
79
+ export type GetJacksonJsonPropertyAnnotation = {
80
+ schema: ApiSchema;
81
+ property: ApiSchemaProperty;
82
+ };
83
+
84
+ export type GetJacksonJsonPropertyDescriptionAnnotation = {
85
+ schema: ApiSchema;
86
+ property: ApiSchemaProperty;
87
+ };
88
+
89
+ export type GetJacksonJsonIncludeAnnotation = {
90
+ schema: ApiSchema;
91
+ property: ApiSchemaProperty;
92
+ };
93
+
94
+ export type GetJakartaValidationAnnotations = {
95
+ schema: ApiSchema;
96
+ property: ApiSchemaProperty;
97
+ };
98
+
99
+ export type GetSwaggerSchemaAnnotation = {
100
+ schema: ApiSchema;
101
+ property: ApiSchemaProperty;
102
+ };
103
+
104
+ export type GetPackageName = {
105
+ schema: ApiSchema;
106
+ };
107
+
108
+ export type ShouldGenerateTypeDeclaration = {
109
+ schema: ApiSchema;
110
+ };
111
+
112
+ export type GetDeclarationTypeName = {
113
+ schema: ApiSchema;
114
+ };
115
+
116
+ export type GetInheritedSchemas = {
117
+ schema: ApiSchema;
118
+ };
119
+
120
+ export type GetClassProperties = {
121
+ schema: ApiSchema<'object'>;
122
+ };
123
+
124
+ export type SortProperties = {
125
+ schema: ApiSchema;
126
+ properties: Iterable<ApiSchemaProperty>;
127
+ };
128
+
129
+ export type NormalizeSchema = {
130
+ schema: ApiSchema;
131
+ };
132
+
133
+ export type NormalizeDiscriminatedBases = {
134
+ schema: ApiSchema;
135
+ };
136
+
137
+ export type HasProperty = {
138
+ schema: ApiSchema;
139
+ propertyName: string;
140
+ /** The schemas already inspected, so a composition that cycles back on itself terminates. */
141
+ visited?: Set<ApiSchema>;
142
+ };
@@ -0,0 +1,4 @@
1
+ export * as DefaultKotlinModelGeneratorArgs from './args.js';
2
+ export * from './model-generator.js';
3
+ export * from './models-generator.js';
4
+ export * from './models.js';