-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24fbc30
commit 77d1baa
Showing
2 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...lin-plugin/src/main/kotlin/app/cash/zipline/api/compatibility/ApiCompatibilityDecision.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (C) 2023 Cash App | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.zipline.api.compatibility | ||
|
||
import app.cash.zipline.api.fir.FirZiplineApi | ||
import app.cash.zipline.api.toml.TomlZiplineApi | ||
import app.cash.zipline.api.toml.TomlZiplineFunction | ||
import app.cash.zipline.api.toml.TomlZiplineService | ||
|
||
sealed interface ApiCompatibilityDecision | ||
|
||
class ActualApiHasProblems( | ||
val messages: List<String>, | ||
) : ApiCompatibilityDecision | ||
|
||
class ExpectedApiRequiresUpdates( | ||
val updatedApi: TomlZiplineApi, | ||
) : ApiCompatibilityDecision | ||
|
||
object ExpectedApiIsUpToDate : ApiCompatibilityDecision | ||
|
||
/** Compare the expected and actual API and decide what to do about it. */ | ||
fun makeApiCompatibilityDecision( | ||
expectedApi: TomlZiplineApi, | ||
actualApi: FirZiplineApi, | ||
): ApiCompatibilityDecision { | ||
val problemMessages = mutableListOf<String>() | ||
var hasChanges = false | ||
|
||
val actualServices = actualApi.services.associateBy { it.name } | ||
if (actualServices.size != expectedApi.services.size) hasChanges = true | ||
|
||
for (expectedService in expectedApi.services) { | ||
val serviceName = expectedService.name | ||
val actualService = actualServices[serviceName] | ||
|
||
if (actualService == null) { | ||
problemMessages += | ||
""" | ||
|Expected service not found: | ||
| $serviceName | ||
""".trimMargin() | ||
continue | ||
} | ||
|
||
val actualFunctions = actualService.functions.associateBy { it.id } | ||
if (actualFunctions.size != expectedService.functions.size) hasChanges = true | ||
|
||
for (expectedFunction in expectedService.functions) { | ||
val functionId = expectedFunction.id | ||
val actualFunction = actualFunctions[functionId] | ||
if (actualFunction == null) { | ||
val comment = expectedFunction.leadingComment | ||
problemMessages += buildString { | ||
append("Expected function $functionId of $serviceName not found") | ||
if (comment.isNotEmpty()) { | ||
append(":\n ") | ||
append(comment.replace("\n", "\n ")) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return when { | ||
problemMessages.isNotEmpty() -> ActualApiHasProblems(problemMessages) | ||
hasChanges -> ExpectedApiRequiresUpdates(actualApi.toToml()) | ||
else -> ExpectedApiIsUpToDate | ||
} | ||
} | ||
|
||
private fun FirZiplineApi.toToml(): TomlZiplineApi { | ||
return TomlZiplineApi( | ||
services.map { service -> | ||
TomlZiplineService( | ||
name = service.name, | ||
functions = service.functions.map { function -> | ||
TomlZiplineFunction( | ||
leadingComment = function.signature, | ||
id = function.id, | ||
) | ||
}, | ||
) | ||
}, | ||
) | ||
} |
Oops, something went wrong.