Skip to content

Commit

Permalink
Underscore in field name (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Jan 8, 2025
1 parent 534ac34 commit 103392c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) = """
Expand Down Expand Up @@ -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("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ open class KotlinEmitter(
.let { if (isDictionary) "Map<String, $it>" 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) = """
Expand Down Expand Up @@ -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("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CompileTypeTest {
|type Request {
| `type`: String,
| url: String,
| body: String?,
| `BODY_TYPE`: String?,
| params: String[],
| headers: { String }
|}
Expand All @@ -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<String>,
| val headers: Map<String, String>
|)
Expand All @@ -46,7 +46,7 @@ class CompileTypeTest {
|public record Request (
| String type,
| String url,
| java.util.Optional<String> body,
| java.util.Optional<String> BODY_TYPE,
| java.util.List<String> params,
| java.util.Map<String, String> headers
|) {
Expand All @@ -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]
|)
Expand All @@ -81,7 +81,7 @@ class CompileTypeTest {
|export type Request = {
| "type": string,
| "url": string,
| "body"?: string,
| "BODY_TYPE"?: string,
| "params": string[],
| "headers": Record<string, string>
|}
Expand All @@ -98,7 +98,7 @@ class CompileTypeTest {
|type Request {
| `type`: String,
| url: String,
| body: String?,
| `BODY_TYPE`: String?,
| params: String[],
| headers: { String }
|}
Expand Down

0 comments on commit 103392c

Please sign in to comment.