Skip to content

Commit 6d2624c

Browse files
dmealingclaude
andcommitted
refactor(codegen): one naming layer per port for a value object's types
A cleanup pass over the ADR-0056 diff, kept where four suites prove it behaviour-neutral. - Kotlin: strictRef / mirrorRef / requireReferenceable move from KotlinExtractSchemaEmitter to KotlinNaming, so every tier asks the naming layer for a value object's types instead of an emitter. The "a no-package template emits into the root package, where no `package` line may appear" rule is spelled once as KotlinNaming.packageHeader, not in four generators. - Python: the `_pkg_of` helper was copied into four modules; they now import the shared value_objects.pkg_of, which itself defers to naming.package_of_resolution_key. object_ref_target caches its per-field resolution (the tree is read-only after load, and the model/type-map/read/create/patch tiers each resolve the same ref). - Java: the render PayloadAccessors reflects a record's components once per class rather than per node per render; Spring/apidocs doc comments now name the symbols that exist after ADR-0056 removed the payload tier. - TypeScript: the `ctx ? ctx.valueObjectEmittedName(vo) : vo.name` conditional is emittedVoName(), called by the five sites that re-spelled it. The C# half of the pass was REVERTED, not kept: it dropped an optional parameter from the public CSharpNaming.MapValueType (an API break a cleanup may not make in a patch) and narrowed EntityGenerator.ValueShapes to abstract value objects only, which changes which POCOs a run emits. Neither is a simplification. Verified on this tree: Python 2254 passed; mvn render+codegen-spring+codegen-kotlin BUILD SUCCESS (385 Kotlin tests); codegen-ts 1687 passed + typecheck clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M1mRRB1K2WdYy82kntMQdx
1 parent 5de4623 commit 6d2624c

26 files changed

