From 31c69ce65ef8cbae79247c1ac7a976a3f364d838 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Thu, 30 May 2024 00:23:18 +0200 Subject: [PATCH] Normalize platforms --- .../oci/component/OciComponentDecoding.kt | 4 ++-- .../oci/internal/dsl/OciExtensionImpl.kt | 3 +-- .../internal/registry/OciComponentRegistry.kt | 5 ++--- .../sgtsilvio/gradle/oci/platform/Platform.kt | 18 ++++++++++++++++-- .../oci/component/OciComponentResolverTest.kt | 8 ++++---- .../gradle/oci/platform/PlatformImplTest.kt | 18 +++++++++--------- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentDecoding.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentDecoding.kt index 76980219..a39a1a01 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentDecoding.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentDecoding.kt @@ -4,7 +4,7 @@ import io.github.sgtsilvio.gradle.oci.internal.json.* import io.github.sgtsilvio.gradle.oci.metadata.GZIP_COMPRESSED_LAYER_MEDIA_TYPE import io.github.sgtsilvio.gradle.oci.metadata.getOciDigest import io.github.sgtsilvio.gradle.oci.metadata.toOciImageReference -import io.github.sgtsilvio.gradle.oci.platform.PlatformImpl +import io.github.sgtsilvio.gradle.oci.platform.Platform import java.util.* internal fun String.decodeAsJsonToOciComponent() = jsonObject(this).decodeOciComponent() @@ -30,7 +30,7 @@ private fun JsonArray.decodePlatformBundles() = OciComponent.PlatformBundles(toM } }) -private fun JsonObject.decodePlatform() = PlatformImpl( +private fun JsonObject.decodePlatform() = Platform( getString("os"), getString("architecture"), getStringOrNull("variant") ?: "", diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciExtensionImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciExtensionImpl.kt index 00857ccb..03cd61e2 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciExtensionImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciExtensionImpl.kt @@ -11,7 +11,6 @@ import io.github.sgtsilvio.gradle.oci.mapping.OciImageMapping import io.github.sgtsilvio.gradle.oci.mapping.OciImageMappingImpl import io.github.sgtsilvio.gradle.oci.platform.Platform import io.github.sgtsilvio.gradle.oci.platform.PlatformFilter -import io.github.sgtsilvio.gradle.oci.platform.PlatformImpl import org.gradle.api.Action import org.gradle.api.model.ObjectFactory import org.gradle.kotlin.dsl.domainObjectContainer @@ -53,7 +52,7 @@ internal abstract class OciExtensionImpl @Inject constructor(private val objectF variant: String, osVersion: String, osFeatures: Set, - ): Platform = PlatformImpl(os, architecture, variant, osVersion, osFeatures.toSortedSet()) + ) = Platform(os, architecture, variant, osVersion, osFeatures.toSortedSet()) final override fun platformFilter(configuration: Action): PlatformFilter { val builder = objectFactory.newInstance() diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciComponentRegistry.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciComponentRegistry.kt index 81bad1fd..91e09cd6 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciComponentRegistry.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciComponentRegistry.kt @@ -5,7 +5,6 @@ import io.github.sgtsilvio.gradle.oci.component.VersionedCoordinates import io.github.sgtsilvio.gradle.oci.internal.json.* import io.github.sgtsilvio.gradle.oci.metadata.* import io.github.sgtsilvio.gradle.oci.platform.Platform -import io.github.sgtsilvio.gradle.oci.platform.PlatformImpl import reactor.core.publisher.Flux import reactor.core.publisher.Mono import java.time.Instant @@ -277,7 +276,7 @@ internal class OciComponentRegistry(val registryApi: OciRegistryApi) { } Pair( - PlatformImpl(os, architecture, variant, osVersion, osFeatures), + Platform(os, architecture, variant, osVersion, osFeatures), OciComponent.Bundle( emptyList(), creationTime, @@ -322,7 +321,7 @@ internal class OciComponentRegistry(val registryApi: OciRegistryApi) { decodeOciDescriptor(manifestMediaType), // TODO support nested index ) // TODO order? - private fun JsonObject.decodePlatform(): Platform = PlatformImpl( + private fun JsonObject.decodePlatform() = Platform( getString("os"), getString("architecture"), getStringOrNull("variant") ?: "", diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/platform/Platform.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/platform/Platform.kt index 77476c78..58fd90f8 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/platform/Platform.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/platform/Platform.kt @@ -7,7 +7,7 @@ import java.util.* /** * @author Silvio Giebl */ -interface Platform : Comparable, Serializable { +sealed interface Platform : Comparable, Serializable { val os: String val architecture: String val variant: String @@ -15,7 +15,15 @@ interface Platform : Comparable, Serializable { val osFeatures: SortedSet } -internal data class PlatformImpl( +internal fun Platform( + os: String, + architecture: String, + variant: String, + osVersion: String, + osFeatures: SortedSet, +): Platform = PlatformImpl(os, architecture, variant.ifEmpty { defaultVariant(architecture) }, osVersion, osFeatures) + +private data class PlatformImpl( override val os: String, override val architecture: String, override val variant: String, @@ -41,3 +49,9 @@ internal data class PlatformImpl( return osFeatures.compareTo(other.osFeatures) } } + +private fun defaultVariant(architecture: String) = when (architecture) { + "arm64" -> "v8" + "arm" -> "v7" + else -> "" +} diff --git a/src/test/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentResolverTest.kt b/src/test/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentResolverTest.kt index 364bbc34..e6312a1d 100644 --- a/src/test/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentResolverTest.kt +++ b/src/test/kotlin/io/github/sgtsilvio/gradle/oci/component/OciComponentResolverTest.kt @@ -1,7 +1,7 @@ package io.github.sgtsilvio.gradle.oci.component import io.github.sgtsilvio.gradle.oci.metadata.OciImageReference -import io.github.sgtsilvio.gradle.oci.platform.PlatformImpl +import io.github.sgtsilvio.gradle.oci.platform.Platform import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test @@ -10,9 +10,9 @@ import org.junit.jupiter.api.Test */ internal class OciComponentResolverTest { - private val amd64 = PlatformImpl("amd64", "linux", "", "", sortedSetOf()) - private val arm64v8 = PlatformImpl("arm64", "linux", "v8", "", sortedSetOf()) - private val arm32v7 = PlatformImpl("arm", "linux", "v7", "", sortedSetOf()) + private val amd64 = Platform("amd64", "linux", "", "", sortedSetOf()) + private val arm64v8 = Platform("arm64", "linux", "v8", "", sortedSetOf()) + private val arm32v7 = Platform("arm", "linux", "v7", "", sortedSetOf()) @Test fun singleComponentWithoutPlatforms_resolvesToInfinitePlatforms() { diff --git a/src/test/kotlin/io/github/sgtsilvio/gradle/oci/platform/PlatformImplTest.kt b/src/test/kotlin/io/github/sgtsilvio/gradle/oci/platform/PlatformImplTest.kt index e11594e8..8b784528 100644 --- a/src/test/kotlin/io/github/sgtsilvio/gradle/oci/platform/PlatformImplTest.kt +++ b/src/test/kotlin/io/github/sgtsilvio/gradle/oci/platform/PlatformImplTest.kt @@ -6,53 +6,53 @@ import org.junit.jupiter.api.Test /** * @author Silvio Giebl */ -class PlatformImplTest { +class PlatformTest { @Test fun `toString nothingEmpty`() { - val platform = PlatformImpl("os", "arch", "variant", "123", sortedSetOf("f1", "f2")) + val platform = Platform("os", "arch", "variant", "123", sortedSetOf("f1", "f2")) assertEquals("@os,arch,variant,123,f1,f2", platform.toString()) } @Test fun `toString emptyVariant`() { - val platform = PlatformImpl("os", "arch", "", "123", sortedSetOf("f1", "f2")) + val platform = Platform("os", "arch", "", "123", sortedSetOf("f1", "f2")) assertEquals("@os,arch,,123,f1,f2", platform.toString()) } @Test fun `toString emptyVariant emptyOsFeatures`() { - val platform = PlatformImpl("os", "arch", "", "123", sortedSetOf()) + val platform = Platform("os", "arch", "", "123", sortedSetOf()) assertEquals("@os,arch,,123", platform.toString()) } @Test fun `toString emptyVariant emptyOsVersion`() { - val platform = PlatformImpl("os", "arch", "", "", sortedSetOf("f1", "f2")) + val platform = Platform("os", "arch", "", "", sortedSetOf("f1", "f2")) assertEquals("@os,arch,,,f1,f2", platform.toString()) } @Test fun `toString emptyVariant emptyOsVersion emptyOsFeatures`() { - val platform = PlatformImpl("os", "arch", "", "", sortedSetOf()) + val platform = Platform("os", "arch", "", "", sortedSetOf()) assertEquals("@os,arch", platform.toString()) } @Test fun `toString emptyOsVersion`() { - val platform = PlatformImpl("os", "arch", "variant", "", sortedSetOf("f1", "f2")) + val platform = Platform("os", "arch", "variant", "", sortedSetOf("f1", "f2")) assertEquals("@os,arch,variant,,f1,f2", platform.toString()) } @Test fun `toString emptyOsVersion emptyOsFeatures`() { - val platform = PlatformImpl("os", "arch", "variant", "", sortedSetOf()) + val platform = Platform("os", "arch", "variant", "", sortedSetOf()) assertEquals("@os,arch,variant", platform.toString()) } @Test fun `toString emptyOsFeatures`() { - val platform = PlatformImpl("os", "arch", "variant", "123", sortedSetOf()) + val platform = Platform("os", "arch", "variant", "123", sortedSetOf()) assertEquals("@os,arch,variant,123", platform.toString()) } } \ No newline at end of file