Skip to content

Commit

Permalink
Build parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Dec 5, 2024
1 parent 8875fb8 commit c4535f7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ open class JavaEmitter(

private fun Reference.Primitive.emit() = when (type) {
is Reference.Primitive.Type.String -> "String"
is Reference.Primitive.Type.Integer -> "Long"
is Reference.Primitive.Type.Number -> "Double"
is Reference.Primitive.Type.Integer -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "int"
Reference.Primitive.Type.Precision._64 -> "Long"
}
is Reference.Primitive.Type.Number -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "Float"
Reference.Primitive.Type.Precision._64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ open class KotlinEmitter(
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
is Reference.Primitive.Type.String -> "String"
is Reference.Primitive.Type.Integer -> "Long"
is Reference.Primitive.Type.Number -> "Double"
is Reference.Primitive.Type.Integer -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "Int"
Reference.Primitive.Type.Precision._64 -> "Long"
}
is Reference.Primitive.Type.Number -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "Float"
Reference.Primitive.Type.Precision._64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ open class ScalaEmitter(
is Reference.Custom -> value
is Reference.Primitive -> when (type) {
is Reference.Primitive.Type.String -> "String"
is Reference.Primitive.Type.Integer -> "Long"
is Reference.Primitive.Type.Number -> "Double"
is Reference.Primitive.Type.Integer -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "Int"
Reference.Primitive.Type.Precision._64 -> "Long"
}
is Reference.Primitive.Type.Number -> when(type.precision){
Reference.Primitive.Type.Precision._32 -> "Float"
Reference.Primitive.Type.Precision._64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ class TypeParser(logger: Logger) : AbstractParser(logger) {
)

is WsInteger -> Reference.Primitive(
type = Reference.Primitive.Type.Integer(),
type = Reference.Primitive.Type.Integer(when(value) {
"Integer32" -> Reference.Primitive.Type.Precision._32
else -> Reference.Primitive.Type.Precision._64
}),
isIterable = isIterable,
isDictionary = isDict
)

is WsNumber -> Reference.Primitive(
type = Reference.Primitive.Type.Number(),
type = Reference.Primitive.Type.Number(when(value) {
"Number32" -> Reference.Primitive.Type.Precision._32
else -> Reference.Primitive.Type.Precision._64
}),
isIterable = isIterable,
isDictionary = isDict
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,74 @@ class ParseTypeTest {
second shouldBe Reference.Custom(value = "Bal", isIterable = false, isDictionary = false)
}
}

@Test
fun testIntegerNumberParser() {
val source = """
|type Bar { int32: Integer32, int64: Integer[] }
|type Foo { num32: Number32, num64: Number64? }
""".trimMargin()

WirespecSpec.tokenize(source)
.let(parser()::parse)
.shouldBeRight()
.also { it.size shouldBe 2 }
.let {
val (first, second) = it.toList()
first shouldBe Type(
comment = null,
identifier = DefinitionIdentifier("Bar"),
extends = emptyList(),
shape = Type.Shape(
value = listOf(
Field(
identifier = FieldIdentifier("int32"),
isNullable = false,
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision._32),
isIterable = false,
isDictionary = false
)
),
Field(
identifier = FieldIdentifier("int64"),
isNullable = false,
reference = Reference.Primitive(
type = Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision._64),
isIterable = true,
isDictionary = false
)
)
)
)
)
second shouldBe Type(
comment = null,
identifier = DefinitionIdentifier("Foo"),
extends = emptyList(),
shape = Type.Shape(
value = listOf(
Field(
identifier = FieldIdentifier("num32"),
isNullable = false,
reference = Reference.Primitive(
type = Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision._32),
isIterable = false,
isDictionary = false
)
),
Field(
identifier = FieldIdentifier("num64"),
isNullable = true,
reference = Reference.Primitive(
type = Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision._64),
isIterable = false,
isDictionary = false
)
)
)
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,9 @@ object GetInventoryEndpoint : Wirespec.Endpoint {

sealed interface Response2XX<T: Any> : Response<T>

sealed interface ResponseMapStringLong : Response<Map<String, Long>>
sealed interface ResponseMapStringInt : Response<Map<String, Int>>

data class Response200(override val body: Map<String, Long>) : Response2XX<Map<String, Long>>, ResponseMapStringLong {
data class Response200(override val body: Map<String, Int>) : Response2XX<Map<String, Int>>, ResponseMapStringInt {
override val status = 200
override val headers = Headers
data object Headers : Wirespec.Response.Headers
Expand All @@ -825,14 +825,14 @@ object GetInventoryEndpoint : Wirespec.Endpoint {
is Response200 -> Wirespec.RawResponse(
statusCode = response.status,
headers = mapOf(),
body = serialization.serialize(response.body, typeOf<Map<String, Long>>()),
body = serialization.serialize(response.body, typeOf<Map<String, Int>>()),
)
}

fun fromResponse(serialization: Wirespec.Deserializer<String>, response: Wirespec.RawResponse): Response<*> =
when (response.statusCode) {
200 -> Response200(
body = serialization.deserialize(requireNotNull(response.body) { "body is null" }, typeOf<Map<String, Long>>()),
body = serialization.deserialize(requireNotNull(response.body) { "body is null" }, typeOf<Map<String, Int>>()),
)
else -> error("Cannot match response with status: ${response.statusCode}")
}
Expand Down Expand Up @@ -1792,7 +1792,7 @@ enum class FindPetsByStatusParameterStatus (override val label: String): Wirespe
data class Order(
val id: Long?,
val petId: Long?,
val quantity: Long?,
val quantity: Int?,
val shipDate: String?,
val status: OrderStatus?,
val complete: Boolean?
Expand Down Expand Up @@ -1833,7 +1833,7 @@ data class User(
val email: String?,
val password: String?,
val phone: String?,
val userStatus: Long?
val userStatus: Int?
)

data class Tag(
Expand All @@ -1860,7 +1860,7 @@ enum class PetStatus (override val label: String): Wirespec.Enum {
}

data class ApiResponse(
val code: Long?,
val code: Int?,
val type: String?,
val message: String?
)

0 comments on commit c4535f7

Please sign in to comment.