Lines changed: 189 additions & 178 deletions

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEnumEmitter.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.nio.file.Path
1414
* verbatim) — is emitted by exactly one piece of code, owned by the object that declares the field
1515
* (the template tier references these enums and emits none, ADR-0056).
1616
*
17-
* <p>Cross-port parity: the Kotlin sibling of the C# `PayloadCodegen.CollectEnumDecls`,
17+
* <p>Cross-port parity: the Kotlin sibling of the C# `EntityGenerator` enum emission,
1818
* the TS union emitter, and the Python `Literal` emitter — all of which reuse the entity
1919
* enum naming/members for the payload path.</p>
2020
*/

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExtractSchemaEmitter.kt‎

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,9 @@ internal object KotlinExtractSchemaEmitter {
5454
fun mirrorName(vo: MetaObject): String =
5555
KotlinNaming.extractedName(PackageMapping.splitFqn(vo.name).second)
5656

57-
/** The mirror class reference for [vo], fully qualified unless [vo] has no package. */
58-
fun mirrorRef(vo: MetaObject): String =
59-
qualified(PackageMapping.splitFqn(vo.name).first, mirrorName(vo))
60-
61-
/** The strict value-object data-class reference for [vo], as [KotlinEntityGenerator] names it. */
62-
fun strictRef(vo: MetaObject): String {
63-
val (pkg, short) = PackageMapping.splitFqn(vo.name)
64-
return qualified(pkg, short)
65-
}
66-
67-
/**
68-
* Fail codegen when a file in [fromPkg] would have to reference [vo]'s root-package types:
69-
* Kotlin cannot name a root-package class from a named package, so the emitted file would not
70-
* compile. [what] names the referring template, for the message.
71-
*/
72-
fun requireReferenceable(fromPkg: String, vo: MetaObject, what: String) {
73-
if (fromPkg.isEmpty() || PackageMapping.splitFqn(vo.name).first.isNotEmpty()) return
74-
throw GeneratorException(
75-
"$what references value object '${vo.name}', which has no package. Kotlin cannot " +
76-
"reference a root-package class from the named package '$fromPkg' — declare " +
77-
"'${vo.name}' in a package"
78-
)
79-
}
57+
// strictRef / mirrorRef / requireReferenceable now live in KotlinNaming — the naming
58+
// layer every tier asks to reference a value object's types (ADR-0056); this emitter is
59+
// a consumer like the others.
8060

8161
/**
8262
* Write the mirror file for [rootVo] and for every value object it reaches through a declared
@@ -90,7 +70,7 @@ internal object KotlinExtractSchemaEmitter {
9070
GeneratedFileWriter.write(outFile, mirrorSource(rootVo))
9171
for (field in rootVo.metaFields) {
9272
val nested = objectRefValueObject(field) ?: continue
93-
requireReferenceable(pkg, nested, "value object '${rootVo.name}'")
73+
KotlinNaming.requireReferenceable(pkg, nested, "value object '${rootVo.name}'")
9474
emitMirrorFiles(nested, outRoot, emitted)
9575
}
9676
}
@@ -166,7 +146,7 @@ internal object KotlinExtractSchemaEmitter {
166146
private fun mirrorPropertyType(field: MetaField<*>): String {
167147
val target = objectRefValueObject(field)
168148
if (target != null) {
169-
return if (field.isArrayType()) "List<${mirrorRef(target)}>?" else "${mirrorRef(target)}?"
149+
return if (field.isArrayType()) "List<${KotlinNaming.mirrorRef(target)}>?" else "${KotlinNaming.mirrorRef(target)}?"
170150
}
171151
return when {
172152
// ADR-0039: resolving array-ness (isArray is the own-only native flag).
@@ -192,7 +172,7 @@ internal object KotlinExtractSchemaEmitter {
192172
val key = "\"${kotlinStringLiteral(field.name)}\""
193173
val target = objectRefValueObject(field)
194174
if (target != null) {
195-
val nested = mirrorRef(target)
175+
val nested = KotlinNaming.mirrorRef(target)
196176
// `it` is a non-null Map here, so fromMap never returns null — `!!` keeps the element
197177
// type non-null to match the List<Nested> mirror property.
198178
return if (field.isArrayType()) "mapObjectList(d, $key) { $nested.fromMap(it)!! }"
@@ -315,8 +295,6 @@ internal object KotlinExtractSchemaEmitter {
315295
}
316296
}
317297

318-
private fun qualified(pkg: String, name: String): String = if (pkg.isEmpty()) name else "$pkg.$name"
319-
320298
// -------------------------------------------------------------------------
321299
// Shared source-emission utilities
322300
// -------------------------------------------------------------------------

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExtractorGenerator.kt‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ open class KotlinExtractorGenerator : MultiFileDirectGeneratorBase<MetaObject>()
6464
val outPkg = KotlinNaming.promptsPackage(templatePkg)
6565
val extractorClass = KotlinNaming.extractorName(templateShort)
6666
val parserClass = KotlinNaming.parserName(templateShort)
67-
KotlinExtractSchemaEmitter.requireReferenceable(outPkg, responseVo, "template '${template.name}'")
68-
val strict = KotlinExtractSchemaEmitter.strictRef(responseVo)
69-
val mirror = KotlinExtractSchemaEmitter.mirrorRef(responseVo)
67+
KotlinNaming.requireReferenceable(outPkg, responseVo, "template '${template.name}'")
68+
val strict = KotlinNaming.strictRef(responseVo)
69+
val mirror = KotlinNaming.mirrorRef(responseVo)
7070

7171
val src = buildString {
7272
append("// GENERATED — DO NOT EDIT — extractor for template.prompt `")
7373
append(template.name)
7474
append("`\n")
75-
// A no-package template emits into the root package (see KotlinNaming.promptsPackage).
76-
if (outPkg.isNotEmpty()) append("package ").append(outPkg).append("\n\n")
75+
append(KotlinNaming.packageHeader(outPkg))
7776
append("import com.metaobjects.loader.MetaDataLoader\n")
7877
append("import com.metaobjects.render.extract.ExtractException\n")
7978
append("import com.metaobjects.render.extract.ExtractOptions\n")

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinNaming.kt‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.metaobjects.generator.kotlin
22

3+
import com.metaobjects.`object`.MetaObject
4+
import com.metaobjects.generator.GeneratorException
35
import com.metaobjects.generator.util.RouteNaming
46

57
/**
@@ -129,6 +131,45 @@ object KotlinNaming {
129131
*/
130132
fun promptsPackage(pkg: String): String = if (pkg.isEmpty()) "" else "$pkg.prompts"
131133

134+
/**
135+
* The `package` file header for a template-tier emission into [pkg]. A no-package template
136+
* emits into the ROOT package (see [promptsPackage]), where no `package` declaration may
137+
* appear — the one spelling of that rule for every template-tier generator.
138+
*/
139+
fun packageHeader(pkg: String): String = if (pkg.isEmpty()) "" else "package $pkg\n\n"
140+
141+
/**
142+
* The strict value-object data-class reference for [vo], as [KotlinEntityGenerator] names it —
143+
* fully qualified unless [vo] has no package. The ONE way any tier names the entity-side type
144+
* (ADR-0056); mirrors Java's `SpringNaming.valueObjectRef`.
145+
*/
146+
fun strictRef(vo: MetaObject): String {
147+
val (pkg, short) = PackageMapping.splitFqn(vo.name)
148+
return if (pkg.isEmpty()) short else "$pkg.$short"
149+
}
150+
151+
/** The mirror class reference for [vo] — [strictRef]'s package with [extractedName]'s name;
152+
* the mirror is emitted beside its value object (ADR-0056). */
153+
fun mirrorRef(vo: MetaObject): String {
154+
val (pkg, short) = PackageMapping.splitFqn(vo.name)
155+
val name = extractedName(short)
156+
return if (pkg.isEmpty()) name else "$pkg.$name"
157+
}
158+
159+
/**
160+
* Fail codegen when a file in [fromPkg] would have to reference [vo]'s root-package types:
161+
* Kotlin cannot name a root-package class from a named package, so the emitted file would not
162+
* compile. [what] names the referring site, for the message.
163+
*/
164+
fun requireReferenceable(fromPkg: String, vo: MetaObject, what: String) {
165+
if (fromPkg.isEmpty() || PackageMapping.splitFqn(vo.name).first.isNotEmpty()) return
166+
throw GeneratorException(
167+
"$what references value object '${vo.name}', which has no package. Kotlin cannot " +
168+
"reference a root-package class from the named package '$fromPkg' — declare " +
169+
"'${vo.name}' in a package"
170+
)
171+
}
172+
132173
/**
133174
* [KotlinExtractSchemaEmitter]: `voShort + "Extracted"` — the all-nullable extraction mirror
134175
* of a value object, emitted beside it (ADR-0056). Keyed by the VALUE OBJECT's short name,

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinOutputParserGenerator.kt‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ open class KotlinOutputParserGenerator : MultiFileDirectGeneratorBase<MetaObject
9797
val (templatePkg, templateShort) = PackageMapping.splitFqn(template.name)
9898
val outPkg = KotlinNaming.promptsPackage(templatePkg)
9999
val parserClass = KotlinNaming.parserName(templateShort)
100-
KotlinExtractSchemaEmitter.requireReferenceable(outPkg, responseVo, "template '${template.name}'")
100+
KotlinNaming.requireReferenceable(outPkg, responseVo, "template '${template.name}'")
101101
// ADR-0056 — the parse result IS the response value object's own data class.
102-
val responseClass = KotlinExtractSchemaEmitter.strictRef(responseVo)
103-
val mirrorClass = KotlinExtractSchemaEmitter.mirrorRef(responseVo)
102+
val responseClass = KotlinNaming.strictRef(responseVo)
103+
val mirrorClass = KotlinNaming.mirrorRef(responseVo)
104104
val parseFn = "parse$templateShort"
105105
val safeParseFn = "safeParse$templateShort"
106106

@@ -119,8 +119,7 @@ open class KotlinOutputParserGenerator : MultiFileDirectGeneratorBase<MetaObject
119119
append("// GENERATED — DO NOT EDIT — response parser for template.prompt `")
120120
append(template.name)
121121
append("`\n")
122-
// A no-package template emits into the root package (see KotlinNaming.promptsPackage).
123-
if (outPkg.isNotEmpty()) append("package ").append(outPkg).append("\n\n")
122+
append(KotlinNaming.packageHeader(outPkg))
124123
if (emitStrict) append("import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper\n")
125124
append("import com.metaobjects.loader.MetaDataLoader\n")
126125
append("import com.metaobjects.`object`.extract.MetaObjectExtractor\n")

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinOutputPromptGenerator.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ open class KotlinOutputPromptGenerator : MultiFileDirectGeneratorBase<MetaObject
101101
append("// GENERATED — DO NOT EDIT — output-format prompt for template.output `")
102102
append(template.name)
103103
append("`\n")
104-
// A no-package template emits into the root package (see KotlinNaming.promptsPackage).
105-
if (outPkg.isNotEmpty()) append("package ").append(outPkg).append("\n\n")
104+
append(KotlinNaming.packageHeader(outPkg))
106105
append("import com.metaobjects.render.prompt.OutputFormatRenderer\n")
107106
append("import com.metaobjects.render.prompt.OutputFormatSpec\n")
108107
append("import com.metaobjects.render.prompt.PromptField\n")

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRenderHelperGenerator.kt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ open class KotlinRenderHelperGenerator : MultiFileDirectGeneratorBase<MetaObject
105105
// ADR-0056 — the payload IS the @payloadRef value object's own data class, the one
106106
// KotlinEntityGenerator emits in the value object's package. Referenced fully qualified;
107107
// this generator declares no payload type of its own.
108-
KotlinExtractSchemaEmitter.requireReferenceable(outPkg, payloadVo, "template '${template.name}'")
109-
val payloadClass = KotlinExtractSchemaEmitter.strictRef(payloadVo)
108+
KotlinNaming.requireReferenceable(outPkg, payloadVo, "template '${template.name}'")
109+
val payloadClass = KotlinNaming.strictRef(payloadVo)
110110

111111
// Payload field tree — reused by the build-time gate AND baked into the emitted
112112
// RenderRequest.verify so the runtime check matches the gate.
@@ -174,8 +174,7 @@ open class KotlinRenderHelperGenerator : MultiFileDirectGeneratorBase<MetaObject
174174
append("// GENERATED — DO NOT EDIT — render helper for template.output `")
175175
append(template.name)
176176
append("`\n")
177-
// A no-package template emits into the root package (see KotlinNaming.promptsPackage).
178-
if (outPkg.isNotEmpty()) append("package ").append(outPkg).append("\n\n")
177+
append(KotlinNaming.packageHeader(outPkg))
179178
append("/** Typed render helper for the `")
180179
append(templateShort)
181180
append("` template.output. Wraps the JVM render() engine; the payload field tree is\n")

‎server/java/codegen-spring/src/main/java/com/metaobjects/generator/apidocs/JavaApiModelBuilder.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353
* <ul>
5454
* <li>DTO and VALIDATION symbols carry the entity's per-field shape via
5555
* {@link JavaFieldShapes#dtoFields(MetaObject)}; PAYLOAD symbols carry the
56-
* template's payload shape via
57-
* {@link JavaFieldShapes#payloadFields(com.metaobjects.MetaData, MetaDataLoader)}.
58-
* Both derive types/optionality from the real generators (drift-proof).</li>
56+
* response/payload value object's own record shape via
57+
* {@link JavaFieldShapes#payloadFieldsOf(com.metaobjects.object.MetaObject, MetaDataLoader)},
58+
* the record {@link #payloadValueObject} resolves to. Both derive types/optionality
59+
* from the real generators (drift-proof).</li>
5960
* <li>A VALIDATION symbol is emitted alongside each DTO (the Jakarta
6061
* constraints live on the DTO record), carrying the same required/optional
6162
* field shape.</li>

‎server/java/codegen-spring/src/main/java/com/metaobjects/generator/apidocs/JavaFieldShapes.java‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,12 @@ public static List<FieldShape> dtoFields(MetaObject entity) {
7171
return out;
7272
}
7373

74-
/**
75-
* The documented field shapes of a template's {@code @payloadRef} value object — see
76-
* {@link #payloadFieldsOf(MetaObject, MetaDataLoader)}. Empty when the template carries no
77-
* resolvable payload (defensive — callers gate on a resolvable ref first).
78-
*/
79-
public static List<FieldShape> payloadFields(MetaData template, MetaDataLoader loader) {
80-
if (!(template instanceof MetaTemplate tmpl)) return List.of();
81-
String payloadRef = tmpl.getPayloadRef();
82-
if (payloadRef == null || payloadRef.isEmpty()) return List.of();
83-
MetaObject vo = SpringNaming.resolveValueObjectRef(
84-
loader, payloadRef, MetaDataUtil.findPackageForMetaData(tmpl));
85-
if (vo == null) return List.of();
86-
return payloadFieldsOf(vo, loader);
87-
}
88-
8974
/**
9075
* The documented field shapes of a value object's record — the type a template renders as
91-
* its payload, or a responding prompt parses its reply into. Uses the record generator's own
92-
* component typing and validation derivation, so the docs cannot drift from the record.
76+
* its payload, or a responding prompt parses its reply into (ADR-0056: the record is the
77+
* value object's own, so the builder resolves the {@code @payloadRef} and hands the target
78+
* here). Uses the record generator's own component typing and validation derivation, so
79+
* the docs cannot drift from the record.
9380
*/
9481
public static List<FieldShape> payloadFieldsOf(MetaObject vo, MetaDataLoader loader) {
9582
if (vo == null) return List.of();

‎server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNaming.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,25 @@ public static String promptsPackage(String pkg) {
227227
return pkg.isEmpty() ? "" : pkg + ".prompts";
228228
}
229229

230+
/**
231+
* The {@code package} file header for a template-tier emission into {@code pkg}. A
232+
* no-package template emits into the ROOT package (see {@link #promptsPackage}), where no
233+
* {@code package} declaration may appear — the one spelling of that rule for every
234+
* template-tier generator.
235+
*/
236+
public static String packageHeader(String pkg) {
237+
return pkg.isEmpty() ? "" : "package " + pkg + ";\n\n";
238+
}
239+
230240
/**
231241
* The fully-qualified reference to the record {@link SpringValueObjectGenerator} emits for
232242
* {@code vo} — what every template-tier generator names a payload or response by (ADR-0056).
233243
* Bare when {@code vo} has no package.
234244
*/
235245
public static String valueObjectRef(MetaObject vo) {
236-
String[] split = splitFqn(vo.getName());
237-
return split[0].isEmpty() ? split[1] : split[0] + "." + split[1];
246+
// The same pkg::Short → pkg.Short rule the entity tier's DTOs bind with — one ladder,
247+
// so the template tier cannot name the record differently from the DTO that embeds it.
248+
return SpringTypeMapper.fqJavaTypeName(vo);
238249
}
239250

240251
/**

0 commit comments

Comments
 (0)