From f43b5d9c97c427d8b8ae41561514660e44828116 Mon Sep 17 00:00:00 2001 From: Jens Keim Date: Wed, 12 Jun 2024 17:02:57 +0200 Subject: [PATCH] feat(Provenance): Add `matches(Provenance)` method In preparation of replacing `VcsInfo` in `Repository` with `KnownProvenance`, we add a method to match `Provenance` against each other. This inherited method, allows any Provenace to by matched against any other. Since the type is veriftied to be equal before matching any attributes, it can even match against `UnknownProvenance`, no need to limit it to `KnownProvenance`. Signed-off-by: Jens Keim --- model/src/main/kotlin/Provenance.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/model/src/main/kotlin/Provenance.kt b/model/src/main/kotlin/Provenance.kt index e71b0652786fb..983a9558f964c 100644 --- a/model/src/main/kotlin/Provenance.kt +++ b/model/src/main/kotlin/Provenance.kt @@ -35,10 +35,16 @@ sealed interface Provenance { * True if this [Provenance] refers to the same source code as [pkg], assuming that it belongs to the package id. */ fun matches(pkg: Package): Boolean + + /** + * True if this [Provenance] matches the [Provenance][other] both in type and attributes. + */ + fun matches(other: Provenance): Boolean } data object UnknownProvenance : Provenance { override fun matches(pkg: Package): Boolean = false + override fun matches(other: Provenance): Boolean = false } sealed interface KnownProvenance : Provenance @@ -53,6 +59,8 @@ data class ArtifactProvenance( val sourceArtifact: RemoteArtifact ) : KnownProvenance { override fun matches(pkg: Package): Boolean = sourceArtifact == pkg.sourceArtifact + override fun matches(other: Provenance): Boolean = + other is ArtifactProvenance && sourceArtifact == other.sourceArtifact } /** @@ -79,6 +87,13 @@ data class RepositoryProvenance( * Return true if this provenance matches the processed VCS information of the [package][pkg]. */ override fun matches(pkg: Package): Boolean = vcsInfo == pkg.vcsProcessed + + /** + * Return true if the [Provenance][other] is a [RepositoryProvenance] and its normalized vcsInfo matches this + * provenance's normalized [VcsInfo][vcsInfo]. + */ + override fun matches(other: Provenance): Boolean = + other is RepositoryProvenance && other.vcsInfo.normalize().matches(vcsInfo.normalize()) } /**