Skip to content

Commit

Permalink
Add reference Unit to wirespec Core (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Nov 28, 2023
1 parent bebd493 commit 2389670
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit

interface LanguageSpec {
val orderedMatchers: List<Pair<Regex, TokenType>>
Expand All @@ -52,6 +53,7 @@ object Wirespec : LanguageSpec {
Regex("^Integer") to WsInteger,
Regex("^Number") to WsNumber,
Regex("^Boolean") to WsBoolean,
Regex("^Unit") to WsUnit,
Regex("^->") to Arrow,
Regex("^GET|^POST|^PUT|^DELETE|^OPTIONS|^HEAD|^PATCH|^TRACE") to Method,
Regex("^/.*/g") to CustomRegex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class JavaEmitter(

private fun Reference.emitSymbol() = withLogging(logger) {
when (this) {
is Reference.Unit -> "Void"
is Reference.Any -> "Object"
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class KotlinEmitter(

private fun Reference.emitSymbol() = withLogging(logger) {
when (this) {
is Reference.Unit -> "Unit"
is Reference.Any -> "Any"
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import community.flock.wirespec.compiler.utils.Logger
import community.flock.wirespec.compiler.utils.noLogger
import community.flock.kotlinx.openapi.bindings.v2.Type as OpenApiType

class OpenApiV2Emitter(override val logger: Logger = noLogger, override val split: Boolean = false) : Emitter(logger, split) {
class OpenApiV2Emitter(override val logger: Logger = noLogger, override val split: Boolean = false) :
Emitter(logger, split) {

override fun emit(ast: AST): List<Pair<String, String>> {
val obj = SwaggerObject(
Expand Down Expand Up @@ -100,15 +101,17 @@ class OpenApiV2Emitter(override val logger: Logger = noLogger, override val spli
.associate {
StatusCode(it.status) to ResponseObject(
description = "${this.name} ${it.status} response",
schema = it.content?.reference?.let { reference ->
when (reference.isIterable) {
false -> reference.emit()
true -> SchemaObject(
type = OpenApiType.ARRAY,
items = reference.emit()
)
schema = it.content
?.takeIf { content -> content.reference !is Field.Reference.Unit }
?.let { content ->
when (content.reference.isIterable) {
false -> content.reference.emit()
true -> SchemaObject(
type = OpenApiType.ARRAY,
items = content.reference.emit()
)
}
}
}
)
}
)
Expand All @@ -129,6 +132,7 @@ class OpenApiV2Emitter(override val logger: Logger = noLogger, override val spli
)

is Field.Reference.Any -> error("Cannot map Any")
is Field.Reference.Unit -> error("Cannot map Unit")
}


Expand All @@ -143,6 +147,7 @@ class OpenApiV2Emitter(override val logger: Logger = noLogger, override val spli
is Field.Reference.Primitive -> type.emitType()
is Field.Reference.Custom -> OpenApiType.OBJECT
is Field.Reference.Any -> OpenApiType.OBJECT
is Field.Reference.Unit -> OpenApiType.OBJECT
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ScalaEmitter(

override fun Reference.emit() = withLogging(logger) {
when (this) {
is Reference.Unit -> "Unit"
is Reference.Any -> "Any"
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class TypeScriptEmitter(logger: Logger = noLogger) : AbstractEmitter(logger) {

override fun Type.Shape.Field.Reference.emit() = withLogging(logger) {
when (this) {
is Reference.Unit -> "void"
is Reference.Any -> "any"
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.Reference
import community.flock.wirespec.compiler.utils.Logger
import community.flock.wirespec.compiler.utils.noLogger

Expand All @@ -32,13 +31,14 @@ class WirespecEmitter(logger: Logger = noLogger) : AbstractEmitter(logger) {

override fun Type.Shape.Field.Reference.emit() = withLogging(logger) {
when (this) {
is Reference.Any -> "Any"
is Reference.Custom -> value
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"
is Type.Shape.Field.Reference.Unit -> "Unit"
is Type.Shape.Field.Reference.Any -> "Any"
is Type.Shape.Field.Reference.Custom -> value
is Type.Shape.Field.Reference.Primitive -> when (type) {
Type.Shape.Field.Reference.Primitive.Type.String -> "String"
Type.Shape.Field.Reference.Primitive.Type.Integer -> "Integer"
Type.Shape.Field.Reference.Primitive.Type.Number -> "Number"
Type.Shape.Field.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 @@ -6,8 +6,7 @@ import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.exceptions.WirespecException.CompilerException.ParserException.WrongTokenException
import community.flock.wirespec.compiler.core.parse.nodes.Endpoint
import community.flock.wirespec.compiler.core.parse.nodes.Type
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field.Reference.Custom
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field.Reference.Primitive
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field.Reference
import community.flock.wirespec.compiler.core.tokenize.types.Arrow
import community.flock.wirespec.compiler.core.tokenize.types.Brackets
import community.flock.wirespec.compiler.core.tokenize.types.Colon
Expand All @@ -26,6 +25,7 @@ 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.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.utils.Logger

class EndpointParser(logger: Logger) : AbstractParser(logger) {
Expand Down Expand Up @@ -201,14 +201,16 @@ class EndpointParser(logger: Logger) : AbstractParser(logger) {
token.log()
val isIterable = (token.type is Brackets).also { if (it) eatToken().bind() }
when (wsType) {
is WsString -> Primitive(Primitive.Type.String, isIterable)
is WsString -> Reference.Primitive(Reference.Primitive.Type.String, isIterable)

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

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

is CustomType -> Custom(value, isIterable)
is WsUnit -> Reference.Unit(isIterable)

is CustomType -> Reference.Custom(value, isIterable)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.utils.Logger

class TypeParser(logger: Logger) : AbstractParser(logger) {
Expand Down Expand Up @@ -107,6 +108,8 @@ class TypeParser(logger: Logger) : AbstractParser(logger) {
isIterable
)

is WsUnit -> Type.Shape.Field.Reference.Unit(isIterable)

is CustomType -> Type.Shape.Field.Reference.Custom(value, isIterable)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class Type(val name: String, val shape: Shape) : Definition {
val isMap: Boolean

data class Any(override val isIterable: Boolean, override val isMap: Boolean = false) : Reference
data class Unit(override val isIterable: Boolean, override val isMap: Boolean = false) : Reference
data class Custom(
val value: String,
override val isIterable: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data object WsInteger : WirespecType
data object WsNumber : WirespecType
data object WsBoolean : WirespecType
data object CustomType : WirespecType
data object WsUnit : WirespecType

data object Method : Keyword

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/lib/src/jsMain/kotlin/Ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import community.flock.wirespec.compiler.core.parse.nodes.Enum
import community.flock.wirespec.compiler.core.parse.nodes.Node
import community.flock.wirespec.compiler.core.parse.nodes.Refined
import community.flock.wirespec.compiler.core.parse.nodes.Type
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit

@ExperimentalJsExport
internal fun List<Node>.produce(): Array<WsNode> = map {
Expand Down Expand Up @@ -46,6 +47,7 @@ private fun Type.Shape.Field.Identifier.produce() = WsIdentifier(this.value)
@ExperimentalJsExport
private fun Type.Shape.Field.Reference.produce() = when(this){
is Type.Shape.Field.Reference.Any -> WsAny(isIterable, isMap)
is Type.Shape.Field.Reference.Unit -> WsUnit(isIterable, isMap)
is Type.Shape.Field.Reference.Custom -> WsCustom(value, isIterable, isMap)
is Type.Shape.Field.Reference.Primitive -> WsPrimitive(type.produce(), isIterable, isMap)
}
Expand Down Expand Up @@ -166,6 +168,9 @@ sealed interface WsReference {
data class WsAny(override val isIterable: Boolean, override val isMap: Boolean = false) : WsReference
@JsExport
@ExperimentalJsExport
data class WsUnit(override val isIterable: Boolean, override val isMap: Boolean = false) : WsReference
@JsExport
@ExperimentalJsExport
data class WsCustom(
val value: String,
override val isIterable: Boolean,
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 @@ -30,6 +30,7 @@ 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
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit
import com.intellij.lexer.LexerBase as IntellijLexer

class Lexer : IntellijLexer() {
Expand Down Expand Up @@ -67,6 +68,7 @@ class Lexer : IntellijLexer() {
is WsInteger -> Types.INTEGER
is WsNumber -> Types.NUMBER
is WsString -> Types.STRING
is WsUnit -> Types.UNIT
is LeftCurly -> Types.LEFT_CURLY
is QuestionMark -> Types.QUESTION_MARK
is Hash -> Types.HASH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SyntaxHighlighter : SyntaxHighlighterBase() {
Types.INTEGER -> arrayOf(KEYWORD)
Types.NUMBER -> arrayOf(KEYWORD)
Types.STRING -> arrayOf(KEYWORD)
Types.UNIT -> arrayOf(KEYWORD)
Types.TYPE_DEF -> arrayOf(KEYWORD)
Types.ENDPOINT_DEF -> arrayOf(KEYWORD)
Types.ENUM_DEF -> arrayOf(KEYWORD)
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 @@ -14,6 +14,7 @@ interface Types {
val INTEGER = ElementType("INTEGER")
val NUMBER = ElementType("NUMBER")
val STRING = ElementType("STRING")
val UNIT = ElementType("UNIT")
val TYPE_DEF = ElementType("TYPE_DEF")
val ENUM_DEF = ElementType("ENUM_DEF")
val REFINED_TYPE_DEF = ElementType("REFINED_TYPE_DEF")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,12 @@ private fun Reference.toIterable() = when (this) {
is Reference.Custom -> this.copy(isIterable = true)
is Reference.Any -> this.copy(isIterable = true)
is Reference.Primitive -> this.copy(isIterable = true)
is Reference.Unit -> this.copy(isIterable = true)
}

private fun Reference.toMap() = when (this) {
is Reference.Custom -> this.copy(isMap = true)
is Reference.Any -> this.copy(isMap = true)
is Reference.Primitive -> this.copy(isMap = true)
is Reference.Unit -> this.copy(isMap = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import community.flock.wirespec.compiler.core.parse.nodes.Enum
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
import community.flock.wirespec.compiler.core.parse.nodes.Type.Shape.Field.Reference.Primitive
import community.flock.wirespec.openapi.Common.className
import community.flock.kotlinx.openapi.bindings.v3.Type as OpenapiType

Expand Down Expand Up @@ -435,7 +434,7 @@ class OpenApiParser(private val openApi: OpenAPIObject) {

enum != null -> Reference.Custom(name, false, additionalProperties != null)
else -> when (val type = type) {
OpenapiType.STRING, OpenapiType.NUMBER, OpenapiType.INTEGER, OpenapiType.BOOLEAN -> Primitive(
OpenapiType.STRING, OpenapiType.NUMBER, OpenapiType.INTEGER, OpenapiType.BOOLEAN -> Reference.Primitive(
type.toPrimitive(),
false,
additionalProperties != null
Expand Down Expand Up @@ -477,10 +476,10 @@ class OpenApiParser(private val openApi: OpenAPIObject) {
?: error("Wrong reference: ${this.ref.value}")

private fun OpenapiType.toPrimitive() = when (this) {
OpenapiType.STRING -> Primitive.Type.String
OpenapiType.INTEGER -> Primitive.Type.Integer
OpenapiType.NUMBER -> Primitive.Type.Number
OpenapiType.BOOLEAN -> Primitive.Type.Boolean
OpenapiType.STRING -> Reference.Primitive.Type.String
OpenapiType.INTEGER -> 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 Expand Up @@ -583,13 +582,15 @@ private fun OpenapiType?.isPrimitive() = when (this) {
}

private fun Reference.toIterable() = when (this) {
is Reference.Custom -> this.copy(isIterable = true)
is Reference.Any -> this.copy(isIterable = true)
is Reference.Primitive -> this.copy(isIterable = true)
is Reference.Custom -> copy(isIterable = true)
is Reference.Any -> copy(isIterable = true)
is Reference.Primitive -> copy(isIterable = true)
is Reference.Unit -> copy(isIterable = true)
}

private fun Reference.toMap() = when (this) {
is Reference.Custom -> this.copy(isMap = true)
is Reference.Any -> this.copy(isMap = true)
is Reference.Primitive -> this.copy(isMap = true)
is Reference.Custom -> copy(isMap = true)
is Reference.Any -> copy(isMap = true)
is Reference.Primitive -> copy(isMap = true)
is Reference.Unit -> copy(isMap = true)
}

0 comments on commit 2389670

Please sign in to comment.