Skip to content

Commit

Permalink
refactor(spdx): Move nullOrBlankToSpdxNoassertionOrNone()
Browse files Browse the repository at this point in the history
Because probably being the most natural string representation for an
`SpdxExpression`, it makes sense to move that function to `spdx-utils`.

Note that in order to continue working for `null`, it needs to stay an
extension function and not just override `toString()` in `SpdxExpression`.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Nov 5, 2024
1 parent 8e196ab commit 7f61de7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 1 addition & 10 deletions plugins/reporters/spdx/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import org.ossreviewtoolkit.utils.spdx.model.SpdxExtractedLicenseInfo
import org.ossreviewtoolkit.utils.spdx.model.SpdxFile
import org.ossreviewtoolkit.utils.spdx.model.SpdxPackage
import org.ossreviewtoolkit.utils.spdx.model.SpdxPackageVerificationCode
import org.ossreviewtoolkit.utils.spdx.nullOrBlankToSpdxNoassertionOrNone
import org.ossreviewtoolkit.utils.spdx.toSpdx
import org.ossreviewtoolkit.utils.spdx.toSpdxId

Expand Down Expand Up @@ -251,16 +252,6 @@ internal fun SpdxDocument.addExtractedLicenseInfo(licenseTextProvider: LicenseTe
return copy(hasExtractedLicensingInfos = extractedLicenseInfo)
}

/**
* Convert an [SpdxExpression] to `NOASSERTION` if null, to `NONE` if blank, or to its string representation otherwise.
*/
private fun SpdxExpression?.nullOrBlankToSpdxNoassertionOrNone(): String =
when {
this == null -> SpdxConstants.NOASSERTION
toString().isBlank() -> SpdxConstants.NONE
else -> toString()
}

/**
* Convert a null or blank [String] to `NONE`.
*/
Expand Down
10 changes: 10 additions & 0 deletions utils/spdx/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ import org.ossreviewtoolkit.utils.spdx.SpdxExpression.Strictness

private val logger = loggerOf(MethodHandles.lookup().lookupClass())

/**
* Convert an [SpdxExpression] to `NOASSERTION` if null, to `NONE` if blank, or to its string representation otherwise.
*/
fun SpdxExpression?.nullOrBlankToSpdxNoassertionOrNone(): String =
when {
this == null -> SpdxConstants.NOASSERTION
toString().isBlank() -> SpdxConstants.NONE
else -> toString()
}

/**
* Create an [SpdxLicenseIdExpression] from this [SpdxLicense].
*/
Expand Down

0 comments on commit 7f61de7

Please sign in to comment.