|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.ossreviewtoolkit.clients.clearlydefined |
| 21 | + |
| 22 | +import kotlinx.serialization.Serializable |
| 23 | + |
| 24 | +/** |
| 25 | + * See https://github.com/clearlydefined/service/blob/b339cb7/schemas/curations-1.0.json#L64-L83 and |
| 26 | + * https://docs.clearlydefined.io/using-data#a-note-on-definition-coordinates. |
| 27 | + */ |
| 28 | +@Serializable(CoordinatesSerializer::class) |
| 29 | +data class Coordinates( |
| 30 | + /** |
| 31 | + * The type of the component. For example, npm, git, nuget, maven, etc. This talks about the shape of the |
| 32 | + * component. |
| 33 | + */ |
| 34 | + val type: ComponentType, |
| 35 | + |
| 36 | + /** |
| 37 | + * Where the component can be found. Examples include npmjs, mavencentral, github, nuget, etc. |
| 38 | + */ |
| 39 | + val provider: Provider, |
| 40 | + |
| 41 | + /** |
| 42 | + * Many component systems have namespaces: GitHub orgs, NPM namespace, Maven group id, etc. This segment must be |
| 43 | + * supplied. If your component does not have a namespace, use '-' (ASCII hyphen). |
| 44 | + */ |
| 45 | + val namespace: String? = null, |
| 46 | + |
| 47 | + /** |
| 48 | + * The name of the component. Given the mentioned [namespace] segment, this is just the simple name. |
| 49 | + */ |
| 50 | + val name: String, |
| 51 | + |
| 52 | + /** |
| 53 | + * Components typically have some differentiator like a version or commit id. Use that here. If this segment is |
| 54 | + * omitted, the latest revision is used (if that makes sense for the provider). |
| 55 | + */ |
| 56 | + val revision: String? = null |
| 57 | +) { |
| 58 | + constructor(value: String) : this(value.split('/', limit = 5)) |
| 59 | + |
| 60 | + private constructor(parts: List<String>) : this( |
| 61 | + type = ComponentType.fromString(parts[0]), |
| 62 | + provider = Provider.fromString(parts[1]), |
| 63 | + namespace = parts[2].takeUnless { it == "-" }, |
| 64 | + name = parts[3], |
| 65 | + revision = parts.getOrNull(4) |
| 66 | + ) |
| 67 | + |
| 68 | + override fun toString() = listOfNotNull(type, provider, namespace ?: "-", name, revision).joinToString("/") |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * See https://github.com/clearlydefined/service/blob/4917725/schemas/definition-1.0.json#L211-L235. |
| 73 | + */ |
| 74 | +@Serializable |
| 75 | +data class SourceLocation( |
| 76 | + // The following properties match those of Coordinates, except that the revision is mandatory here. |
| 77 | + val type: ComponentType, |
| 78 | + val provider: Provider, |
| 79 | + val namespace: String? = null, |
| 80 | + val name: String, |
| 81 | + val revision: String, |
| 82 | + |
| 83 | + val path: String? = null, |
| 84 | + val url: String? = null |
| 85 | +) |
| 86 | + |
| 87 | +/** |
| 88 | + * Convert a [SourceLocation] to a [Coordinates] object. |
| 89 | + */ |
| 90 | +fun SourceLocation.toCoordinates(): Coordinates = Coordinates(type, provider, namespace, name, revision) |
0 commit comments