Skip to content

Commit 093b9db

Browse files
committed
refactor(scancode): Split the model into classes in a separate package
This improves the overview quite a bit. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 8c44641 commit 093b9db

File tree

6 files changed

+211
-133
lines changed

6 files changed

+211
-133
lines changed

plugins/scanners/scancode/src/main/kotlin/ScanCodeResultModelMapper.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import org.ossreviewtoolkit.model.TextLocation
3232
import org.ossreviewtoolkit.model.createAndLogIssue
3333
import org.ossreviewtoolkit.model.mapLicense
3434
import org.ossreviewtoolkit.model.utils.associateLicensesWithExceptions
35+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.FileEntry
36+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.LicenseEntry
37+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.ScanCodeResult
3538

3639
import org.semver4j.Semver
3740

plugins/scanners/scancode/src/main/kotlin/ScanCodeResultParser.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import kotlinx.serialization.json.jsonArray
3030
import kotlinx.serialization.json.jsonObject
3131
import kotlinx.serialization.modules.SerializersModule
3232

33+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.CopyrightEntry
34+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.FileEntry
35+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.LicenseEntry
36+
import org.ossreviewtoolkit.plugins.scanners.scancode.model.ScanCodeResult
37+
3338
import org.semver4j.Semver
3439

3540
fun parseResult(result: File) = parseResult(result.readText())
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2023 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.plugins.scanners.scancode.model
21+
22+
import kotlinx.serialization.Serializable
23+
24+
/**
25+
* An interface to be able to treat all versions of copyright entries the same.
26+
*
27+
* Note that the data class constructors of the individual version's implementation of this interface should contain
28+
* only those properties which are actually present in the data, in the order used in the data, to exactly resemble that
29+
* version's data model. Other properties required to implement the interface should be added to the body of the data
30+
* class to clearly separate data model properties from "synthetic" interface properties.
31+
*/
32+
sealed interface CopyrightEntry {
33+
val statement: String
34+
val startLine: Int
35+
val endLine: Int
36+
37+
@Serializable
38+
data class Version1(
39+
val value: String,
40+
override val startLine: Int,
41+
override val endLine: Int
42+
) : CopyrightEntry {
43+
override val statement = value
44+
}
45+
46+
@Serializable
47+
data class Version2(
48+
val copyright: String,
49+
override val startLine: Int,
50+
override val endLine: Int
51+
) : CopyrightEntry {
52+
override val statement = copyright
53+
}
54+
}

plugins/scanners/scancode/src/main/kotlin/ScanCodeResultModel.kt renamed to plugins/scanners/scancode/src/main/kotlin/model/FileEntry.kt

Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,20 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
package org.ossreviewtoolkit.plugins.scanners.scancode
20+
package org.ossreviewtoolkit.plugins.scanners.scancode.model
2121

22-
import java.io.File
22+
import kotlin.collections.flatMap
23+
import kotlin.collections.orEmpty
2324

2425
import kotlinx.serialization.Serializable
25-
import kotlinx.serialization.SerializationException
26-
import kotlinx.serialization.json.JsonArray
27-
import kotlinx.serialization.json.JsonElement
28-
import kotlinx.serialization.json.JsonPrimitive
29-
import kotlinx.serialization.json.jsonPrimitive
3026

27+
import org.ossreviewtoolkit.plugins.scanners.scancode.ScanCode
3128
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
3229
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
3330
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseWithExceptionExpression
3431
import org.ossreviewtoolkit.utils.spdx.toSpdx
3532
import org.ossreviewtoolkit.utils.spdx.toSpdxId
3633

