Skip to content

Commit

Permalink
Fix enum lists
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Nov 9, 2023
1 parent be7de7e commit 2e19d5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class JavaEmitter(
if (!ast.hasEndpoints()) "" else "import community.flock.wirespec.Wirespec;\nimport java.util.concurrent.CompletableFuture;\nimport java.util.function.Function;\n\n"

override fun emit(ast: AST): List<Pair<String, String>> = super.emit(ast)
.map { (name, result) -> name to "$pkg\n\n${import(ast)}$result" }
.map { (name, result) -> name.sanitizeSymbol() to "$pkg\n\n${import(ast)}$result" }

override fun Type.emit() = withLogging(logger) {
"""public record ${name.sanitizeSymbol()}(
Expand Down Expand Up @@ -92,10 +92,10 @@ class JavaEmitter(
}

override fun Enum.emit() = withLogging(logger) {
fun String.sanitize() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
fun String.sanitizeEnum() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
val body = """
|${SPACER}public final String label;
|${SPACER}$name(String label) {
|${SPACER}${name.sanitizeSymbol()}(String label) {
|${SPACER}${SPACER}this.label = label;
|${SPACER}}
""".trimMargin()
Expand All @@ -105,11 +105,11 @@ class JavaEmitter(
|${SPACER}${SPACER}return label;
|${SPACER}}
""".trimMargin()
"public enum ${name.sanitizeSymbol()} {\n${SPACER}${entries.joinToString(",\n${SPACER}") { enum -> "${enum.sanitize()}(\"${enum}\")" }};\n${body}\n${toString}\n}\n"
"public enum ${name.sanitizeSymbol()} {\n${SPACER}${entries.joinToString(",\n${SPACER}") { enum -> "${enum.sanitizeEnum()}(\"${enum}\")" }};\n${body}\n${toString}\n}\n"
}

override fun Refined.emit() = withLogging(logger) {
"""public record $name(String value) {
"""public record ${name.sanitizeSymbol()}(String value) {
|${SPACER}static void validate($name record) {
|${SPACER}${validator.emit()}
|${SPACER}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class KotlinEmitter(

override fun emit(ast: AST): List<Pair<String, String>> =
super.emit(ast).map { (name, result) ->
name to """
name.sanitizeSymbol() to """
|${if (packageName.isBlank()) "" else "package $packageName"}
|${if (ast.hasEndpoints()) import else ""}
|${result}
Expand Down Expand Up @@ -104,8 +104,8 @@ class KotlinEmitter(
}

override fun Enum.emit() = withLogging(logger) {
fun String.sanitize() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
"enum class ${name.sanitizeSymbol()} (val label: String){\n${SPACER}${entries.joinToString(",\n${SPACER}") { "${it.sanitize().sanitizeKeywords()}(\"$it\")" }};\n\n${SPACER}override fun toString(): String {\n${SPACER}${SPACER}return label\n${SPACER}}\n}\n"
fun String.sanitizeEnum() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
"enum class ${name.sanitizeSymbol()} (val label: String){\n${SPACER}${entries.joinToString(",\n${SPACER}") { "${it.sanitizeEnum().sanitizeKeywords()}(\"$it\")" }};\n\n${SPACER}override fun toString(): String {\n${SPACER}${SPACER}return label\n${SPACER}}\n}\n"
}

override fun Refined.emit() = withLogging(logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,8 @@ class OpenApiParser(private val openApi: SwaggerObject) {

else -> when (type) {
null, OpenapiType.OBJECT -> {
val fields = properties
?.flatMap { (key, value) ->
when (value) {
is SchemaObject -> when (value.type) {
OpenapiType.ARRAY -> emptyList()
else -> value.flatten(className(name, key))
}

is ReferenceObject -> emptyList()

}
}
?: emptyList()
val fields = properties.orEmpty()
.flatMap { (key, value) -> value.flatten(className(name, key)) }

val schema = listOf(
Type(name, Type.Shape(toField(name)))
Expand Down Expand Up @@ -317,7 +306,7 @@ class OpenApiParser(private val openApi: SwaggerObject) {
null -> error("items cannot be null when type is array: ${this.ref}")
}

else -> when(refOrSchema){
else -> when (refOrSchema) {
is SchemaObject -> Reference.Custom(className(this.getReference()), false)
is ReferenceObject -> Reference.Custom(className(refOrSchema.getReference()), false)
}
Expand All @@ -328,6 +317,15 @@ class OpenApiParser(private val openApi: SwaggerObject) {


private fun SchemaObject.toReference(name: String): Reference = when {
additionalProperties != null -> when (val additionalProperties = additionalProperties!!) {
is BooleanObject -> Reference.Any(false, true)
is ReferenceObject -> additionalProperties.toReference().toMap()
is SchemaObject -> additionalProperties
.takeIf { it.type != null }
?.run { toReference(name).toMap() }
?: Reference.Any(false, true)
}

enum != null -> Reference.Custom(name, false, additionalProperties != null)
else -> when (val type = type) {
OpenapiType.STRING, OpenapiType.INTEGER, OpenapiType.NUMBER, OpenapiType.BOOLEAN ->
Expand Down Expand Up @@ -381,8 +379,9 @@ class OpenApiParser(private val openApi: SwaggerObject) {
is SchemaObject -> {
Field(
identifier = Field.Identifier(key),
reference = when (value.type) {
OpenapiType.ARRAY -> value.toReference(className(name, key, "Array"))
reference = when {
value.enum != null -> value.toReference(className(name, key))
value.type == OpenapiType.ARRAY -> value.toReference(className(name, key, "Array"))
else -> value.toReference(className(name, key))
},
isNullable = !(this.required?.contains(key) ?: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,8 @@ class OpenApiParser(private val openApi: OpenAPIObject) {

else -> when (type) {
null, OpenapiType.OBJECT -> {
val fields = properties.orEmpty().flatMap { (key, value) ->
when (value) {
is SchemaObject -> value.flatten(className(name, key))
is ReferenceObject -> emptyList()
}
val fields = properties.orEmpty().flatMap {
(key, value) ->value.flatten(className(name, key))
}
val schema = listOf(
Type(name, Type.Shape(this.toField(name)))
Expand All @@ -374,8 +371,8 @@ class OpenApiParser(private val openApi: OpenAPIObject) {
name: String,
): List<Definition> {
return when (this) {
is ReferenceObject -> emptyList()
is SchemaObject -> this.flatten(name)
is ReferenceObject -> emptyList()
}
}

Expand Down Expand Up @@ -470,7 +467,7 @@ class OpenApiParser(private val openApi: OpenAPIObject) {
is SchemaObject -> {
Field(
identifier = Field.Identifier(key),
reference = when{
reference = when {
value.enum != null -> value.toReference(className(name, key))
value.type == OpenapiType.ARRAY -> value.toReference(className(name, key, "Array"))
else -> value.toReference(className(name, key))
Expand Down

0 comments on commit 2e19d5a

Please sign in to comment.