-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(model)!: Move PURL-related extension functions to a separate…
… file Prepare for adding more PURL functionality. Signed-off-by: Sebastian Schuberth <[email protected]>
- Loading branch information
1 parent
71f82f9
commit 490a641
Showing
5 changed files
with
67 additions
and
46 deletions.
There are no files selected for viewing
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,64 @@ | ||
/* | ||
* Copyright (C) 2020 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.model.utils | ||
|
||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.Package | ||
|
||
/** | ||
* Map a [Package]'s type to the string representation of the respective [PurlType], or fall back to [PurlType.GENERIC] | ||
* if the [Package]'s type has no direct equivalent. | ||
*/ | ||
fun Identifier.getPurlType() = | ||
when (type.lowercase()) { | ||
"bower" -> PurlType.BOWER | ||
"carthage" -> PurlType.CARTHAGE | ||
"composer" -> PurlType.COMPOSER | ||
"conan" -> PurlType.CONAN | ||
"crate" -> PurlType.CARGO | ||
"go" -> PurlType.GOLANG | ||
"gem" -> PurlType.GEM | ||
"hackage" -> PurlType.HACKAGE | ||
"maven" -> PurlType.MAVEN | ||
"npm" -> PurlType.NPM | ||
"nuget" -> PurlType.NUGET | ||
"pod" -> PurlType.COCOAPODS | ||
"pub" -> PurlType.PUB | ||
"pypi" -> PurlType.PYPI | ||
"spm" -> PurlType.SWIFT | ||
else -> PurlType.GENERIC | ||
}.toString() | ||
|
||
/** | ||
* Create the canonical [package URL](https://github.com/package-url/purl-spec) ("purl") based on the properties of | ||
* the [Identifier]. Some issues remain with this specification | ||
* (see e.g. https://github.com/package-url/purl-spec/issues/33). | ||
* Optional [qualifiers] may be given and will be appended to the purl as query parameters e.g. | ||
* pkg:deb/debian/[email protected]?arch=i386&distro=jessie | ||
* Optional [subpath] may be given and will be appended to the purl e.g. | ||
* pkg:golang/google.golang.org/genproto#googleapis/api/annotations | ||
* | ||
* This implementation uses the package type as 'type' purl element as it is used | ||
* [in the documentation](https://github.com/package-url/purl-spec/blob/master/README.rst#purl). | ||
* E.g. 'maven' for Gradle projects. | ||
*/ | ||
@JvmOverloads | ||
fun Identifier.toPurl(qualifiers: Map<String, String> = emptyMap(), subpath: String = "") = | ||
if (this == Identifier.EMPTY) "" else createPurl(getPurlType(), namespace, name, version, qualifiers, subpath) |
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 |
---|---|---|
|
@@ -21,8 +21,6 @@ | |
|
||
package org.ossreviewtoolkit.model.utils | ||
|
||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.Package | ||
import org.ossreviewtoolkit.utils.common.percentEncode | ||
|
||
/** | ||
|
@@ -58,47 +56,6 @@ enum class PurlType(private val value: String) { | |
override fun toString() = value | ||
} | ||
|
||
/** | ||
* Map a [Package]'s type to the string representation of the respective [PurlType], or fall back to [PurlType.GENERIC] | ||
* if the [Package]'s type has no direct equivalent. | ||
*/ | ||
fun Identifier.getPurlType() = | ||
when (type.lowercase()) { | ||
"bower" -> PurlType.BOWER | ||
"carthage" -> PurlType.CARTHAGE | ||
"composer" -> PurlType.COMPOSER | ||
"conan" -> PurlType.CONAN | ||
"crate" -> PurlType.CARGO | ||
"go" -> PurlType.GOLANG | ||
"gem" -> PurlType.GEM | ||
"hackage" -> PurlType.HACKAGE | ||
"maven" -> PurlType.MAVEN | ||
"npm" -> PurlType.NPM | ||
"nuget" -> PurlType.NUGET | ||
"pod" -> PurlType.COCOAPODS | ||
"pub" -> PurlType.PUB | ||
"pypi" -> PurlType.PYPI | ||
"spm" -> PurlType.SWIFT | ||
else -> PurlType.GENERIC | ||
}.toString() | ||
|
||
/** | ||
* Create the canonical [package URL](https://github.com/package-url/purl-spec) ("purl") based on the properties of | ||
* the [Identifier]. Some issues remain with this specification | ||
* (see e.g. https://github.com/package-url/purl-spec/issues/33). | ||
* Optional [qualifiers] may be given and will be appended to the purl as query parameters e.g. | ||
* pkg:deb/debian/[email protected]?arch=i386&distro=jessie | ||
* Optional [subpath] may be given and will be appended to the purl e.g. | ||
* pkg:golang/google.golang.org/genproto#googleapis/api/annotations | ||
* | ||
* This implementation uses the package type as 'type' purl element as it is used | ||
* [in the documentation](https://github.com/package-url/purl-spec/blob/master/README.rst#purl). | ||
* E.g. 'maven' for Gradle projects. | ||
*/ | ||
@JvmOverloads | ||
fun Identifier.toPurl(qualifiers: Map<String, String> = emptyMap(), subpath: String = "") = | ||
if (this == Identifier.EMPTY) "" else createPurl(getPurlType(), namespace, name, version, qualifiers, subpath) | ||
|
||
/** | ||
* Create the canonical [package URL](https://github.com/package-url/purl-spec) ("purl") based on given properties: | ||
* [type] (which must be a String representation of a [PurlType] instance, [namespace], [name] and [version]. | ||
|
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
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
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