Skip to content

Commit

Permalink
Cleanup list/set/mapOf() -> emptyList/Set/Map()
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Apr 15, 2024
1 parent d41f332 commit 4f98cf8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import java.time.Instant
*/
internal class OciComponentBuilder : Serializable {
private var imageReference: OciImageReference? = null
private var capabilities: Set<VersionedCoordinates> = setOf()
private var capabilities: Set<VersionedCoordinates> = emptySet()
private var bundleOrPlatformBundles: OciComponent.BundleOrPlatformBundles? = null
private var indexAnnotations: Map<String, String> = mapOf()
private var indexAnnotations: Map<String, String> = emptyMap()

fun imageReference(v: OciImageReference) = apply { imageReference = v }
fun capabilities(v: Set<VersionedCoordinates>) = apply { capabilities = v }
Expand All @@ -32,18 +32,18 @@ internal class OciComponentBundleBuilder : Serializable {
private var creationTime: SerializableInstant? = null
private var author: String? = null
private var user: String? = null
private var ports: Set<String> = setOf()
private var environment: Map<String, String> = mapOf()
private var ports: Set<String> = emptySet()
private var environment: Map<String, String> = emptyMap()
private var command: OciComponent.Bundle.Command? = null
private var volumes: Set<String> = setOf()
private var volumes: Set<String> = emptySet()
private var workingDirectory: String? = null
private var stopSignal: String? = null
private var configAnnotations: Map<String, String> = mapOf()
private var configDescriptorAnnotations: Map<String, String> = mapOf()
private var manifestAnnotations: Map<String, String> = mapOf()
private var manifestDescriptorAnnotations: Map<String, String> = mapOf()
private var parentCapabilities: List<Coordinates> = listOf()
private var layers: List<OciComponent.Bundle.Layer> = listOf()
private var configAnnotations: Map<String, String> = emptyMap()
private var configDescriptorAnnotations: Map<String, String> = emptyMap()
private var manifestAnnotations: Map<String, String> = emptyMap()
private var manifestDescriptorAnnotations: Map<String, String> = emptyMap()
private var parentCapabilities: List<Coordinates> = emptyList()
private var layers: List<OciComponent.Bundle.Layer> = emptyList()

fun parentCapabilities(v: List<Coordinates>) = apply { parentCapabilities = v }
fun creationTime(v: Instant?) = apply { creationTime = v?.toSerializableInstant() }
Expand Down Expand Up @@ -89,7 +89,7 @@ internal class OciComponentBundleCommandBuilder : Serializable {

fun build() = when {
(entryPoint == null) && (arguments == null) -> null
else -> OciComponent.Bundle.Command(entryPoint, arguments ?: listOf())
else -> OciComponent.Bundle.Command(entryPoint, arguments ?: emptyList())
}
}

Expand All @@ -115,7 +115,7 @@ internal class OciComponentBundleLayerDescriptorBuilder : Serializable {
private var diffId: OciDigest? = null
private var classifier: String? = null
private var extension: String? = null
private var annotations: Map<String, String> = mapOf()
private var annotations: Map<String, String> = emptyMap()

fun digest(v: OciDigest?) = apply { digest = v }
fun size(v: Long?) = apply { size = v }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private fun JsonObject.decodePlatform() = PlatformImpl(
)

private fun JsonObject.decodeBundle() = OciComponent.Bundle(
getOrNull("parentCapabilities") { asArray().toList { asObject().decodeCoordinates() } } ?: listOf(),
getOrNull("parentCapabilities") { asArray().toList { asObject().decodeCoordinates() } } ?: emptyList(),
getInstantOrNull("creationTime"),
getStringOrNull("author"),
getStringOrNull("user"),
Expand All @@ -53,7 +53,7 @@ private fun JsonObject.decodeBundle() = OciComponent.Bundle(
getStringMapOrEmpty("configDescriptorAnnotations"),
getStringMapOrEmpty("manifestAnnotations"),
getStringMapOrEmpty("manifestDescriptorAnnotations"),
getOrNull("layers") { asArray().toList { asObject().decodeLayer() } } ?: listOf(),
getOrNull("layers") { asArray().toList { asObject().decodeLayer() } } ?: emptyList(),
)

private fun JsonObject.decodeCommand() = OciComponent.Bundle.Command(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface OciExtension {
architecture: String,
variant: String = "",
osVersion: String = "",
osFeatures: Set<String> = setOf(),
osFeatures: Set<String> = emptySet(),
): Platform

fun platformFilter(configuration: Action<in PlatformFilterBuilder>): PlatformFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ internal fun <T, U, R> Provider<T>.zipAbsentAsNull(other: Provider<U>, combiner:
internal fun <T, E, R> Provider<T>.zipAbsentAsEmptySet(
other: Provider<Set<E>>,
combiner: (T, Set<E>) -> R,
): Provider<R> = zip(other.orElse(setOf())) { t, set -> combiner(t, set) }
): Provider<R> = zip(other.orElse(emptySet())) { t, set -> combiner(t, set) }

internal fun <T, K, V, R> Provider<T>.zipAbsentAsEmptyMap(
other: Provider<Map<K, V>>,
combiner: (T, Map<K, V>) -> R,
): Provider<R> = zip(other.orElse(mapOf())) { t, map -> combiner(t, map) }
): Provider<R> = zip(other.orElse(emptyMap())) { t, map -> combiner(t, map) }
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ internal class OciComponentRegistry(val registryApi: OciRegistryApi) {
val manifestAnnotations = manifestJsonObject.getStringMapOrEmpty("annotations")
val configDescriptor = manifestJsonObject.get("config") { asObject().decodeOciDescriptor(configMediaType) }
val layerDescriptors =
manifestJsonObject.getOrNull("layers") { asArray().toList { asObject().decodeOciDescriptor() } } ?: listOf()
manifestJsonObject.getOrNull("layers") { asArray().toList { asObject().decodeOciDescriptor() } }
?: emptyList()
manifestJsonObject.requireStringOrNull("mediaType", manifestMediaType)
manifestJsonObject.requireLong("schemaVersion", 2)
return registryApi.pullBlobAsString(registry, imageName, configDescriptor.digest, configDescriptor.size, credentials).map { config ->
Expand All @@ -199,7 +200,7 @@ internal class OciComponentRegistry(val registryApi: OciRegistryApi) {
// sorted for canonical json: Cmd, Entrypoint, Env, ExposedPorts, Labels, StopSignal, User, Volumes, WorkingDir
val arguments = getStringListOrNull("Cmd")
val entryPoint = getStringListOrNull("Entrypoint")
command = if ((entryPoint == null) && (arguments == null)) null else OciComponent.Bundle.Command(entryPoint, arguments ?: listOf())
command = if ((entryPoint == null) && (arguments == null)) null else OciComponent.Bundle.Command(entryPoint, arguments ?: emptyList())
environment = getOrNull("Env") {
asArray().toMap(TreeMap()) {
val environmentString = asString()
Expand Down Expand Up @@ -282,7 +283,7 @@ internal class OciComponentRegistry(val registryApi: OciRegistryApi) {
Pair(
PlatformImpl(os, architecture, variant, osVersion, osFeatures),
OciComponent.Bundle(
listOf(),
emptyList(),
creationTime,
author,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ internal data class OciRegistryTokenPayload(
internal fun JsonObject.decodeOciRegistryTokenPayload() = OciRegistryTokenPayload(
getStringOrNull("iss"),
getStringOrNull("sub"),
getOrNull("aud") { if (isString()) listOf(asString()) else asArray().toStringList() } ?: listOf(),
getOrNull("aud") { if (isString()) listOf(asString()) else asArray().toStringList() } ?: emptyList(),
getInstantOfEpochSecondOrNull("exp"),
getInstantOfEpochSecondOrNull("nbf"),
getInstantOfEpochSecondOrNull("iat"),
getStringOrNull("jti"),
getOrNull("access") { asArray().toSet(HashSet()) { asObject().decodeResourceScope() } } ?: setOf(),
getOrNull("access") { asArray().toSet(HashSet()) { asObject().decodeResourceScope() } } ?: emptySet(),
)

private fun JsonObject.getInstantOfEpochSecondOrNull(key: String) = getOrNull(key) { Instant.ofEpochSecond(asLong()) }
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ private class OrPlatformFilter(filters: Array<FieldPlatformFilter>) : PlatformFi

private fun normalizeFilters(filters: Array<FieldPlatformFilter>): Array<FieldPlatformFilter> {
fun <K> MutableMap<K, Set<String>>.merge(key: K, value: String?) {
val valueSet = if (value == null) setOf() else setOf(value)
val valueSet = if (value == null) emptySet() else setOf(value)
merge(key, valueSet) { a, b ->
if (a.isEmpty() || b.isEmpty()) setOf() else a + b
if (a.isEmpty() || b.isEmpty()) emptySet() else a + b
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ internal class OciComponentResolverTest {
)
}

private fun createBundle(name: String, parentCapabilities: List<Coordinates> = listOf()) = OciComponent.Bundle(
private fun createBundle(name: String, parentCapabilities: List<Coordinates> = emptyList()) = OciComponent.Bundle(
parentCapabilities,
null,
name,
Expand All @@ -324,7 +324,7 @@ internal class OciComponentResolverTest {
sortedMapOf(),
sortedMapOf(),
sortedMapOf(),
listOf()
emptyList(),
)
}

Expand Down

0 comments on commit 4f98cf8

Please sign in to comment.