@goast/kotlin 0.7.0-beta.2 → 0.7.1
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/src/assets.d.ts +17 -0
- package/dist/src/assets.js +17 -0
- package/dist/src/ast/common.d.ts +4 -0
- package/dist/src/ast/node.d.ts +11 -0
- package/dist/src/ast/node.js +11 -0
- package/dist/src/ast/nodes/annotation.d.ts +44 -0
- package/dist/src/ast/nodes/annotation.js +40 -0
- package/dist/src/ast/nodes/argument.d.ts +33 -0
- package/dist/src/ast/nodes/argument.js +33 -0
- package/dist/src/ast/nodes/call.d.ts +42 -0
- package/dist/src/ast/nodes/call.js +42 -0
- package/dist/src/ast/nodes/class.d.ts +96 -0
- package/dist/src/ast/nodes/class.js +90 -0
- package/dist/src/ast/nodes/collection-literal.d.ts +35 -0
- package/dist/src/ast/nodes/collection-literal.js +35 -0
- package/dist/src/ast/nodes/constructor.d.ts +65 -0
- package/dist/src/ast/nodes/constructor.js +62 -0
- package/dist/src/ast/nodes/doc-tag.d.ts +41 -0
- package/dist/src/ast/nodes/doc-tag.js +41 -0
- package/dist/src/ast/nodes/doc.d.ts +44 -0
- package/dist/src/ast/nodes/doc.js +44 -0
- package/dist/src/ast/nodes/enum-value.d.ts +50 -0
- package/dist/src/ast/nodes/enum-value.js +47 -0
- package/dist/src/ast/nodes/enum.d.ts +67 -0
- package/dist/src/ast/nodes/enum.js +64 -0
- package/dist/src/ast/nodes/function.d.ts +105 -0
- package/dist/src/ast/nodes/function.js +105 -0
- package/dist/src/ast/nodes/generic-parameter.d.ts +37 -0
- package/dist/src/ast/nodes/generic-parameter.js +37 -0
- package/dist/src/ast/nodes/init-block.d.ts +32 -0
- package/dist/src/ast/nodes/init-block.js +32 -0
- package/dist/src/ast/nodes/interface.d.ts +73 -0
- package/dist/src/ast/nodes/interface.js +68 -0
- package/dist/src/ast/nodes/lambda-type.d.ts +42 -0
- package/dist/src/ast/nodes/lambda-type.js +42 -0
- package/dist/src/ast/nodes/lambda.d.ts +41 -0
- package/dist/src/ast/nodes/lambda.js +41 -0
- package/dist/src/ast/nodes/object.d.ts +66 -0
- package/dist/src/ast/nodes/object.js +62 -0
- package/dist/src/ast/nodes/parameter.d.ts +68 -0
- package/dist/src/ast/nodes/parameter.js +68 -0
- package/dist/src/ast/nodes/property.d.ts +152 -0
- package/dist/src/ast/nodes/property.js +148 -0
- package/dist/src/ast/nodes/reference.d.ts +107 -0
- package/dist/src/ast/nodes/reference.js +75 -0
- package/dist/src/ast/nodes/string.d.ts +57 -0
- package/dist/src/ast/nodes/string.js +57 -0
- package/dist/src/ast/nodes/types.d.ts +9 -0
- package/dist/src/ast/references/jackson.d.ts +67 -0
- package/dist/src/ast/references/jackson.js +59 -0
- package/dist/src/ast/references/jakarta.d.ts +13 -0
- package/dist/src/ast/references/jakarta.js +13 -0
- package/dist/src/ast/references/java.d.ts +19 -0
- package/dist/src/ast/references/java.js +19 -0
- package/dist/src/ast/references/kotlin.d.ts +28 -0
- package/dist/src/ast/references/kotlin.js +28 -0
- package/dist/src/ast/references/kotlinx.d.ts +8 -0
- package/dist/src/ast/references/kotlinx.js +8 -0
- package/dist/src/ast/references/okhttp3.d.ts +8 -0
- package/dist/src/ast/references/okhttp3.js +8 -0
- package/dist/src/ast/references/reactor.d.ts +8 -0
- package/dist/src/ast/references/reactor.js +8 -0
- package/dist/src/ast/references/spring-reactive.d.ts +18 -0
- package/dist/src/ast/references/spring-reactive.js +18 -0
- package/dist/src/ast/references/spring.d.ts +27 -0
- package/dist/src/ast/references/spring.js +27 -0
- package/dist/src/ast/references/swagger.d.ts +13 -0
- package/dist/src/ast/references/swagger.js +13 -0
- package/dist/src/ast/utils/get-kotlin-builder-options.d.ts +10 -0
- package/dist/src/ast/utils/get-kotlin-builder-options.js +10 -0
- package/dist/src/ast/utils/to-kt-node.d.ts +40 -0
- package/dist/src/ast/utils/write-kt-annotations.d.ts +13 -0
- package/dist/src/ast/utils/write-kt-annotations.js +13 -0
- package/dist/src/ast/utils/write-kt-arguments.d.ts +15 -0
- package/dist/src/ast/utils/write-kt-arguments.js +15 -0
- package/dist/src/ast/utils/write-kt-enum-values.d.ts +13 -0
- package/dist/src/ast/utils/write-kt-enum-values.js +13 -0
- package/dist/src/ast/utils/write-kt-generic-parameters.d.ts +10 -0
- package/dist/src/ast/utils/write-kt-generic-parameters.js +10 -0
- package/dist/src/ast/utils/write-kt-members.d.ts +16 -0
- package/dist/src/ast/utils/write-kt-members.js +16 -0
- package/dist/src/ast/utils/write-kt-node.d.ts +29 -0
- package/dist/src/ast/utils/write-kt-node.js +24 -0
- package/dist/src/ast/utils/write-kt-parameters.d.ts +13 -0
- package/dist/src/ast/utils/write-kt-parameters.js +13 -0
- package/dist/src/common-results.d.ts +9 -0
- package/dist/src/config.d.ts +72 -0
- package/dist/src/config.js +5 -0
- package/dist/src/file-builder.d.ts +91 -0
- package/dist/src/file-builder.js +81 -0
- package/dist/src/generators/file-generator.d.ts +12 -0
- package/dist/src/generators/file-generator.js +7 -0
- package/dist/src/generators/models/args.d.ts +233 -0
- package/dist/src/generators/models/model-generator.d.ts +25 -0
- package/dist/src/generators/models/model-generator.js +14 -0
- package/dist/src/generators/models/models-generator.d.ts +15 -0
- package/dist/src/generators/models/models-generator.js +15 -0
- package/dist/src/generators/models/models.d.ts +93 -2
- package/dist/src/generators/models/models.js +5 -0
- package/dist/src/generators/services/okhttp3-clients/args.d.ts +138 -0
- package/dist/src/generators/services/okhttp3-clients/models.d.ts +83 -0
- package/dist/src/generators/services/okhttp3-clients/models.js +5 -0
- package/dist/src/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +33 -0
- package/dist/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +20 -0
- package/dist/src/generators/services/okhttp3-clients/okhttp3-clients-generator.d.ts +21 -0
- package/dist/src/generators/services/okhttp3-clients/okhttp3-clients-generator.js +21 -0
- package/dist/src/generators/services/okhttp3-clients/refs.d.ts +25 -0
- package/dist/src/generators/services/okhttp3-clients/refs.js +7 -0
- package/dist/src/generators/services/spring-controllers/args.d.ts +169 -0
- package/dist/src/generators/services/spring-controllers/models.d.ts +120 -0
- package/dist/src/generators/services/spring-controllers/models.js +4 -0
- package/dist/src/generators/services/spring-controllers/refs.d.ts +9 -0
- package/dist/src/generators/services/spring-controllers/refs.js +9 -0
- package/dist/src/generators/services/spring-controllers/spring-controller-generator.d.ts +29 -0
- package/dist/src/generators/services/spring-controllers/spring-controller-generator.js +19 -0
- package/dist/src/generators/services/spring-controllers/spring-controllers-generator.d.ts +22 -0
- package/dist/src/generators/services/spring-controllers/spring-controllers-generator.js +22 -0
- package/dist/src/generators/services/spring-reactive-web-clients/args.d.ts +224 -0
- package/dist/src/generators/services/spring-reactive-web-clients/models.d.ts +86 -0
- package/dist/src/generators/services/spring-reactive-web-clients/models.js +4 -0
- package/dist/src/generators/services/spring-reactive-web-clients/refs.d.ts +7 -0
- package/dist/src/generators/services/spring-reactive-web-clients/refs.js +7 -0
- package/dist/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.d.ts +36 -0
- package/dist/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.js +25 -0
- package/dist/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.d.ts +20 -0
- package/dist/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.js +20 -0
- package/dist/src/import-collection.d.ts +64 -0
- package/dist/src/import-collection.js +46 -0
- package/dist/src/types.d.ts +4 -0
- package/dist/src/utils.d.ts +52 -0
- package/dist/src/utils.js +52 -0
- package/package.json +2 -2
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { isAppendValue, isAppendValueGroup, notNullish } from '@goast/core';
|
|
2
2
|
import { KtProperty } from '../nodes/property.js';
|
|
3
3
|
import { writeKtNode } from './write-kt-node.js';
|
|
4
|
+
/**
|
|
5
|
+
* Writes class members, separating them with blank lines where needed.
|
|
6
|
+
*
|
|
7
|
+
* Each member is either compact or a block. A compact member is a `KtProperty` without a doc comment and without
|
|
8
|
+
* annotations, or a plain append value such as a string. All other members are blocks. The rules are:
|
|
9
|
+
* - A block member is preceded by a blank line, except when it is the first member and `alreadyHasMembers` is not set.
|
|
10
|
+
* - A block member is followed by a blank line, except when it is the last member.
|
|
11
|
+
* - A compact member is written on its own line and followed by a line break. No blank line is added before or after
|
|
12
|
+
* it, so consecutive compact members are written on consecutive lines.
|
|
13
|
+
*
|
|
14
|
+
* @template TBuilder The type of the source builder the members are written to.
|
|
15
|
+
* @param builder The builder to write to.
|
|
16
|
+
* @param members The members in the order they are written. Nullish entries are ignored.
|
|
17
|
+
* @param options Options for writing. Set `alreadyHasMembers` when other members precede these, so that a first block
|
|
18
|
+
* member also gets a blank line before it.
|
|
19
|
+
*/
|
|
4
20
|
export function writeKtMembers(builder, members, options) {
|
|
5
21
|
const filteredMembers = members.filter(notNullish);
|
|
6
22
|
builder.forEach(filteredMembers, (b, m, i) => b.if(() => !(m instanceof KtProperty && !m.doc && m.annotations.length === 0) &&
|
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
import { type AppendValue, type Nullable, type Separator, type SingleOrMultiple, type SourceBuilder } from '@goast/core';
|
|
2
2
|
import { KtNode } from '../node.ts';
|
|
3
|
+
/**
|
|
4
|
+
* A value that the Kotlin writers accept: a plain append value, which is appended as is, or a `KtNode`, which writes
|
|
5
|
+
* itself.
|
|
6
|
+
* @template TBuilder The type of the source builder the value is written to.
|
|
7
|
+
*/
|
|
3
8
|
export type KtAppendValue<TBuilder extends SourceBuilder> = AppendValue<TBuilder> | KtNode<TBuilder>;
|
|
9
|
+
/**
|
|
10
|
+
* Writes one Kotlin node or append value to the builder.
|
|
11
|
+
*
|
|
12
|
+
* A `KtNode` writes itself. Any other value is appended as is, so nullish values write nothing. No separators, line
|
|
13
|
+
* breaks or blank lines are added.
|
|
14
|
+
*
|
|
15
|
+
* @template TBuilder The type of the source builder the node is written to.
|
|
16
|
+
* @param builder The builder to write to.
|
|
17
|
+
* @param node The node or append value to write. Nullish values write nothing.
|
|
18
|
+
*/
|
|
4
19
|
export declare function writeKtNode<TBuilder extends SourceBuilder>(builder: TBuilder, node: Nullable<KtAppendValue<TBuilder>>): void;
|
|
20
|
+
/**
|
|
21
|
+
* Writes one or more Kotlin nodes or append values to the builder, ignoring nullish entries.
|
|
22
|
+
*
|
|
23
|
+
* After nullish entries are removed, a single entry is written as is, and several entries are written one after
|
|
24
|
+
* another.
|
|
25
|
+
* Between two entries the `separator` option is written: a string is appended as is, and a callback appends its own
|
|
26
|
+
* content. Without a separator, the entries are written back to back. No entries write nothing. No line breaks or blank
|
|
27
|
+
* lines are added by this function.
|
|
28
|
+
*
|
|
29
|
+
* @template TBuilder The type of the source builder the nodes are written to.
|
|
30
|
+
* @param builder The builder to write to.
|
|
31
|
+
* @param nodes A single node or an array of nodes. Nullish entries are ignored.
|
|
32
|
+
* @param options Options for writing. `separator` is written between each pair of consecutive entries.
|
|
33
|
+
*/
|
|
5
34
|
export declare function writeKtNodes<TBuilder extends SourceBuilder>(builder: TBuilder, nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>, options?: {
|
|
6
35
|
separator?: Separator<TBuilder, KtAppendValue<TBuilder>>;
|
|
7
36
|
}): void;
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { notNullish, toArray, } from '@goast/core';
|
|
2
2
|
import { KtNode } from '../node.js';
|
|
3
|
+
/**
|
|
4
|
+
* Writes one Kotlin node or append value to the builder.
|
|
5
|
+
*
|
|
6
|
+
* A `KtNode` writes itself. Any other value is appended as is, so nullish values write nothing. No separators, line
|
|
7
|
+
* breaks or blank lines are added.
|
|
8
|
+
*
|
|
9
|
+
* @template TBuilder The type of the source builder the node is written to.
|
|
10
|
+
* @param builder The builder to write to.
|
|
11
|
+
* @param node The node or append value to write. Nullish values write nothing.
|
|
12
|
+
*/
|
|
3
13
|
export function writeKtNode(builder, node) {
|
|
4
14
|
if (node instanceof KtNode) {
|
|
5
15
|
node.write(builder);
|
|
@@ -8,6 +18,20 @@ export function writeKtNode(builder, node) {
|
|
|
8
18
|
builder.append(node);
|
|
9
19
|
}
|
|
10
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Writes one or more Kotlin nodes or append values to the builder, ignoring nullish entries.
|
|
23
|
+
*
|
|
24
|
+
* After nullish entries are removed, a single entry is written as is, and several entries are written one after
|
|
25
|
+
* another.
|
|
26
|
+
* Between two entries the `separator` option is written: a string is appended as is, and a callback appends its own
|
|
27
|
+
* content. Without a separator, the entries are written back to back. No entries write nothing. No line breaks or blank
|
|
28
|
+
* lines are added by this function.
|
|
29
|
+
*
|
|
30
|
+
* @template TBuilder The type of the source builder the nodes are written to.
|
|
31
|
+
* @param builder The builder to write to.
|
|
32
|
+
* @param nodes A single node or an array of nodes. Nullish entries are ignored.
|
|
33
|
+
* @param options Options for writing. `separator` is written between each pair of consecutive entries.
|
|
34
|
+
*/
|
|
11
35
|
export function writeKtNodes(builder, nodes, options) {
|
|
12
36
|
const filteredNodes = toArray(nodes).filter(notNullish);
|
|
13
37
|
if (filteredNodes.length === 1) {
|
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
import { type Nullable, type SingleOrMultiple, type SourceBuilder } from '@goast/core';
|
|
2
2
|
import { type KtAppendValue } from './write-kt-node.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Writes a parenthesized parameter list, such as the parameters of a function or constructor.
|
|
5
|
+
*
|
|
6
|
+
* The list is multiline when there are more than two parameters or when any parameter has annotations. A multiline list
|
|
7
|
+
* puts the opening and closing parentheses on their own lines, indents the parameters and writes each one on its own
|
|
8
|
+
* line, separated by `,` and a line break. When any parameter has annotations, a blank line is also written between
|
|
9
|
+
* each pair of parameters. Otherwise the parameters are written inline, separated by `, `. An empty list is written
|
|
10
|
+
* as `()`. No trailing comma is written.
|
|
11
|
+
*
|
|
12
|
+
* @template TBuilder The type of the source builder the parameters are written to.
|
|
13
|
+
* @param builder The builder to write to.
|
|
14
|
+
* @param nodes The parameters, as a single node or an array. Nullish entries are ignored.
|
|
15
|
+
*/
|
|
3
16
|
export declare function writeKtParameters<TBuilder extends SourceBuilder>(builder: TBuilder, nodes: SingleOrMultiple<Nullable<KtAppendValue<TBuilder>>>): void;
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { notNullish, toArray } from '@goast/core';
|
|
2
2
|
import { writeKtNode } from './write-kt-node.js';
|
|
3
|
+
/**
|
|
4
|
+
* Writes a parenthesized parameter list, such as the parameters of a function or constructor.
|
|
5
|
+
*
|
|
6
|
+
* The list is multiline when there are more than two parameters or when any parameter has annotations. A multiline list
|
|
7
|
+
* puts the opening and closing parentheses on their own lines, indents the parameters and writes each one on its own
|
|
8
|
+
* line, separated by `,` and a line break. When any parameter has annotations, a blank line is also written between
|
|
9
|
+
* each pair of parameters. Otherwise the parameters are written inline, separated by `, `. An empty list is written
|
|
10
|
+
* as `()`. No trailing comma is written.
|
|
11
|
+
*
|
|
12
|
+
* @template TBuilder The type of the source builder the parameters are written to.
|
|
13
|
+
* @param builder The builder to write to.
|
|
14
|
+
* @param nodes The parameters, as a single node or an array. Nullish entries are ignored.
|
|
15
|
+
*/
|
|
3
16
|
export function writeKtParameters(builder, nodes) {
|
|
4
17
|
const filteredNodes = toArray(nodes).filter(notNullish);
|
|
5
18
|
const hasAnnotations = filteredNodes.some((p) => typeof p === 'object' && 'annotations' in p && Array.isArray(p.annotations) && p.annotations.length > 0);
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes a type that a generated Kotlin file imports.
|
|
3
|
+
*/
|
|
1
4
|
export type KotlinImport = {
|
|
5
|
+
/**
|
|
6
|
+
* The package the type is declared in, such as `kotlin.collections`. Without a package, no import is written.
|
|
7
|
+
*/
|
|
2
8
|
packageName?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The name of the type to import from the package, such as `List`.
|
|
11
|
+
*/
|
|
3
12
|
typeName: string;
|
|
4
13
|
};
|
package/dist/src/config.d.ts
CHANGED
|
@@ -1,13 +1,80 @@
|
|
|
1
1
|
import type { DefaultGenerationProviderConfig, OpenApiGeneratorConfig, StringCasing, StringCasingWithOptions } from '@goast/core';
|
|
2
2
|
/** The Spring Boot major version the Kotlin generators target. */
|
|
3
3
|
export type SpringBootVersion = 3 | 4;
|
|
4
|
+
/**
|
|
5
|
+
* The configuration that all Kotlin generators share. It extends the core `OpenApiGeneratorConfig` with the casing
|
|
6
|
+
* rules for generated names, the imports that are left out of generated files, and the targeted Spring Boot version.
|
|
7
|
+
* @see defaultKotlinGeneratorConfig
|
|
8
|
+
*/
|
|
4
9
|
export type KotlinGeneratorConfig = OpenApiGeneratorConfig & {
|
|
10
|
+
/**
|
|
11
|
+
* The casing applied to type names, such as model classes, enums and the types of generated services. It also
|
|
12
|
+
* determines the file names of models, since each model file is named after its type.
|
|
13
|
+
* @default 'pascal'
|
|
14
|
+
* @see StringCasingWithOptions
|
|
15
|
+
*/
|
|
5
16
|
typeNameCasing: StringCasing | StringCasingWithOptions;
|
|
17
|
+
/**
|
|
18
|
+
* The casing applied to parameter names. Service generators use it for the parameters of endpoint functions, and the
|
|
19
|
+
* models generator uses it for the parameters of the generated `set` function for `additionalProperties`.
|
|
20
|
+
* @default 'camel'
|
|
21
|
+
*/
|
|
6
22
|
parameterNameCasing: StringCasing | StringCasingWithOptions;
|
|
23
|
+
/**
|
|
24
|
+
* The casing applied to function names. Service generators use it for the endpoint functions, and the models
|
|
25
|
+
* generator uses it for the generated `set` and `getMap` functions for `additionalProperties`.
|
|
26
|
+
* @default 'camel'
|
|
27
|
+
*/
|
|
7
28
|
functionNameCasing: StringCasing | StringCasingWithOptions;
|
|
29
|
+
/**
|
|
30
|
+
* The casing applied to property names. The models generator uses it for the properties of models, the `value`
|
|
31
|
+
* property of enums and the `additionalProperties` property.
|
|
32
|
+
* @default 'camel'
|
|
33
|
+
*/
|
|
8
34
|
propertyNameCasing: StringCasing | StringCasingWithOptions;
|
|
35
|
+
/**
|
|
36
|
+
* The casing applied to the names of enum constants. With the default casing, the value `available` becomes
|
|
37
|
+
* `AVAILABLE`, because `snake` writes words in upper case unless `wordCasing` is given.
|
|
38
|
+
* @default 'snake'
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* enumValueNameCasing: 'snake'; // 'pending review' => 'PENDING_REVIEW'
|
|
42
|
+
* enumValueNameCasing: { casing: 'snake', wordCasing: 'all-lower' }; // 'pending review' => 'pending_review'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
9
45
|
enumValueNameCasing: StringCasing | StringCasingWithOptions;
|
|
46
|
+
/**
|
|
47
|
+
* The casing applied to constant names. The models generator does not write constants, and the spring-controller
|
|
48
|
+
* generator uses it for the path constants of endpoints.
|
|
49
|
+
* @default 'snake'
|
|
50
|
+
*/
|
|
10
51
|
constantNameCasing: StringCasing | StringCasingWithOptions;
|
|
52
|
+
/**
|
|
53
|
+
* The imports that are left out of generated files, because Kotlin already imports these packages implicitly. An
|
|
54
|
+
* entry ending in `.*` excludes a whole package, but not its subpackages. Any other entry excludes one type by its
|
|
55
|
+
* fully qualified name, such as `com.example.Shared`.
|
|
56
|
+
* @default
|
|
57
|
+
* [
|
|
58
|
+
* 'kotlin.*',
|
|
59
|
+
* 'kotlin.annotation.*',
|
|
60
|
+
* 'kotlin.collections.*',
|
|
61
|
+
* 'kotlin.comparisons.*',
|
|
62
|
+
* 'kotlin.io.*',
|
|
63
|
+
* 'kotlin.ranges.*',
|
|
64
|
+
* 'kotlin.sequences.*',
|
|
65
|
+
* 'kotlin.text.*',
|
|
66
|
+
* 'java.lang.*',
|
|
67
|
+
* 'kotlin.jvm.*',
|
|
68
|
+
* ]
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* // With globalImports: ['kotlin.*', 'kotlin.collections.*', 'com.example.Shared']
|
|
72
|
+
* // 'kotlin.String' is not imported, because the package 'kotlin' is excluded.
|
|
73
|
+
* // 'kotlin.collections.List' is not imported, because the package 'kotlin.collections' is excluded.
|
|
74
|
+
* // 'com.example.Shared' is not imported, because the type is excluded by its full name.
|
|
75
|
+
* // 'kotlin.time.Duration' is imported, because 'kotlin.*' does not exclude subpackages.
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
11
78
|
globalImports: string[];
|
|
12
79
|
/**
|
|
13
80
|
* Whether to include the source OpenAPI document path in the generated documentation comments.
|
|
@@ -23,4 +90,9 @@ export type KotlinGeneratorConfig = OpenApiGeneratorConfig & {
|
|
|
23
90
|
*/
|
|
24
91
|
springBootVersion: SpringBootVersion;
|
|
25
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* The default values of the Kotlin generator configuration. Each property's `@default` tag in `KotlinGeneratorConfig`
|
|
95
|
+
* gives the same value. `indent` is four spaces here, while the core default is two.
|
|
96
|
+
* @see KotlinGeneratorConfig
|
|
97
|
+
*/
|
|
26
98
|
export declare const defaultKotlinGeneratorConfig: DefaultGenerationProviderConfig<KotlinGeneratorConfig>;
|
package/dist/src/config.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default values of the Kotlin generator configuration. Each property's `@default` tag in `KotlinGeneratorConfig`
|
|
3
|
+
* gives the same value. `indent` is four spaces here, while the core default is two.
|
|
4
|
+
* @see KotlinGeneratorConfig
|
|
5
|
+
*/
|
|
1
6
|
export const defaultKotlinGeneratorConfig = {
|
|
2
7
|
indent: { type: 'spaces', count: 4 },
|
|
3
8
|
typeNameCasing: 'pascal',
|
|
@@ -4,18 +4,109 @@ import { type KotlinGeneratorConfig } from './config.ts';
|
|
|
4
4
|
import { ImportCollection } from './import-collection.ts';
|
|
5
5
|
type AnnotationArgumentValue<T extends SourceBuilder> = string | ((builder: T) => void);
|
|
6
6
|
type AnnotationArgument<T extends SourceBuilder> = AnnotationArgumentValue<T> | [key: string, value: AnnotationArgumentValue<T>] | [value: AnnotationArgumentValue<T>, condition: boolean] | [key: string, value: AnnotationArgumentValue<T>, condition: boolean];
|
|
7
|
+
/**
|
|
8
|
+
* The values that can be appended to a `KotlinFileBuilder`: Kotlin nodes, and the additional values of the builder.
|
|
9
|
+
* @template TAdditionalAppends The additional values, besides Kotlin nodes, that the builder accepts.
|
|
10
|
+
*/
|
|
7
11
|
export type KotlinAppends<TAdditionalAppends> = KtNode<KotlinFileBuilder> | TAdditionalAppends;
|
|
12
|
+
/**
|
|
13
|
+
* A parameter of an append method of a `KotlinFileBuilder`: a single value or builder function, or an array of them.
|
|
14
|
+
* @template TBuilder The Kotlin file builder type that the value is appended to.
|
|
15
|
+
* @template TAdditionalAppends The additional values, besides Kotlin nodes, that the builder accepts.
|
|
16
|
+
* @see KotlinAppends
|
|
17
|
+
*/
|
|
8
18
|
export type KotlinAppendParam<TBuilder extends KotlinFileBuilder, TAdditionalAppends> = AppendParam<TBuilder, KotlinAppends<TAdditionalAppends>>;
|
|
19
|
+
/**
|
|
20
|
+
* Checks whether a value can be appended to a Kotlin file builder: an append value, or a Kotlin node.
|
|
21
|
+
* @template TBuilder The Kotlin file builder type that the value is appended to.
|
|
22
|
+
* @param value The value to check.
|
|
23
|
+
* @returns `true` if the value can be appended, otherwise `false`.
|
|
24
|
+
*/
|
|
9
25
|
export declare function isKotlinAppendValue<TBuilder extends KotlinFileBuilder>(value: unknown): value is AppendValue<TBuilder>;
|
|
26
|
+
/**
|
|
27
|
+
* Builds the source of one Kotlin file. Besides the code, it collects the imports that the code needs and writes them
|
|
28
|
+
* above the code, after the `package` line.
|
|
29
|
+
*
|
|
30
|
+
* Imports are collected while the code is written. A `KtReference` created by `ktReference` writes its name and then
|
|
31
|
+
* calls `addImport` on the builder. The builder ignores references to its own package and passes the others to its
|
|
32
|
+
* `ImportCollection`. `toString` then writes the `package` line, the import statements that the collection writes, and
|
|
33
|
+
* finally the code. Annotations written with `appendAnnotation` register their imports the same way.
|
|
34
|
+
*
|
|
35
|
+
* @template TAdditionalAppends The additional values, besides Kotlin nodes, that can be appended to the builder.
|
|
36
|
+
* @see ImportCollection
|
|
37
|
+
* @see ktReference
|
|
38
|
+
*/
|
|
10
39
|
export declare class KotlinFileBuilder<TAdditionalAppends = never> extends SourceBuilder<KotlinAppends<TAdditionalAppends>> {
|
|
40
|
+
/**
|
|
41
|
+
* The package the file is declared in, or `undefined` for a file without a package. References to this package do not
|
|
42
|
+
* produce imports.
|
|
43
|
+
*/
|
|
11
44
|
readonly packageName: string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The imports of this file. The collection is created with its default options, so its `globalImports` is always the
|
|
47
|
+
* default list, whatever the configuration of this builder says.
|
|
48
|
+
*/
|
|
12
49
|
readonly imports: ImportCollection;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the options of this builder, which are the Kotlin generator configuration that it was created with, or the
|
|
52
|
+
* defaults.
|
|
53
|
+
* @returns The options of this builder.
|
|
54
|
+
*/
|
|
13
55
|
get options(): KotlinGeneratorConfig;
|
|
56
|
+
/**
|
|
57
|
+
* Initializes a new instance of the KotlinFileBuilder class.
|
|
58
|
+
* @param packageName The package of the file, or `undefined` for a file without a package.
|
|
59
|
+
* @param options The Kotlin generator configuration to use. Defaults to `defaultKotlinGeneratorConfig`.
|
|
60
|
+
*/
|
|
14
61
|
constructor(packageName?: string, options?: KotlinGeneratorConfig);
|
|
62
|
+
/**
|
|
63
|
+
* Registers the import of a type that the file refers to. Nothing is registered when `packageName` is missing, or
|
|
64
|
+
* when it is the package of this file, because no import is needed there.
|
|
65
|
+
* @param name The name of the type, such as `List`.
|
|
66
|
+
* @param packageName The package the type is declared in, such as `kotlin.collections`.
|
|
67
|
+
* @returns A reference to this instance.
|
|
68
|
+
*/
|
|
15
69
|
addImport(name: string, packageName?: string): this;
|
|
70
|
+
/**
|
|
71
|
+
* Appends an annotation, such as `@JsonProperty("id")`, on a line of its own, and registers its import.
|
|
72
|
+
*
|
|
73
|
+
* Each entry of `args` is either a value, which is written as a positional argument, or one of these tuples:
|
|
74
|
+
* - `[value, condition]` writes the value as a positional argument when the condition is `true`.
|
|
75
|
+
* - `[key, value]` writes the named argument `key = value`.
|
|
76
|
+
* - `[key, value, condition]` writes the named argument unless the condition is `false`.
|
|
77
|
+
*
|
|
78
|
+
* The arguments are written on one line, or one per line when a value is a builder function or the arguments are
|
|
79
|
+
* long.
|
|
80
|
+
* @param name The simple name of the annotation, such as `JsonProperty`.
|
|
81
|
+
* @param packageName The package of the annotation. It is imported unless it is the package of this file.
|
|
82
|
+
* @param args The arguments of the annotation, in the forms described above.
|
|
83
|
+
* @returns A reference to this instance.
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* builder.appendAnnotation('JsonProperty', 'com.fasterxml.jackson.annotation', [
|
|
87
|
+
* '"id"',
|
|
88
|
+
* ['required', 'true', isRequired],
|
|
89
|
+
* ]);
|
|
90
|
+
* // Emits: @JsonProperty("id", required = true)
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
16
93
|
appendAnnotation(name: string, packageName?: string, args?: AnnotationArgument<this>[]): this;
|
|
94
|
+
/**
|
|
95
|
+
* Removes the code and all collected imports. The package name is kept.
|
|
96
|
+
*/
|
|
17
97
|
clear(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Gets the source of the file: the `package` line, the import statements, and the code.
|
|
100
|
+
* @param addPadding Whether to separate the imports from the code with an empty line, and to end the source with a
|
|
101
|
+
* line break. Defaults to `true`.
|
|
102
|
+
* @returns The source of the file.
|
|
103
|
+
*/
|
|
18
104
|
toString(addPadding?: boolean): string;
|
|
105
|
+
/**
|
|
106
|
+
* Writes the source of the file to a path, creating missing parent directories. If the file already exists, the
|
|
107
|
+
* `existingFileBehavior` of the configuration decides what happens.
|
|
108
|
+
* @param filePath The path of the file to write.
|
|
109
|
+
*/
|
|
19
110
|
writeToFile(filePath: string): void;
|
|
20
111
|
protected appendSingle(value: KotlinAppendParam<this, TAdditionalAppends>): void;
|
|
21
112
|
}
|
package/dist/src/file-builder.js
CHANGED
|
@@ -2,25 +2,92 @@ import { isAppendValue, SourceBuilder, writeGeneratedFile } from '@goast/core';
|
|
|
2
2
|
import { KtNode } from './ast/node.js';
|
|
3
3
|
import { defaultKotlinGeneratorConfig } from './config.js';
|
|
4
4
|
import { ImportCollection } from './import-collection.js';
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a value can be appended to a Kotlin file builder: an append value, or a Kotlin node.
|
|
7
|
+
* @template TBuilder The Kotlin file builder type that the value is appended to.
|
|
8
|
+
* @param value The value to check.
|
|
9
|
+
* @returns `true` if the value can be appended, otherwise `false`.
|
|
10
|
+
*/
|
|
5
11
|
export function isKotlinAppendValue(value) {
|
|
6
12
|
return isAppendValue(value) || value instanceof KtNode;
|
|
7
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Builds the source of one Kotlin file. Besides the code, it collects the imports that the code needs and writes them
|
|
16
|
+
* above the code, after the `package` line.
|
|
17
|
+
*
|
|
18
|
+
* Imports are collected while the code is written. A `KtReference` created by `ktReference` writes its name and then
|
|
19
|
+
* calls `addImport` on the builder. The builder ignores references to its own package and passes the others to its
|
|
20
|
+
* `ImportCollection`. `toString` then writes the `package` line, the import statements that the collection writes, and
|
|
21
|
+
* finally the code. Annotations written with `appendAnnotation` register their imports the same way.
|
|
22
|
+
*
|
|
23
|
+
* @template TAdditionalAppends The additional values, besides Kotlin nodes, that can be appended to the builder.
|
|
24
|
+
* @see ImportCollection
|
|
25
|
+
* @see ktReference
|
|
26
|
+
*/
|
|
8
27
|
export class KotlinFileBuilder extends SourceBuilder {
|
|
28
|
+
/**
|
|
29
|
+
* The package the file is declared in, or `undefined` for a file without a package. References to this package do not
|
|
30
|
+
* produce imports.
|
|
31
|
+
*/
|
|
9
32
|
packageName;
|
|
33
|
+
/**
|
|
34
|
+
* The imports of this file. The collection is created with its default options, so its `globalImports` is always the
|
|
35
|
+
* default list, whatever the configuration of this builder says.
|
|
36
|
+
*/
|
|
10
37
|
imports = new ImportCollection();
|
|
38
|
+
/**
|
|
39
|
+
* Gets the options of this builder, which are the Kotlin generator configuration that it was created with, or the
|
|
40
|
+
* defaults.
|
|
41
|
+
* @returns The options of this builder.
|
|
42
|
+
*/
|
|
11
43
|
get options() {
|
|
12
44
|
return super.options;
|
|
13
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Initializes a new instance of the KotlinFileBuilder class.
|
|
48
|
+
* @param packageName The package of the file, or `undefined` for a file without a package.
|
|
49
|
+
* @param options The Kotlin generator configuration to use. Defaults to `defaultKotlinGeneratorConfig`.
|
|
50
|
+
*/
|
|
14
51
|
constructor(packageName, options) {
|
|
15
52
|
super(options ?? defaultKotlinGeneratorConfig);
|
|
16
53
|
this.packageName = packageName;
|
|
17
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Registers the import of a type that the file refers to. Nothing is registered when `packageName` is missing, or
|
|
57
|
+
* when it is the package of this file, because no import is needed there.
|
|
58
|
+
* @param name The name of the type, such as `List`.
|
|
59
|
+
* @param packageName The package the type is declared in, such as `kotlin.collections`.
|
|
60
|
+
* @returns A reference to this instance.
|
|
61
|
+
*/
|
|
18
62
|
addImport(name, packageName) {
|
|
19
63
|
if (packageName !== undefined && packageName !== this.packageName) {
|
|
20
64
|
this.imports.addImport(name, packageName);
|
|
21
65
|
}
|
|
22
66
|
return this;
|
|
23
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Appends an annotation, such as `@JsonProperty("id")`, on a line of its own, and registers its import.
|
|
70
|
+
*
|
|
71
|
+
* Each entry of `args` is either a value, which is written as a positional argument, or one of these tuples:
|
|
72
|
+
* - `[value, condition]` writes the value as a positional argument when the condition is `true`.
|
|
73
|
+
* - `[key, value]` writes the named argument `key = value`.
|
|
74
|
+
* - `[key, value, condition]` writes the named argument unless the condition is `false`.
|
|
75
|
+
*
|
|
76
|
+
* The arguments are written on one line, or one per line when a value is a builder function or the arguments are
|
|
77
|
+
* long.
|
|
78
|
+
* @param name The simple name of the annotation, such as `JsonProperty`.
|
|
79
|
+
* @param packageName The package of the annotation. It is imported unless it is the package of this file.
|
|
80
|
+
* @param args The arguments of the annotation, in the forms described above.
|
|
81
|
+
* @returns A reference to this instance.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* builder.appendAnnotation('JsonProperty', 'com.fasterxml.jackson.annotation', [
|
|
85
|
+
* '"id"',
|
|
86
|
+
* ['required', 'true', isRequired],
|
|
87
|
+
* ]);
|
|
88
|
+
* // Emits: @JsonProperty("id", required = true)
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
24
91
|
appendAnnotation(name, packageName, args) {
|
|
25
92
|
const allArgs = [];
|
|
26
93
|
if (args) {
|
|
@@ -52,10 +119,19 @@ export class KotlinFileBuilder extends SourceBuilder {
|
|
|
52
119
|
this.appendLine();
|
|
53
120
|
return this;
|
|
54
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Removes the code and all collected imports. The package name is kept.
|
|
124
|
+
*/
|
|
55
125
|
clear() {
|
|
56
126
|
super.clear();
|
|
57
127
|
this.imports.clear();
|
|
58
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the source of the file: the `package` line, the import statements, and the code.
|
|
131
|
+
* @param addPadding Whether to separate the imports from the code with an empty line, and to end the source with a
|
|
132
|
+
* line break. Defaults to `true`.
|
|
133
|
+
* @returns The source of the file.
|
|
134
|
+
*/
|
|
59
135
|
toString(addPadding = true) {
|
|
60
136
|
return new SourceBuilder(this.options)
|
|
61
137
|
.if(this.packageName !== undefined, (builder) => builder.appendLine(`package ${this.packageName}`).appendLine())
|
|
@@ -65,6 +141,11 @@ export class KotlinFileBuilder extends SourceBuilder {
|
|
|
65
141
|
.if(addPadding, (builder) => builder.ensureCurrentLineEmpty())
|
|
66
142
|
.toString();
|
|
67
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Writes the source of the file to a path, creating missing parent directories. If the file already exists, the
|
|
146
|
+
* `existingFileBehavior` of the configuration decides what happens.
|
|
147
|
+
* @param filePath The path of the file to write.
|
|
148
|
+
*/
|
|
68
149
|
writeToFile(filePath) {
|
|
69
150
|
writeGeneratedFile(this.options, filePath, this.toString());
|
|
70
151
|
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { type MaybePromise, type Nullable, type OpenApiGenerationProviderContext, type OpenApiGeneratorInput } from '@goast/core';
|
|
2
2
|
import type { KotlinGeneratorConfig } from '../config.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for the generators that produce Kotlin output. It provides the naming helpers that the generators share,
|
|
5
|
+
* such as the enum constant names, which are derived from the enum values with the `enumValueNameCasing` option.
|
|
6
|
+
* Subclasses implement `generate` to produce the output for a context.
|
|
7
|
+
* @template TContext The type of the context the generator receives.
|
|
8
|
+
* @template TOutput The type of the output the generator produces.
|
|
9
|
+
*/
|
|
3
10
|
export declare abstract class KotlinFileGenerator<TContext extends OpenApiGenerationProviderContext<OpenApiGeneratorInput, KotlinGeneratorConfig>, TOutput> {
|
|
11
|
+
/**
|
|
12
|
+
* Generates the Kotlin output for the given context.
|
|
13
|
+
* @param context The context to generate the output from.
|
|
14
|
+
* @returns The generated output, or a promise of it.
|
|
15
|
+
*/
|
|
4
16
|
abstract generate(context: TContext): MaybePromise<TOutput>;
|
|
5
17
|
protected toPropertyName(context: TContext, name: string): string;
|
|
6
18
|
/**
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { toCasing, } from '@goast/core';
|
|
2
2
|
import { toKotlinPropertyName, toKotlinStringLiteral } from '../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for the generators that produce Kotlin output. It provides the naming helpers that the generators share,
|
|
5
|
+
* such as the enum constant names, which are derived from the enum values with the `enumValueNameCasing` option.
|
|
6
|
+
* Subclasses implement `generate` to produce the output for a context.
|
|
7
|
+
* @template TContext The type of the context the generator receives.
|
|
8
|
+
* @template TOutput The type of the output the generator produces.
|
|
9
|
+
*/
|
|
3
10
|
export class KotlinFileGenerator {
|
|
4
11
|
toPropertyName(context, name) {
|
|
5
12
|
return toKotlinPropertyName(toCasing(name, context.config.propertyNameCasing));
|