Skip to content

Commit

Permalink
feat(Provenance): Add matches(Provenance) method
Browse files Browse the repository at this point in the history
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 <jens.keim@forvia.com>
  • Loading branch information
pepper-jk committed Jun 26, 2024
1 parent 9d271b5 commit f43b5d9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions model/src/main/kotlin/Provenance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

/**
Expand All @@ -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())
}

/**
Expand Down

0 comments on commit f43b5d9

Please sign in to comment.