37-
@Serializable
38-
data class ScanCodeResult(
39-
val headers: List<HeaderEntry>,
40-
val files: List<FileEntry>,
41-
val licenseReferences: List<LicenseReference>? = null // Available only with "--license-references".
42-
)
43-
44-
@Serializable
45-
data class HeaderEntry(
46-
val toolName: String,
47-
val toolVersion: String,
48-
val options: Map<String, JsonElement>,
49-
val startTimestamp: String,
50-
val endTimestamp: String,
51-
val outputFormatVersion: String
52-
) {
53-
fun getInput(): File {
54-
val inputPath = when (val input = options.getValue("input")) {
55-
is JsonPrimitive -> input.content
56-
is JsonArray -> input.first().jsonPrimitive.content
57-
else -> throw SerializationException("Unknown input element type.")
58-
}
59-
60-
return File(inputPath)
61-
}
62-
63-
fun getPrimitiveOptions(): List<Pair<String, String>> =
64-
options.mapNotNull { entry ->
65-
(entry.value as? JsonPrimitive)?.let { entry.key to it.content }
66-
}
67-
}
68-
6934
/**
7035
* An interface to be able to treat all versions of file entries the same.
7136
*
@@ -149,97 +114,3 @@ sealed interface FileEntry {
149114
data class LicenseDetection(
150115
val matches: List<LicenseEntry>
151116
)
152-
153-
/**
154-
* An interface to be able to treat all versions of license entries the same.
155-
*
156-
* Note that the data class constructors of the individual version's implementation of this interface should contain
157-
* only those properties which are actually present in the data, in the order used in the data, to exactly resemble that
158-
* version's data model. Other properties required to implement the interface should be added to the body of the data
159-
* class to clearly separate data model properties from "synthetic" interface properties.
160-
*/
161-
sealed interface LicenseEntry {
162-
val licenseExpression: String
163-
val startLine: Int
164-
val endLine: Int
165-
val score: Float
166-
val matchedText: String?
167-
168-
@Serializable
169-
data class Version1(
170-
val key: String,
171-
override val score: Float,
172-
val spdxLicenseKey: String? = null, // This might be explicitly set to null in JSON.
173-
override val startLine: Int,
174-
override val endLine: Int,
175-
val matchedRule: LicenseRule,
176-
override val matchedText: String? = null
177-
) : LicenseEntry {
178-
override val licenseExpression = matchedRule.licenseExpression
179-
}
180-
181-
@Serializable
182-
data class Version3(
183-
override val licenseExpression: String,
184-
val spdxLicenseExpression: String? = null,
185-
val fromFile: String? = null,
186-
override val startLine: Int,
187-
override val endLine: Int,
188-
override val score: Float,
189-
override val matchedText: String? = null
190-
) : LicenseEntry
191-
192-
@Serializable
193-
data class Version4(
194-
override val licenseExpression: String,
195-
val licenseExpressionSpdx: String? = null,
196-
val fromFile: String? = null,
197-
override val startLine: Int,
198-
override val endLine: Int,
199-
override val score: Float,
200-
override val matchedText: String? = null
201-
) : LicenseEntry
202-
}
203-
204-
@Serializable
205-
data class LicenseRule(
206-
val licenseExpression: String
207-
)
208-
209-
/**
210-
* An interface to be able to treat all versions of copyright entries the same.
211-
*
212-
* Note that the data class constructors of the individual version's implementation of this interface should contain
213-
* only those properties which are actually present in the data, in the order used in the data, to exactly resemble that
214-
* version's data model. Other properties required to implement the interface should be added to the body of the data
215-
* class to clearly separate data model properties from "synthetic" interface properties.
216-
*/
217-
sealed interface CopyrightEntry {
218-
val statement: String
219-
val startLine: Int
220-
val endLine: Int
221-
222-
@Serializable
223-
data class Version1(
224-
val value: String,
225-
override val startLine: Int,
226-
override val endLine: Int
227-
) : CopyrightEntry {
228-
override val statement = value
229-
}
230-
231-
@Serializable
232-
data class Version2(
233-
val copyright: String,
234-
override val startLine: Int,
235-
override val endLine: Int
236-
) : CopyrightEntry {
237-
override val statement = copyright
238-
}
239-
}
240-
241-
@Serializable
242-
data class LicenseReference(
243-
val key: String,
244-
val spdxLicenseKey: String
245-
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2023 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.plugins.scanners.scancode.model
21+
22+
import kotlinx.serialization.Serializable
23+
24+
/**
25+
* An interface to be able to treat all versions of license entries the same.
26+
*
27+
* Note that the data class constructors of the individual version's implementation of this interface should contain
28+
* only those properties which are actually present in the data, in the order used in the data, to exactly resemble that
29+
* version's data model. Other properties required to implement the interface should be added to the body of the data
30+
* class to clearly separate data model properties from "synthetic" interface properties.
31+
*/
32+
sealed interface LicenseEntry {
33+
val licenseExpression: String
34+
val startLine: Int
35+
val endLine: Int
36+
val score: Float
37+
val matchedText: String?
38+
39+
@Serializable
40+
data class Version1(
41+
val key: String,
42+
override val score: Float,
43+
val spdxLicenseKey: String? = null, // This might be explicitly set to null in JSON.
44+
override val startLine: Int,
45+
override val endLine: Int,
46+
val matchedRule: LicenseRule,
47+
override val matchedText: String? = null
48+
) : LicenseEntry {
49+
override val licenseExpression = matchedRule.licenseExpression
50+
}
51+
52+
@Serializable
53+
data class Version3(
54+
override val licenseExpression: String,
55+
val spdxLicenseExpression: String? = null,
56+
val fromFile: String? = null,
57+
override val startLine: Int,
58+
override val endLine: Int,
59+
override val score: Float,
60+
override val matchedText: String? = null
61+
) : LicenseEntry
62+
63+
@Serializable
64+
data class Version4(
65+
override val licenseExpression: String,
66+
val licenseExpressionSpdx: String? = null,
67+
val fromFile: String? = null,
68+
override val startLine: Int,
69+
override val endLine: Int,
70+
override val score: Float,
71+
override val matchedText: String? = null
72+
) : LicenseEntry
73+
}
74+
75+
@Serializable
76+
data class LicenseRule(
77+
val licenseExpression: String
78+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2023 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.plugins.scanners.scancode.model
21+
22+
import java.io.File
23+
24+
import kotlinx.serialization.Serializable
25+
import kotlinx.serialization.SerializationException
26+
import kotlinx.serialization.json.JsonArray
27+
import kotlinx.serialization.json.JsonElement
28+
import kotlinx.serialization.json.JsonPrimitive
29+
import kotlinx.serialization.json.jsonPrimitive
30+
31+
@Serializable
32+
data class ScanCodeResult(
33+
val headers: List<HeaderEntry>,
34+
val files: List<FileEntry>,
35+
val licenseReferences: List<LicenseReference>? = null // Available only with "--license-references".
36+
)
37+
38+
@Serializable
39+
data class HeaderEntry(
40+
val toolName: String,
41+
val toolVersion: String,
42+
val options: Map<String, JsonElement>,
43+
val startTimestamp: String,
44+
val endTimestamp: String,
45+
val outputFormatVersion: String
46+
) {
47+
fun getInput(): File {
48+
val inputPath = when (val input = options.getValue("input")) {
49+
is JsonPrimitive -> input.content
50+
is JsonArray -> input.first().jsonPrimitive.content
51+
else -> throw SerializationException("Unknown input element type.")
52+
}
53+
54+
return File(inputPath)
55+
}
56+
57+
fun getPrimitiveOptions(): List<Pair<String, String>> =
58+
options.mapNotNull { entry ->
59+
(entry.value as? JsonPrimitive)?.let { entry.key to it.content }
60+
}
61+
}
62+
63+
@Serializable
64+
data class LicenseReference(
65+
val key: String,
66+
val spdxLicenseKey: String
67+
)

0 commit comments

Comments
 (0)