Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,26 @@ four `rules.*` and `verify.lifecycle.unreachable` settings, held to `fixtures/ru
and no longer call `ruff`; the others convert one at a time, and `ruff_format` stays exported for
owned generators that call it.

- **A code writer for Kotlin generators: `KotlinWriter` in `codegen-kotlin`** (ADR-0063), on the
same `codegen-base` core the Java writer extends. It collects imports while the body is written
(`use("pkg.Name")`, `use("pkg.Name", alias)`), skips Kotlin's default imports and same-package
names, sorts them in one group, backticks keyword segments, writes KDoc, quotes strings with `$`
escaped, and breaks a call that does not fit 120 columns with a trailing comma; `block { }`,
`reopen` (`} else {`) and `indented { }` are the scoped forms. It runs the shared `core` cases
and ten new `kotlin` cases, and is on the engine surface an ejected Kotlin generator may
reference (`eject.engineSurface.kotlin.engine`). A layout ratchet (`KotlinLayoutRatchetTest`,
which reads generator source with the Kotlin compiler's lexer) fails on a string holding a line
break or starting with indentation, a repeated-space indent and a KotlinPoet import; every file
that still types its layout is on a shrink-only allowlist naming the change that converts it.
The fourteen small generators now write through it: names, filter allowlist, decimal and
duration wire, validation issues, unknown body keys, validator, extractor, output parser,
output prompt, render helper, stored proc, report export and requirement tests. **Their output
changes in layout only:** imports in sorted order, a list that fits written on one line, a
trailing comma on a broken list, a one-entry KDoc on one line, no blank line at the start of a
block, and a blank line after a file header. Regenerating shows those diffs and nothing else. Every public method an owned copy
ejected at an earlier version calls (each emitter's `write`, `BODY`, `body(wire)`,
`KotlinNaming.packageHeader`) is kept.

- **Python publishes a shared model (FR-023).** A `shared-model` generator, wired in
`metaobjects.config.yaml` like any other (`metaobjects gen --list` names its options), writes
`<name>.metaobjects.json` and `metaobjects.pkg.json` for a consumer's `deps sync`: `name` and
Expand Down
2 changes: 1 addition & 1 deletion docs/CONFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ regenerate with `ls -d fixtures/<corpus>/*/ | wc -l` for directory-shaped corpor
| [`fixtures/codegen-skip-conformance/`](../fixtures/codegen-skip-conformance/) (when `gen` may skip and what a local `verify --codegen` may answer, ADR-0064) | one case table in `cases.json`, plus the manifest layout vectors in `manifests.json` (ADR-0064 Amendment 1) | ✓ | ✓ | ✓ (through the Java runner) | ✓ | ✓ | ✓ (every case but the three-way merge one) |
| [`fixtures/codegen-noop/`](../fixtures/codegen-noop/) (vocabulary with no lowering yet: FR-044 reporting, what is lowered and what stays inert; ADR-0066 business rules, inert everywhere) | 2 model pairs (`reporting/` and `rules/`, each `with` vs `without`) | ✓ (codegen + migrate) | ✓ | ✓ | ✓ | ✓ | `rules/` (the layer generators lower it; the flat ones stay inert) |
| [`fixtures/cube-model/`](../fixtures/cube-model/) (FR-044 Plan 4 — the `cube-model` generator's golden Cube files, plus the canonical model's golden) | 46 cases (36 trees + 10 errors) + 1 canonical golden | ✓ (the exporter is TypeScript-only, spec R6; its `cube` lane also loads the files into a real Cube) | — | — | — | — | — |
| [`fixtures/writer-conformance/`](../fixtures/writer-conformance/) (ADR-0063 — the code writer generators write through) | 76 cases (31 core + 10 TypeScript + 9 Java + 6 C# + 12 Python + 8 Rust) | ✓ (the first writer; core and TypeScript dialect) | ✓ (core and Java dialect; `JavaWriter` in `codegen-base`) | — (writer not built yet) | ✓ (core and C# dialect) | ✓ (core and Python dialect) | ✓ (core and Rust dialect) |
| [`fixtures/writer-conformance/`](../fixtures/writer-conformance/) (ADR-0063 — the code writer generators write through) | 86 cases (31 core + 10 TypeScript + 9 Java + 10 Kotlin + 6 C# + 12 Python + 8 Rust) | ✓ (the first writer; core and TypeScript dialect) | ✓ (core and Java dialect; `JavaWriter` in `codegen-base`) | ✓ (core and Kotlin dialect; `KotlinWriter` in `codegen-kotlin`) | ✓ (core and C# dialect) | ✓ (core and Python dialect) | ✓ (core and Rust dialect) |
| [`fixtures/contract-conformance/`](../fixtures/contract-conformance/) (FR-022 — the `contract-schema` and `openapi` generators' golden trees and refusals) | 22 cases (17 trees + 5 errors) | ✓ (one neutral engine, ADR-0034 Amendment 10; the generated API lanes also hold real traffic to the documents) | — | — | — | — | — |
| [`fixtures/explorer-conformance/`](../fixtures/explorer-conformance/) (the `explorer.json` data spine: `meta export --effective` + `meta verify --format json` + the generated-file manifest in, one explorer document out) | 1 case (`shop`) | ✓ (the builder is TypeScript, and reads only port-neutral inputs) | — | — | — | — | — |

Expand Down
3 changes: 2 additions & 1 deletion docs/features/own-your-codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,8 @@ runner types, the naming and type-mapping tables (`SpringNaming`, `SpringTypeMap
`KotlinNaming`, `KotlinTypeMapper`), the questions a generator asks of the model
(`FindInbound`, `TphPlan`, the M:N support, `RestSurfaceGate`), the code writer the Java
generators lay their output out with (`CodeWriter`, `JavaWriter`, `WriterOptions`, and
`SpringJavaWriter` for Spring source), and the Kotlin
`SpringJavaWriter` for Spring source), the code writer the Kotlin generators lay their output
out with (`KotlinWriter`, on the same `CodeWriter`), and the Kotlin
`Fr019SharedEnum`. Anything that returns emitted text is handed over; a table or a model
question is not. The Kotlin `Fr019SharedEnum` writes no output. It answers where a shared or
`@provided` enum lives, and the type mapper and the generator utilities take its
Expand Down
5 changes: 5 additions & 0 deletions docs/ports/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ your own `<groupId>`, `<artifactId>` and `<version>`:

## Configure

Generators that have moved to the code writer lay their output out with `KotlinWriter`
(ADR-0063; the dialect and its ops are in [spec/code-writer.md](../../spec/code-writer.md#kotlin-dialect-kotlinwriter)).
The layout ratchet (`KotlinLayoutRatchetTest`) keeps the emitters still on KotlinPoet on a
shrink-only allowlist.

The 18 generators registered in `codegen-kotlin` (`GeneratorRegistry.kt`):

| Generator | Output | Per |
Expand Down
6 changes: 5 additions & 1 deletion fixtures/generator-registry-conformance/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,16 @@
"com.metaobjects.generator.kotlin.KotlinTphPlan",
"com.metaobjects.generator.kotlin.KotlinTypeMapper",
"com.metaobjects.generator.kotlin.PackageMapping",
"com.metaobjects.generator.kotlin.writer.KotlinWriter",
"com.metaobjects.generator.requirement.RenderedTest",
"com.metaobjects.generator.requirement.RequirementTestArgs",
"com.metaobjects.generator.requirement.RequirementTestRenderer",
"com.metaobjects.generator.util.GeneratedFileWriter",
"com.metaobjects.generator.util.GeneratorUtil",
"com.metaobjects.generator.util.RestSurfaceGate"
"com.metaobjects.generator.util.RestSurfaceGate",
"com.metaobjects.generator.writer.CodeWriter",
"com.metaobjects.generator.writer.Group",
"com.metaobjects.generator.writer.WriterOptions"
]
},
"python": {
Expand Down
8 changes: 5 additions & 3 deletions fixtures/writer-conformance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ format are in [`spec/code-writer.md`](../../spec/code-writer.md).
- `cases/*.json` with `"dialect": "core"` pin the language-neutral core: indentation, blocks,
blank-line requests, snippets, fit-or-break lists, line endings and the error codes. Every
port's writer runs them with the options the case states.
- Cases with a language dialect (`"dialect": "typescript"`, `"java"`, `"python"`, `"csharp"`, `"rust"`) pin that
- Cases with a language dialect (`"dialect": "typescript"`, `"java"`, `"kotlin"`, `"python"`, `"csharp"`, `"rust"`) pin that
language's writer: import or `using` collection and ordering, doc comments and its defaults. A
runner runs the `core` cases and its own dialect's, and skips the others by name.

Runners:

- TypeScript, `server/typescript/packages/codegen-ts/test/writer-conformance.test.ts` (`core` and `typescript`).
- Java, `server/java/codegen-base/src/test/java/com/metaobjects/generator/writer/WriterConformanceTest.java` (`core` and `java`).
- Kotlin, `server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/writer/KotlinWriterConformanceTest.kt` (`core` and `kotlin`).
- Python, `server/python/tests/codegen/test_writer_conformance.py` (`core` and `python`).
- Rust, `server/rust/metaobjects-codegen/tests/writer_conformance.rs` (`core` and `rust`).
- C#, `server/csharp/MetaObjects.Codegen.Tests/WriterConformanceTests.cs` (`core` and `csharp`).

The ports without a writer yet (Kotlin) run the `core` cases when their writers
land, and add cases for their own dialect beside these.
The `kotlin` ops are `package` (a name), `use` (a name), `useAs` (a name, then the alias),
`header` and `doc` (lines) and `reopen` (parts), beside the core ops. A `kotlin` case takes the
dialect's defaults unless it states options.

The `csharp` ops are `header` (lines), `using`, `usingStatic` (a namespace or type), `usingAlias`
(alias, target) and `docSummary` (lines), beside the core ops. A `csharp` case may state
Expand Down
60 changes: 60 additions & 0 deletions fixtures/writer-conformance/cases/kotlin-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"description": "The Kotlin dialect indents four spaces, breaks at 120 columns and puts a comma after the last item of a broken list, as Kotlin's coding conventions write it.",
"dialect": "kotlin",
"ops": [
[
"open",
"object A {"
],
[
"line",
"val NAMES: List<String> = ",
{
"list": {
"open": "listOf(",
"items": [
"\"n00\"",
"\"n01\"",
"\"n02\"",
"\"n03\"",
"\"n04\"",
"\"n05\"",
"\"n06\"",
"\"n07\"",
"\"n08\"",
"\"n09\"",
"\"n10\"",
"\"n11\"",
"\"n12\"",
"\"n13\""
],
"close": ")"
}
}
],
[
"close",
"}"
]
],
"expected": [
"object A {",
" val NAMES: List<String> = listOf(",
" \"n00\",",
" \"n01\",",
" \"n02\",",
" \"n03\",",
" \"n04\",",
" \"n05\",",
" \"n06\",",
" \"n07\",",
" \"n08\",",
" \"n09\",",
" \"n10\",",
" \"n11\",",
" \"n12\",",
" \"n13\",",
" )",
"}"
]
}
44 changes: 44 additions & 0 deletions fixtures/writer-conformance/cases/kotlin-doc-comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"description": "A one-entry KDoc that fits is one line; more entries, or one that does not fit, make a block. An empty entry is a bare ` *` line. Kotlin comments nest, so a comment opener or terminator inside the text is neutralised.",
"dialect": "kotlin",
"ops": [
[
"doc",
"The user."
],
[
"line",
"a()"
],
[
"doc",
"First line.",
"",
"@see other"
],
[
"line",
"b()"
],
[
"doc",
"ends */ here, opens /* there"
],
[
"line",
"c()"
]
],
"expected": [
"/** The user. */",
"a()",
"/**",
" * First line.",
" *",
" * @see other",
" */",
"b()",
"/** ends *&#47; here, opens &#47;* there */",
"c()"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"description": "reopen() with no block open is an unbalanced close.",
"dialect": "kotlin",
"ops": [
[
"reopen",
"} else {"
]
],
"error": "ERR_WRITER_UNBALANCED_CLOSE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"description": "Header lines print first, then a blank line, the package line (no semicolon), a blank line, the imports, a blank line and the body.",
"dialect": "kotlin",
"ops": [
[
"header",
"// Generated by MetaObjects. Do not edit."
],
[
"package",
"com.acme.api"
],
[
"use",
"java.time.Instant"
],
[
"open",
"interface A {"
],
[
"line",
"fun at(): Instant"
],
[
"close",
"}"
]
],
"expected": [
"// Generated by MetaObjects. Do not edit.",
"",
"package com.acme.api",
"",
"import java.time.Instant",
"",
"interface A {",
" fun at(): Instant",
"}"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"description": "Imports print in code-unit order, one per line, with no blank line between packages. A name used twice is imported once, and an aliased import sorts by its qualified name.",
"dialect": "kotlin",
"ops": [
[
"package",
"com.acme"
],
[
"use",
"java.util.UUID"
],
[
"use",
"com.acme.other.Zed"
],
[
"useAs",
"org.jetbrains.exposed.sql.Table",
"ExposedTable"
],
[
"use",
"java.time.Instant"
],
[
"use",
"java.time.Instant"
],
[
"line",
"class A"
]
],
"expected": [
"package com.acme",
"",
"import com.acme.other.Zed",
"import java.time.Instant",
"import java.util.UUID",
"import org.jetbrains.exposed.sql.Table as ExposedTable",
"",
"class A"
]
}
15 changes: 15 additions & 0 deletions fixtures/writer-conformance/cases/kotlin-imports-conflict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"description": "One local name imported from two places is an error, not a silent shadow; an alias is how a generator says both.",
"dialect": "kotlin",
"ops": [
[
"use",
"java.util.Date"
],
[
"use",
"java.sql.Date"
]
],
"error": "ERR_WRITER_IMPORT_CONFLICT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"description": "A name in a package Kotlin imports by default (kotlin, kotlin.collections, java.lang and the rest) or in the file's own package imports nothing, whichever order the package and the names were given in; a subpackage of a default package is imported.",
"dialect": "kotlin",
"ops": [
[
"use",
"kotlin.collections.LinkedHashMap"
],
[
"use",
"java.lang.IllegalStateException"
],
[
"use",
"com.acme.Sibling"
],
[
"use",
"kotlin.math.abs"
],
[
"package",
"com.acme"
],
[
"line",
"class A"
]
],
"expected": [
"package com.acme",
"",
"import kotlin.math.abs",
"",
"class A"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"description": "A hard keyword used as a package or import segment is written in backticks, so the line parses.",
"dialect": "kotlin",
"ops": [
[
"package",
"com.acme.object"
],
[
"use",
"com.metaobjects.object.MetaObject"
],
[
"line",
"class A"
]
],
"expected": [
"package com.acme.`object`",
"",
"import com.metaobjects.`object`.MetaObject",
"",
"class A"
]
}
Loading
Loading