Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Nov 12, 2023
1 parent 9160f98 commit 6bf9fac
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,17 @@ class JavaEmitter(
override fun Reference.emit() = withLogging(logger) {
emitSymbol()
.let { if (isIterable) "java.util.List<$it>" else it }
.let { if (isMap) "java.util.Map<String, $it>" else it }
}

override fun Enum.emit() = withLogging(logger) {
fun String.sanitizeEnum() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
fun String.sanitizeEnum() = this
.replace("/", "_")
.replace(" ", "_")
.replace("-", "_")
.replace("", "_")
.let { if (it.first().isDigit()) "_$it" else it }

val body = """
|${SPACER}public final String label;
|${SPACER}${name.sanitizeSymbol()}(String label) {
Expand Down Expand Up @@ -180,7 +187,7 @@ class JavaEmitter(
""".trimMargin()

private fun List<Endpoint.Response>.emitResponseMapper() = """
|${SPACER}static <B, Res extends Wirespec.Response<?>> Function<Wirespec.Response<B>, Res> RESPONSE_MAPPER(Wirespec.ContentMapper<B> contentMapper) {
|${SPACER}static <B, Res extends Response<?>> Function<Wirespec.Response<B>, Res> RESPONSE_MAPPER(Wirespec.ContentMapper<B> contentMapper) {
|return response -> {
|${distinctBy { it.status to it.content?.type }.joinToString("") { it.emitResponseMapperCondition() }}
|${SPACER}${SPACER}throw new IllegalStateException("Unknown response type");
Expand Down Expand Up @@ -249,7 +256,7 @@ class JavaEmitter(

fun String.sanitizeKeywords() = if (reservedKeywords.contains(this)) "_$this" else this

fun String.sanitizeSymbol() = replace(".", "")
fun String.sanitizeSymbol() = replace(".", "").replace(" ", "_")
companion object {
private val reservedKeywords = listOf(
"abstract", "continue", "for", "new", "switch",
Expand Down
9 changes: 7 additions & 2 deletions src/lsp/node/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/openapi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ kotlin {
dependencies {
implementation(project(":src:compiler:core"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("community.flock.kotlinx.openapi.bindings:kotlin-openapi-bindings:0.0.17")
implementation("community.flock.kotlinx.openapi.bindings:kotlin-openapi-bindings:0.0.19")
}
}
commonTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import community.flock.kotlinx.openapi.bindings.v2.SwaggerObject
import community.flock.wirespec.compiler.core.parse.nodes.Definition
import community.flock.wirespec.compiler.core.parse.nodes.Endpoint
import community.flock.wirespec.compiler.core.parse.nodes.Enum
import community.flock.wirespec.compiler.core.parse.nodes.Refined
import community.flock.wirespec.compiler.core.parse.nodes.Type
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field.Reference
Expand Down Expand Up @@ -231,10 +232,14 @@ class OpenApiParser(private val openApi: SwaggerObject) {
): List<Definition> = when {
additionalProperties != null -> when (additionalProperties) {
is BooleanObject -> emptyList()
else -> additionalProperties!!.resolve().flatten(name)
else -> additionalProperties
?.resolve()
?.takeIf { it.properties != null }
?.flatten(name)
?: emptyList()
}

allOf != null -> listOf(Type(name, Type.Shape(allOf.orEmpty().flatMap { it.resolve().toField(name) })))
allOf != null -> listOf(Type(name, Type.Shape(allOf.orEmpty().flatMap { it.resolve().toField(name) }.distinctBy { it.identifier })))
.plus(allOf!!.flatMap {
when (it) {
is ReferenceObject -> emptyList()
Expand Down Expand Up @@ -264,14 +269,9 @@ class OpenApiParser(private val openApi: SwaggerObject) {
}

OpenapiType.ARRAY -> when (val it = this.items) {
is ReferenceObject ->
emptyList()

is SchemaObject ->
it.flatten(className(name, "Array"))

null ->
emptyList()
is ReferenceObject -> emptyList()
is SchemaObject -> it.flatten(className(name, "Array"))
null -> emptyList()
}

else -> emptyList()
Expand Down Expand Up @@ -321,7 +321,7 @@ class OpenApiParser(private val openApi: SwaggerObject) {
is BooleanObject -> Reference.Any(false, true)
is ReferenceObject -> additionalProperties.toReference().toMap()
is SchemaObject -> additionalProperties
.takeIf { it.type != null }
.takeIf { it.type.isPrimitive() || it.properties != null}
?.run { toReference(name).toMap() }
?: Reference.Any(false, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class OpenApiParser(private val openApi: OpenAPIObject) {

oneOf != null -> TODO("oneOf is not implemented")
anyOf != null -> TODO("anyOf is not implemented")
allOf != null -> listOf(Type(name, Type.Shape(allOf.orEmpty().flatMap { it.resolve().toField(name) })))
allOf != null -> listOf(Type(name, Type.Shape(allOf.orEmpty().flatMap { it.resolve().toField(name) }.distinctBy { it.identifier })))
.plus(allOf!!.flatMap {
when (it) {
is ReferenceObject -> emptyList()
Expand Down Expand Up @@ -406,7 +406,7 @@ class OpenApiParser(private val openApi: OpenAPIObject) {
is BooleanObject -> Reference.Any(false, true)
is ReferenceObject -> additionalProperties.toReference().toMap()
is SchemaObject -> additionalProperties
.takeIf { it.type != null }
.takeIf { it.type.isPrimitive() || it.properties != null}
?.run { toReference(name).toMap() }
?: Reference.Any(false, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ object Expected {
value = listOf(
Type.Shape.Field(
identifier = Identifier(value = "a"),
reference = Reference.Primitive(type = Reference.Primitive.Type.Integer, isIterable = false),
reference = Reference.Primitive(type = Reference.Primitive.Type.Number, isIterable = false),
isNullable = true
),
Type.Shape.Field(
identifier = Identifier(value = "b"),
reference = Reference.Primitive(type = Reference.Primitive.Type.Integer, isIterable = false),
reference = Reference.Primitive(type = Reference.Primitive.Type.Number, isIterable = false),
isNullable = true
)
)
Expand Down Expand Up @@ -128,15 +128,15 @@ object Expected {
Type.Shape.Field(
identifier = Identifier(value = "a"),
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer,
type = Reference.Primitive.Type.Number,
isIterable = false
),
isNullable = true
),
Type.Shape.Field(
identifier = Identifier(value = "b"),
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer,
type = Reference.Primitive.Type.Number,
isIterable = false
),
isNullable = true
Expand Down Expand Up @@ -294,7 +294,7 @@ object Expected {
Type.Shape.Field(
identifier = Identifier(value = "code"),
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer,
type = Reference.Primitive.Type.Number,
isIterable = false
),
isNullable = true
Expand All @@ -317,7 +317,7 @@ object Expected {
Type.Shape.Field(
identifier = Identifier(value = "code"),
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer,
type = Reference.Primitive.Type.Number,
isIterable = false
),
isNullable = true
Expand Down

0 comments on commit 6bf9fac

Please sign in to comment.