From b0c08d18bec3e2353a75d8273653e8098803ae8c Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Sun, 14 Jul 2024 13:41:49 +0200 Subject: [PATCH] Improve readability of OciRepositoryHandler.getOrHeadGradleModuleMetadata --- .../internal/registry/OciRepositoryHandler.kt | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt index ff10743d..32e799e1 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt @@ -186,13 +186,26 @@ internal class OciRepositoryHandler( isGet: Boolean, response: HttpServerResponse, ): Publisher { + data class OciImageVariantsMetadata( + val imageDefName: String, + val capabilities: Set, + val platformToMetadata: Map, + val digest: OciDigest, + val size: Int, + ) val componentId = mappedComponent.componentId - val variantMetadataMonoList = mappedComponent.variants.map { (variantName, variant) -> + val imageVariantsMetadataMonoList = mappedComponent.variants.map { (imageDefName, variant) -> getImageMetadata(registryUri, variant.imageReference, credentials).map { imageMetadata -> - Triple(variantName, variant.capabilities, imageMetadata) + OciImageVariantsMetadata( + imageDefName, + variant.capabilities, + imageMetadata.platformToMetadata, + imageMetadata.digest, + imageMetadata.size, + ) } } - val moduleJsonMono = variantMetadataMonoList.zip { variantMetadataList -> + val moduleJsonMono = imageVariantsMetadataMonoList.zip { imageVariantsMetadataList -> jsonObject { addString("formatVersion", "1.1") addObject("component") { @@ -205,18 +218,18 @@ internal class OciRepositoryHandler( } val fileNamePrefix = "${componentId.name}-${componentId.version}-" addArray("variants") { - for ((imageDefName, capabilities, imageMetadata) in variantMetadataList) { + for ((imageDefName, capabilities, platformToMetadata, digest, size) in imageVariantsMetadataList) { addObject { addString("name", createOciVariantName(imageDefName)) addOciVariantAttributes(MULTIPLE_PLATFORMS_ATTRIBUTE_VALUE) addCapabilities("capabilities", capabilities, componentId) addArray("dependencies") { - for (platform in imageMetadata.platformToMetadata.keys) { + for (platform in platformToMetadata.keys) { addDependency(componentId, capabilities, platform) } } } - for ((platform, metadata) in imageMetadata.platformToMetadata) { + for ((platform, metadata) in platformToMetadata) { addObject { addString("name", createOciVariantName(imageDefName, platform)) addOciVariantAttributes(platform.toString()) @@ -235,7 +248,7 @@ internal class OciRepositoryHandler( val metadataName = fileNamePrefix + createOciMetadataClassifier(imageDefName) + createPlatformPostfix(platform) + ".json" val escapedImageReference = metadata.imageReference.toString().escapePathSegment() addString("name", metadataName) - addString("url", "$escapedImageReference/${imageMetadata.digest}/${imageMetadata.size}/$platform/$metadataName") + addString("url", "$escapedImageReference/$digest/$size/$platform/$metadataName") addNumber("size", metadataJson.size.toLong()) addString("sha512", DigestUtils.sha512Hex(metadataJson)) addString("sha256", DigestUtils.sha256Hex(metadataJson))