Skip to content

Commit 1a4f866

Browse files
committed
refactor(codegen-kotlin): FR-010 port clarity cleanup (no behavior change)
Promote isRequired, scalarKind, and buildMapOfLiteral from private to internal on KotlinRecoverSchemaEmitter and delete the three identical copies from KotlinOutputFormatSpecEmitter, which now delegates to the canonical implementations. Also drops five unused scalar-type imports from KotlinOutputFormatSpecEmitter. No emitted strings change.
1 parent 649c016 commit 1a4f866

2 files changed

Lines changed: 12 additions & 55 deletions

File tree

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

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

3-
import com.metaobjects.field.BooleanField
4-
import com.metaobjects.field.DoubleField
53
import com.metaobjects.field.EnumField
6-
import com.metaobjects.field.IntegerField
7-
import com.metaobjects.field.LongField
84
import com.metaobjects.field.MetaField
95
import com.metaobjects.field.ObjectField
10-
import com.metaobjects.field.StringField
116
import com.metaobjects.`object`.MetaObject
127
import com.metaobjects.template.MetaTemplate
138
import com.metaobjects.template.OutputTemplate
@@ -32,8 +27,8 @@ import java.util.Properties
3227
* - [BooleanField] → `FieldKind.BOOLEAN`
3328
* - [ObjectField] → `FieldKind.OBJECT`, nested arg `null` (FR-010: nested prompt deferred)
3429
*
35-
* String escaping delegates to [KotlinRecoverSchemaEmitter.kotlinStringLiteral], which
36-
* is also used by [KotlinRecoverSchemaEmitter] for the recover-schema paths.
30+
* String escaping, scalar-kind mapping, required-field resolution, and map-literal
31+
* building all delegate to [KotlinRecoverSchemaEmitter] (shared helpers, same package).
3732
*
3833
* This object is internal; generators delegate here.
3934
*/
@@ -105,7 +100,7 @@ internal object KotlinOutputFormatSpecEmitter {
105100
*/
106101
private fun promptFieldLiteral(field: MetaField<*>): String {
107102
val name = field.name
108-
val required = isRequired(field)
103+
val required = KotlinRecoverSchemaEmitter.isRequired(field)
109104
val array = field.isArray
110105

111106
// Nested object — bounded deferral (mirrors Java plan 3.1).
@@ -119,7 +114,7 @@ internal object KotlinOutputFormatSpecEmitter {
119114
}
120115

121116
// Scalar — resolve @example and @instruction.
122-
val kindName = scalarKind(field) ?: "STRING" // Unknown type: fall back to STRING.
117+
val kindName = KotlinRecoverSchemaEmitter.scalarKind(field) ?: "STRING" // Unknown type: fall back to STRING.
123118
val exampleLit = optStringAttr(field, MetaField.ATTR_EXAMPLE)
124119
val instructionLit = optStringAttr(field, MetaField.ATTR_INSTRUCTION)
125120

@@ -144,7 +139,7 @@ internal object KotlinOutputFormatSpecEmitter {
144139
val enumDocLit: String = run {
145140
if (!field.hasMetaAttr(EnumField.ATTR_ENUM_DOC, false)) return@run "null"
146141
val v = field.getMetaAttr(EnumField.ATTR_ENUM_DOC, false).value
147-
if (v is Properties && v.isNotEmpty()) buildMapOfLiteral(v) else "null"
142+
if (v is Properties && v.isNotEmpty()) KotlinRecoverSchemaEmitter.buildMapOfLiteral(v) else "null"
148143
}
149144

150145
val exampleLit = optStringAttr(field, MetaField.ATTR_EXAMPLE)
@@ -168,44 +163,4 @@ internal object KotlinOutputFormatSpecEmitter {
168163
return "\"${KotlinRecoverSchemaEmitter.kotlinStringLiteral(v)}\""
169164
}
170165

171-
/**
172-
* Returns `true` when the field carries `@required: true`.
173-
* Mirrors [KotlinRecoverSchemaEmitter]'s `isRequired` helper.
174-
*/
175-
private fun isRequired(field: MetaField<*>): Boolean =
176-
field.hasMetaAttr(MetaField.ATTR_REQUIRED)
177-
&& "true".equals(field.getMetaAttr(MetaField.ATTR_REQUIRED).valueAsString, ignoreCase = true)
178-
179-
/**
180-
* Return the `FieldKind` enum member name for a scalar field, or `null` when the
181-
* field type is not a known scalar. Matches the same instanceof order as
182-
* [KotlinRecoverSchemaEmitter.scalarKind].
183-
*/
184-
private fun scalarKind(field: MetaField<*>): String? = when (field) {
185-
is StringField -> "STRING"
186-
is IntegerField -> "INT"
187-
is LongField -> "LONG"
188-
is DoubleField -> "DOUBLE"
189-
is BooleanField -> "BOOLEAN"
190-
else -> null
191-
}
192-
193-
// -------------------------------------------------------------------------
194-
// Private helpers — source literal builders
195-
// -------------------------------------------------------------------------
196-
197-
/**
198-
* Emit a `mapOf(k to v, ...)` literal from a [Properties]. Entries are sorted by key
199-
* for deterministic output. Keys and values are escaped via
200-
* [KotlinRecoverSchemaEmitter.kotlinStringLiteral]. Kotlin's `mapOf` has no arity cap
201-
* (unlike `java.util.Map.of` which is limited to 10 pairs).
202-
*/
203-
private fun buildMapOfLiteral(props: Properties): String {
204-
val keys = props.keys.map { it.toString() }.sorted()
205-
val entries = keys.joinToString(", ") { k ->
206-
val v = props.getProperty(k)
207-
"\"${KotlinRecoverSchemaEmitter.kotlinStringLiteral(k)}\" to \"${KotlinRecoverSchemaEmitter.kotlinStringLiteral(v)}\""
208-
}
209-
return "mapOf($entries)"
210-
}
211166
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ internal object KotlinRecoverSchemaEmitter {
147147
/**
148148
* Emit a `mapOf(k to v, ...)` literal from a [Properties]. Entries are sorted by key
149149
* for deterministic output. Kotlin's `mapOf` has no arity cap (unlike `java.util.Map.of`).
150+
* Shared with [KotlinOutputFormatSpecEmitter].
150151
*/
151-
private fun buildMapOfLiteral(props: Properties): String {
152+
internal fun buildMapOfLiteral(props: Properties): String {
152153
val keys = props.keys.map { it.toString() }.sorted()
153154
val entries = keys.joinToString(", ") { k ->
154155
val v = props.getProperty(k)
@@ -160,9 +161,9 @@ internal object KotlinRecoverSchemaEmitter {
160161
/**
161162
* Return the [FieldKind] enum member name for a scalar field, or `null` when the
162163
* field type is not a known scalar. Matches the same instanceof order as
163-
* [KotlinTypeMapper.kotlinTypeName].
164+
* [KotlinTypeMapper.kotlinTypeName]. Shared with [KotlinOutputFormatSpecEmitter].
164165
*/
165-
private fun scalarKind(field: MetaField<*>): String? = when (field) {
166+
internal fun scalarKind(field: MetaField<*>): String? = when (field) {
166167
is StringField -> "STRING"
167168
is IntegerField -> "INT"
168169
is LongField -> "LONG"
@@ -234,11 +235,12 @@ internal object KotlinRecoverSchemaEmitter {
234235
// -------------------------------------------------------------------------
235236

236237
/**
237-
* Returns `true` when the field carries `@required: true`. Uses `getValueAsString()`
238+
* Returns `true` when the field carries `@required: true`. Uses `valueAsString`
238239
* to handle both the Boolean-attribute and String-attribute storage paths
239240
* (mirrors [KotlinGenUtil.isRequiredField] and the Java `RecoverSchemaEmitter`).
241+
* Shared with [KotlinOutputFormatSpecEmitter].
240242
*/
241-
private fun isRequired(field: MetaField<*>): Boolean =
243+
internal fun isRequired(field: MetaField<*>): Boolean =
242244
field.hasMetaAttr(MetaField.ATTR_REQUIRED)
243245
&& "true".equals(field.getMetaAttr(MetaField.ATTR_REQUIRED).valueAsString, ignoreCase = true)
244246

0 commit comments

Comments
 (0)