Skip to content

Commit

Permalink
Normalize platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed May 29, 2024
1 parent 0f2274d commit 31c69ce
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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") ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +52,7 @@ internal abstract class OciExtensionImpl @Inject constructor(private val objectF
variant: String,
osVersion: String,
osFeatures: Set<String>,
): Platform = PlatformImpl(os, architecture, variant, osVersion, osFeatures.toSortedSet())
) = Platform(os, architecture, variant, osVersion, osFeatures.toSortedSet())

final override fun platformFilter(configuration: Action<in OciExtension.PlatformFilterBuilder>): PlatformFilter {
val builder = objectFactory.newInstance<OciExtension.PlatformFilterBuilder>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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") ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ import java.util.*
/**
* @author Silvio Giebl
*/
interface Platform : Comparable<Platform>, Serializable {
sealed interface Platform : Comparable<Platform>, Serializable {
val os: String
val architecture: String
val variant: String
val osVersion: String
val osFeatures: SortedSet<String>
}

internal data class PlatformImpl(
internal fun Platform(
os: String,
architecture: String,
variant: String,
osVersion: String,
osFeatures: SortedSet<String>,
): 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,
Expand All @@ -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 -> ""
}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 31c69ce

Please sign in to comment.