diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/LanguageSpec.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/LanguageSpec.kt index 4f380e5a..fad420f6 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/LanguageSpec.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/LanguageSpec.kt @@ -67,7 +67,7 @@ object WirespecSpec : LanguageSpec { Regex("^Unit") to WsUnit, Regex("^GET|^POST|^PUT|^DELETE|^OPTIONS|^HEAD|^PATCH|^TRACE") to Method, Regex("^[1-5][0-9][0-9]") to StatusCode, - Regex("^[a-z`][a-zA-Z0-9`]*") to CustomValue, + Regex("^[a-z`][a-zA-Z0-9_`]*") to CustomValue, Regex("^[A-Z][a-zA-Z0-9_]*") to CustomType, Regex("^/[a-zA-Z0-9-_]+") to Path, Regex("^\\/\\*(\\*(?!\\/)|[^*])*\\*\\/") to WsComment, diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt index 5eac711d..eaffcb0e 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt @@ -113,8 +113,8 @@ open class JavaEmitter( } override fun emit(identifier: Identifier) = when (identifier) { - is DefinitionIdentifier -> identifier.value.sanitizeSymbol().firstToUpper() - is FieldIdentifier -> identifier.value.sanitizeSymbol().firstToLower().sanitizeKeywords() + is DefinitionIdentifier -> identifier.value.sanitizeSymbol() + is FieldIdentifier -> identifier.value.sanitizeSymbol().sanitizeKeywords() } override fun emit(refined: Refined) = """ @@ -354,7 +354,8 @@ open class JavaEmitter( private fun String.sanitizeSymbol() = this .split(".", " ", "-") - .joinToString("") { it.firstToUpper() } + .mapIndexed { index, s -> if(index > 0) s.firstToUpper() else s } + .joinToString("") .asSequence() .filter { it.isLetterOrDigit() || it in listOf('_') } .joinToString("") diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt index cd194840..06d3bad8 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt @@ -96,8 +96,8 @@ open class KotlinEmitter( .let { if (isDictionary) "Map" else it } override fun emit(identifier: Identifier) = when (identifier) { - is DefinitionIdentifier -> identifier.value.sanitizeSymbol().firstToUpper() - is FieldIdentifier -> identifier.value.sanitizeSymbol().firstToLower().sanitizeKeywords() + is DefinitionIdentifier -> identifier.value.sanitizeSymbol() + is FieldIdentifier -> identifier.value.sanitizeSymbol().sanitizeKeywords() } override fun emit(refined: Refined) = """ @@ -294,7 +294,8 @@ open class KotlinEmitter( private fun String.sanitizeSymbol() = this .split(".", " ") - .joinToString("") { it.firstToUpper() } + .mapIndexed { index, s -> if(index > 0) s.firstToUpper() else s } + .joinToString("") .asSequence() .filter { it.isLetterOrDigit() || it in listOf('_') } .joinToString("") diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt index 8e7c8ff9..41eac194 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt @@ -8,9 +8,11 @@ import community.flock.wirespec.compiler.core.emit.common.Spacer import community.flock.wirespec.compiler.core.parse.AST import community.flock.wirespec.compiler.core.parse.Channel import community.flock.wirespec.compiler.core.parse.Definition +import community.flock.wirespec.compiler.core.parse.DefinitionIdentifier import community.flock.wirespec.compiler.core.parse.Endpoint import community.flock.wirespec.compiler.core.parse.Enum import community.flock.wirespec.compiler.core.parse.Field +import community.flock.wirespec.compiler.core.parse.FieldIdentifier import community.flock.wirespec.compiler.core.parse.Identifier import community.flock.wirespec.compiler.core.parse.Reference import community.flock.wirespec.compiler.core.parse.Refined @@ -44,8 +46,11 @@ open class WirespecEmitter(logger: Logger = noLogger) : DefinitionModelEmitter, override fun Field.emit() = "${emit(identifier)}: ${reference.emit()}${if (isNullable) "?" else ""}" - override fun emit(identifier: Identifier) = - identifier.run { if (value in reservedKeywords) value.addBackticks() else value } + override fun emit(identifier: Identifier) = when(identifier){ + is DefinitionIdentifier -> identifier.run { if (value in reservedKeywords) value.addBackticks() else value } + is FieldIdentifier -> identifier.run { if (value in reservedKeywords || value.first().isUpperCase()) value.addBackticks() else value } + } + override fun emit(channel: Channel): String = "channel ${emit(channel.identifier)} -> ${channel.reference.emit()}" diff --git a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/CompileTypeTest.kt b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/CompileTypeTest.kt index cf52c590..01473de9 100644 --- a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/CompileTypeTest.kt +++ b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/CompileTypeTest.kt @@ -14,7 +14,7 @@ class CompileTypeTest { |type Request { | `type`: String, | url: String, - | body: String?, + | `BODY_TYPE`: String?, | params: String[], | headers: { String } |} @@ -28,7 +28,7 @@ class CompileTypeTest { |data class Request( | val type: String, | val url: String, - | val body: String?, + | val BODY_TYPE: String?, | val params: List, | val headers: Map |) @@ -46,7 +46,7 @@ class CompileTypeTest { |public record Request ( | String type, | String url, - | java.util.Optional body, + | java.util.Optional BODY_TYPE, | java.util.List params, | java.util.Map headers |) { @@ -65,7 +65,7 @@ class CompileTypeTest { |case class Request( | val `type`: String, | val url: String, - | val body: Option[String], + | val BODY_TYPE: Option[String], | val params: List[String], | val headers: Map[String, String] |) @@ -81,7 +81,7 @@ class CompileTypeTest { |export type Request = { | "type": string, | "url": string, - | "body"?: string, + | "BODY_TYPE"?: string, | "params": string[], | "headers": Record |} @@ -98,7 +98,7 @@ class CompileTypeTest { |type Request { | `type`: String, | url: String, - | body: String?, + | `BODY_TYPE`: String?, | params: String[], | headers: { String } |}