Skip to content

Commit

Permalink
Reference internal classes full qualified (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored May 16, 2024
1 parent c3ef1d5 commit cbb77a9
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ class JavaEmitter(

override fun Reference.Generics.emit(): String = references
.takeIf { it.isNotEmpty() }
?.joinToString(", ", "<", ">") { it.emitWrap() }
?.map { it.emitWrap() }
?.joinToString(", ", "<", ">") { it }
.orEmpty()

override fun Reference.emit(): String = when (this) {
Expand All @@ -288,7 +289,7 @@ class JavaEmitter(
.let { if (isOptional) "java.util.Optional<$it>" else it }

override fun Reference.Custom.emit(): String = """
|${name.sanitizeSymbol()}${generics.emit()}
|${if (name in internalClasses && !isInternal) "${packageName}." else ""}${name.sanitizeSymbol()}${generics.emit()}
""".trimMargin()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class KotlinEmitter(
"Wirespec.${name}${generics.emit()}"

override fun Reference.Custom.emit(): String = """
|${name.sanitizeSymbol()}${generics.emit()}
|${if (name in internalClasses && !isInternal) "${packageName}." else ""}${name.sanitizeSymbol()}${generics.emit()}
""".trimMargin()

override fun Reference.Language.emit(): String = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class TypeScriptEmitter(logger: Logger = noLogger) : DefinitionModelEmitter, Emi
private fun Identifier.sanitizeSymbol() = value.sanitizeSymbol()

private fun String.sanitizeSymbol() = asSequence()
.filter { it.isLetterOrDigit() || listOf('_').contains(it) }
.filter { it.isLetterOrDigit() || it in listOf('_') }
.joinToString("")

private fun Field.Reference.toField(identifier: String, isNullable: Boolean) = Field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ abstract class Emitter(
fun AST.hasEndpoints() = any { it is Endpoint }
fun String.isInt() = toIntOrNull() != null
fun String.isStatusCode() = toIntOrNull()?.let { it in 0..599 } ?: false
val internalClasses = setOf(
"Request", "Response"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@ sealed interface Reference {
val isNullable: Boolean
val isIterable: Boolean
val isOptional: Boolean
val isInternal: Boolean

data class Language(
val primitive: Primitive,
override val isNullable: Boolean = false,
override val isIterable: Boolean = false,
override val isOptional: Boolean = false,
override val isInternal: Boolean = false,
val generics: Generics = Generics()
) : Reference {
enum class Primitive { Any, Unit, String, Integer, Long, Double, Number, Boolean, Map, List }
Expand All @@ -163,6 +165,7 @@ sealed interface Reference {
override val isNullable: Boolean = false,
override val isIterable: Boolean = false,
override val isOptional: Boolean = false,
override val isInternal: Boolean = false,
val generics: Generics = Generics()
) : Reference

Expand All @@ -171,6 +174,7 @@ sealed interface Reference {
override val isNullable: Boolean = false,
override val isIterable: Boolean = false,
override val isOptional: Boolean = false,
override val isInternal: Boolean = false,
val generics: Generics = Generics()
) : Reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ object ClassModelTransformer : Transformer {
supers = listOf(
Reference.Custom(
name = className("Request"),
isInternal = true,
generics = Reference.Generics(
listOf(
it.content?.reference?.transform(it.content.isNullable, false)
Expand All @@ -289,7 +290,10 @@ object ClassModelTransformer : Transformer {
EndpointClass.RequestMapper.RequestCondition(
content = it.content?.transform(),
isIterable = it.content?.reference?.isIterable ?: false,
responseReference = Reference.Custom(className("Request", it.content.name()))
responseReference = Reference.Custom(
name = className("Request", it.content.name()),
isInternal = true
)
)
}
),
Expand All @@ -301,7 +305,8 @@ object ClassModelTransformer : Transformer {
content = it.content?.transform(),
isIterable = it.content?.reference?.isIterable ?: false,
responseReference = Reference.Custom(
name = className("Response", it.status, it.content.name())
name = className("Response", it.status, it.content.name()),
isInternal = true
)
)
}
Expand All @@ -313,6 +318,7 @@ object ClassModelTransformer : Transformer {
EndpointClass.ResponseInterface(
name = Reference.Custom(
name = className("Response", it),
isInternal = true,
generics = Reference.Generics(
listOf(
Reference.Custom("T")
Expand All @@ -321,6 +327,7 @@ object ClassModelTransformer : Transformer {
),
`super` = Reference.Custom(
name = "Response",
isInternal = true,
generics = Reference.Generics(
listOf(
Reference.Custom("T")
Expand All @@ -336,6 +343,7 @@ object ClassModelTransformer : Transformer {
EndpointClass.ResponseInterface(
name = Reference.Custom(
name = className("Response", it),
isInternal = true,
generics = Reference.Generics(
listOf(
Reference.Custom("T")
Expand All @@ -344,6 +352,7 @@ object ClassModelTransformer : Transformer {
),
`super` = Reference.Custom(
name = className("Response", it.groupStatus()),
isInternal = true,
generics = Reference.Generics(
listOf(
Reference.Custom("T")
Expand Down Expand Up @@ -487,6 +496,7 @@ object ClassModelTransformer : Transformer {
)
),
isNullable = false,
isInternal = true
),
statusCode = it.status,
content = it.content?.transform(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import community.flock.wirespec.compiler.core.exceptions.WirespecException.Compi
import community.flock.wirespec.compiler.core.tokenize.Token
import community.flock.wirespec.compiler.core.tokenize.Tokens
import community.flock.wirespec.compiler.core.tokenize.removeWhiteSpace
import community.flock.wirespec.compiler.core.tokenize.types.CustomType
import community.flock.wirespec.compiler.core.tokenize.types.WirespecDefinition
import community.flock.wirespec.compiler.utils.Logger

Expand All @@ -31,7 +30,7 @@ class TokenProvider(private val logger: Logger, list: NonEmptyList<Token>) {
}

fun Token.shouldBeDefined(): Either<WirespecException, Unit> = either {
if (!definitionNames.contains(value)) {
if (value !in definitionNames) {
raise(ParserException.DefinitionNotExistsException(value, coordinates))
}
}
Expand Down
Loading

0 comments on commit cbb77a9

Please sign in to comment.