Skip to content

Commit

Permalink
Add number as primitive type (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Nov 12, 2023
1 parent 2e19d5a commit c1184a9
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class JavaEmitter(
is Reference.Primitive -> when (type) {
Reference.Primitive.Type.String -> "String"
Reference.Primitive.Type.Integer -> "Integer"
Reference.Primitive.Type.Number -> "Double"
Reference.Primitive.Type.Boolean -> "Boolean"
}
}.sanitizeSymbol()
Expand All @@ -89,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 @@ -179,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 @@ -248,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
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class KotlinEmitter(
is Reference.Primitive -> when (type) {
Reference.Primitive.Type.String -> "String"
Reference.Primitive.Type.Integer -> "Int"
Reference.Primitive.Type.Number -> "Double"
Reference.Primitive.Type.Boolean -> "Boolean"
}
}.sanitizeSymbol()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ScalaEmitter(
is Reference.Primitive -> when (type) {
Reference.Primitive.Type.String -> "String"
Reference.Primitive.Type.Integer -> "Int"
Reference.Primitive.Type.Number -> "Double"
Reference.Primitive.Type.Boolean -> "Boolean"
}
}.let { if (isIterable) "List[$it]" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class TypeScriptEmitter(logger: Logger = noLogger) : Emitter(logger) {
is Reference.Primitive -> when (type) {
Reference.Primitive.Type.String -> "string"
Reference.Primitive.Type.Integer -> "number"
Reference.Primitive.Type.Number -> "number"
Reference.Primitive.Type.Boolean -> "boolean"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class WirespecEmitter(logger: Logger = noLogger) : Emitter(logger) {
is Reference.Primitive -> when (type) {
Reference.Primitive.Type.String -> "String"
Reference.Primitive.Type.Integer -> "Integer"
Reference.Primitive.Type.Number -> "Number"
Reference.Primitive.Type.Boolean -> "Boolean"
}
}.let { if (isIterable) "$it[]" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import community.flock.wirespec.compiler.core.tokenize.types.TRACE
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.utils.Logger

Expand Down Expand Up @@ -131,6 +132,7 @@ class EndpointParser(logger: Logger) : AbstractParser(logger) {
is WsString -> Primitive(Primitive.Type.String, isIterable)

is WsInteger -> Primitive(Primitive.Type.Integer, isIterable)
is WsNumber -> Primitive(Primitive.Type.Number, isIterable)

is WsBoolean -> Primitive(Primitive.Type.Boolean, isIterable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.utils.Logger

Expand Down Expand Up @@ -96,6 +97,11 @@ class TypeParser(logger: Logger) : AbstractParser(logger) {
isIterable
)

is WsNumber -> Type.Shape.Field.Reference.Primitive(
Type.Shape.Field.Reference.Primitive.Type.Number,
isIterable
)

is WsBoolean -> Type.Shape.Field.Reference.Primitive(
Type.Shape.Field.Reference.Primitive.Type.Boolean,
isIterable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class Type(val name: String, val shape: Shape) : Definition {
override val isIterable: Boolean,
override val isMap: Boolean = false
) : Reference {
enum class Type { String, Integer, Boolean }
enum class Type { String, Integer, Number, Boolean }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data object WsEndpointDef : WirespecDefinition
sealed interface WirespecType : Keyword
data object WsString : WirespecType
data object WsInteger : WirespecType
data object WsNumber : WirespecType
data object WsBoolean : WirespecType
data object CustomType : WirespecType

Expand Down
2 changes: 2 additions & 0 deletions src/lsp/intellij-plugin/src/main/kotlin/Lexer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsEndpointDef
import community.flock.wirespec.compiler.core.tokenize.types.WsEnumTypeDef
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsRefinedTypeDef
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.core.tokenize.types.WsTypeDef
Expand Down Expand Up @@ -59,6 +60,7 @@ class Lexer : IntellijLexer() {
is CustomType -> Types.CUSTOM_TYPE
is WsBoolean -> Types.BOOLEAN
is WsInteger -> Types.INTEGER
is WsNumber -> Types.NUMBER
is WsString -> Types.STRING
is LeftCurly -> Types.LEFT_CURLY
is QuestionMark -> Types.QUESTION_MARK
Expand Down
1 change: 1 addition & 0 deletions src/lsp/intellij-plugin/src/main/kotlin/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Types {
val CUSTOM_TYPE = ElementType("CUSTOM_TYPE")
val BOOLEAN = ElementType("BOOLEAN")
val INTEGER = ElementType("INTEGER")
val NUMBER = ElementType("NUMBER")
val STRING = ElementType("STRING")
val TYPE_DEF = ElementType("TYPE_DEF")
val ENUM_DEF = ElementType("ENUM_DEF")
Expand Down
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 Expand Up @@ -369,7 +369,7 @@ class OpenApiParser(private val openApi: SwaggerObject) {
private fun OpenapiType.toPrimitive() = when (this) {
OpenapiType.STRING -> Reference.Primitive.Type.String
OpenapiType.INTEGER -> Reference.Primitive.Type.Integer
OpenapiType.NUMBER -> Reference.Primitive.Type.Integer
OpenapiType.NUMBER -> Reference.Primitive.Type.Number
OpenapiType.BOOLEAN -> Reference.Primitive.Type.Boolean
else -> error("Type is not a primitive")
}
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 Expand Up @@ -457,7 +457,7 @@ class OpenApiParser(private val openApi: OpenAPIObject) {
private fun OpenapiType.toPrimitive() = when (this) {
OpenapiType.STRING -> Primitive.Type.String
OpenapiType.INTEGER -> Primitive.Type.Integer
OpenapiType.NUMBER -> Primitive.Type.Integer
OpenapiType.NUMBER -> Primitive.Type.Number
OpenapiType.BOOLEAN -> Primitive.Type.Boolean
else -> error("Type is not a primitive")
}
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 c1184a9

Please sign in to comment.