Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Wirespec AST #209

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ include(
"src:converter:openapi",
"src:integration:jackson",
"src:integration:wirespec",
"src:generator",
"src:tools:compare",
"src:tools:generator",
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ data class Type(
}

data class Field(val identifier: Identifier, val reference: Reference, val isNullable: Boolean) {
val isNotNullable = !isNullable

sealed interface Reference : Value<String> {
val isIterable: Boolean
val isDictionary: Boolean
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/npm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ kotlin {
implementation(project(":src:compiler:lib"))
implementation(project(":src:plugin:cli"))
implementation(project(":src:converter:openapi"))
implementation(project(":src:generator"))
implementation(project(":src:tools:generator"))
}
}
val jsMain by getting {
Expand Down
53 changes: 53 additions & 0 deletions src/tools/compare/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Libraries.ARROW_CORE
import Libraries.KOTEST_ASSERTIONS
import Libraries.KOTEST_ASSERTIONS_ARROW

plugins {
kotlin("multiplatform")
kotlin("jvm") apply false
}

group = "${Settings.GROUP_ID}.compare"
version = Settings.version

repositories {
mavenCentral()
maven(uri("https://s01.oss.sonatype.org/service/local/repo_groups/public/content"))
}

kotlin {
macosX64()
macosArm64()
linuxX64()
mingwX64()
js(IR) {
nodejs()
}
jvm {
withJava()
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
}
sourceSets {
commonMain {
dependencies {
implementation(project(":src:compiler:core"))
implementation(ARROW_CORE)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("community.flock.kotlinx.rgxgen:kotlin-rgxgen:0.0.1")
}
}
commonTest {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(kotlin("test-junit"))
implementation(KOTEST_ASSERTIONS)
implementation(KOTEST_ASSERTIONS_ARROW)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package community.flock.wirespec.compare

import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.left
import arrow.core.mapOrAccumulate
import arrow.core.nel
import arrow.core.raise.either
import arrow.core.raise.ensure
import arrow.core.raise.zipOrAccumulate
import arrow.core.right
import community.flock.wirespec.compiler.core.parse.Definition
import community.flock.wirespec.compiler.core.parse.Endpoint
import community.flock.wirespec.compiler.core.parse.Field
import community.flock.wirespec.compiler.core.parse.Type


object Compare {

fun compare(left: List<Definition>, right: List<Definition>): Either<NonEmptyList<Difference>, List<Any>> {
val list = (left to right).pairBy { it.identifier.value }
return list.mapOrAccumulate { it.compareDefinition().bindNel() }
}

private fun Paired<Definition>.compareDefinition(): Either<NonEmptyList<Difference>, Any> =
when (this) {
is Paired.Old -> RemovedDefinitionDifference(key, old).nel().left()
is Paired.New -> AddedDefinitionDifference(key, new).nel().left()
is Paired.Both -> when {
old == new -> true.right()
old is Type && new is Type -> (this as Paired.Both<Type>).compareType()
old is Endpoint && new is Endpoint -> (this as Paired.Both<Endpoint>).compareEndpoint()
else -> TODO()
}
}

private fun Paired.Both<Type>.compareType(): Either<NonEmptyList<Difference>, List<Boolean>> {
val paired = (old.shape.value to new.shape.value).pairBy { it.identifier.value }
return paired.mapOrAccumulate { it.compareField(key).bindNel() }
}

private fun Paired<Field>.compareField(definitionKey: String): Either<NonEmptyList<Difference>, Boolean> =
when (this) {
is Paired.Old -> RemovedFieldDifference("${definitionKey}.${key}", old).nel().left()
is Paired.New -> AddedFieldDifference("${definitionKey}.${key}", new).nel().left()
is Paired.Both ->
either {
zipOrAccumulate(
{
ensure(old.isNullable == new.isNullable) {
ChangedNullableFieldDifference("${definitionKey}.${key}", old, new)
}
},
{
into { it.reference }.compareReference("${definitionKey}.${key}").bindNel()
},
) { _, _ -> true }
}
}

private fun Paired<Field.Reference>.compareReference(definitionKey: String): Either<NonEmptyList<ReferenceDifference>, Boolean> =
when (this) {
is Paired.Old -> RemovedReferenceDifference("${definitionKey}.${key}", old).nel().left()
is Paired.New -> AddedReferenceDifference("${definitionKey}.${key}", new).nel().left()
is Paired.Both -> either {
zipOrAccumulate(
{
ensure(old.isIterable == new.isIterable) {
ChangedIterableReferenceDifference(definitionKey, old, new)
}
},
{
ensure(old.isDictionary == new.isDictionary) {
ChangedMapReferenceDifference(definitionKey, old, new)
}
},
{
ensure(old.value == new.value) {
ChangedValueReferenceDifference(definitionKey, old, new)
}
}
) { _, _, _ -> true }
}
}

private fun Paired<Endpoint.Request>.compareRequest(definitionKey: String): Either<NonEmptyList<Difference>, Boolean> =
when (this) {
is Paired.Old -> RemovedRequestDifference("${definitionKey}.${key}", old).nel().left()
is Paired.New -> AddedRequestDifference("${definitionKey}.${key}", new).nel().left()
is Paired.Both -> when {
old.content == new.content -> true.right()
else -> into { it.content ?: error("") }.compareContent("${definitionKey}.${key}")
}
}

private fun Paired<Endpoint.Content>.compareContent(definitionKey: String): Either<NonEmptyList<Difference>, Boolean> =
when (this) {
is Paired.Old -> RemovedContentDifference("${definitionKey}.${key}", old).nel().left()
is Paired.New -> AddedContentDifference("${definitionKey}.${key}", new).nel().left()
is Paired.Both -> either {
zipOrAccumulate(
{
ensure(old.type == new.type) {
ChangedTypeContentDifference(
"${definitionKey}.${key}",
old,
new
)
}
},
{
into { it.reference }.compareReference("${definitionKey}.${key}").bindNel()
},
{ _, _ -> true }
)
}
}

private fun Paired.Both<Endpoint>.compareEndpoint(): Either<NonEmptyList<Difference>, Any> = either {
zipOrAccumulate(
{ ensure(old.method == new.method) { MethodEndpointDifference(key, old, new) } },
{ ensure(old.path == new.path) { PathEndpointDifference(key, old, new) } },
{
(old.query to new.query).pairBy { it.identifier.value }
.mapOrAccumulate { it.compareField(key).bindNel() }
},
{
(old.headers to new.headers).pairBy { it.identifier.value }
.mapOrAccumulate { it.compareField(key).bindNel() }
},
{
(old.cookies to new.cookies).pairBy { it.identifier.value }
.mapOrAccumulate { it.compareField(key).bindNel() }
},
{
(old.requests to new.requests).pairBy { it.content?.type.orEmpty() }
.mapOrAccumulate { it.compareRequest(key).bindNel() }
},
)
{ _, _, _, _, _, _ -> true }
}

}

Loading
Loading