Skip to content

Commit 552147d

Browse files
committedFeb 17, 2025·
chore(scanner): Simplify Semver parsing with coerce()
The `coerce()` function provides similar lenient parsing, see [1]. [1]: https://github.com/semver4j/semver4j/blob/01f63c7efce49fa03258065bb7d3f737a7bc0819/src/test/java/org/semver4j/SemverTest.java#L751-L802 Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent 4a6ef3d commit 552147d

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed
 

‎scanner/src/main/kotlin/ScannerMatcher.kt

+3-17
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ data class ScannerMatcher(
6868
* [details]. These defaults can be overridden by the provided [config].
6969
*/
7070
fun create(details: ScannerDetails, config: ScannerMatcherConfig = ScannerMatcherConfig.EMPTY): ScannerMatcher {
71-
val scannerVersion = Semver(normalizeVersion(details.version))
72-
val minVersion = parseVersion(config.minVersion) ?: scannerVersion
73-
val maxVersion = parseVersion(config.maxVersion) ?: minVersion.nextMinor()
71+
val scannerVersion = checkNotNull(Semver.coerce(details.version))
72+
val minVersion = Semver.coerce(config.minVersion) ?: scannerVersion
73+
val maxVersion = Semver.coerce(config.maxVersion) ?: minVersion.nextMinor()
7474
val name = config.regScannerName ?: details.name
7575
val configuration = config.configuration ?: details.configuration
7676

@@ -158,17 +158,3 @@ data class ScannerMatcherConfig(
158158
}
159159
}
160160
}
161-
162-
/**
163-
* Parse the given [versionStr] to a [Semver] while trying to be failure tolerant.
164-
*/
165-
private fun parseVersion(versionStr: String?): Semver? = versionStr?.let { Semver(normalizeVersion(it)) }
166-
167-
/**
168-
* Normalize the given [versionStr] to make sure that it can be parsed to a [Semver]. The [Semver] class
169-
* requires that all components of a semantic version number are present. This function enables a more lenient
170-
* style when declaring a version. So for instance, the user can just write "2", and this gets expanded to
171-
* "2.0.0".
172-
*/
173-
private fun normalizeVersion(versionStr: String): String =
174-
versionStr.takeIf { v -> v.count { it == '.' } >= 2 } ?: normalizeVersion("$versionStr.0")

0 commit comments

Comments
 (0)
Please sign in to comment.