diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt index 8e74e0b3..d2b01772 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt @@ -46,7 +46,7 @@ internal class OciVariant( ) internal class OciLayer( // TODO internal? - val descriptor: OciMetadata.Layer.Descriptor, + val descriptor: OciLayerDescriptor, val file: File, ) @@ -168,7 +168,7 @@ abstract class OciImagesInputTask : DefaultTask() { return digestToLayerFile } - private fun checkDuplicateLayer(layerDescriptor: OciMetadata.Layer.Descriptor, file1: File, file2: File) { + private fun checkDuplicateLayer(layerDescriptor: OciLayerDescriptor, file1: File, file2: File) { if (!FileUtils.contentEquals(file1, file2)) { throw IllegalStateException("hash collision for digest ${layerDescriptor.digest}: expected file contents of $file1 and $file2 to be the same") } diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt index 9956e265..1e2d0bc5 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt @@ -277,15 +277,15 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( OciMetadataBuilder::manifestDescriptorAnnotations, ) .zip(imageDefinition.indexAnnotations.orElse(emptyMap()), OciMetadataBuilder::indexAnnotations) - .zip(createMetadataLayers(providerFactory), OciMetadataBuilder::layers) + .zip(createLayerMetadataList(providerFactory), OciMetadataBuilder::layers) .map { it.build() } - private fun createMetadataLayers(providerFactory: ProviderFactory): Provider> = + private fun createLayerMetadataList(providerFactory: ProviderFactory): Provider> = providerFactory.provider { layers.list }.flatMap { layers -> - var listProvider = providerFactory.provider { listOf() } + var listProvider = providerFactory.provider { listOf() } for (layer in layers) { layer as Layer - listProvider = listProvider.zip(layer.createMetadataLayer(providerFactory)) { list, e -> list + e } + listProvider = listProvider.zip(layer.createLayerMetadata(providerFactory)) { list, e -> list + e } } listProvider } @@ -407,26 +407,23 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( fun getTask() = externalTask ?: task - fun createMetadataLayer(providerFactory: ProviderFactory): Provider = - providerFactory.provider { OciMetadataLayerBuilder() } - .zipAbsentAsNull(metadata.creationTime, OciMetadataLayerBuilder::creationTime) - .zipAbsentAsNull(metadata.author, OciMetadataLayerBuilder::author) - .zipAbsentAsNull(metadata.createdBy, OciMetadataLayerBuilder::createdBy) - .zipAbsentAsNull(metadata.comment, OciMetadataLayerBuilder::comment) - .zipAbsentAsNull( - createMetadataLayerDescriptor(providerFactory), - OciMetadataLayerBuilder::descriptor, - ) + fun createLayerMetadata(providerFactory: ProviderFactory): Provider = + providerFactory.provider { OciLayerMetadataBuilder() } + .zipAbsentAsNull(metadata.creationTime, OciLayerMetadataBuilder::creationTime) + .zipAbsentAsNull(metadata.author, OciLayerMetadataBuilder::author) + .zipAbsentAsNull(metadata.createdBy, OciLayerMetadataBuilder::createdBy) + .zipAbsentAsNull(metadata.comment, OciLayerMetadataBuilder::comment) + .zipAbsentAsNull(createLayerDescriptor(providerFactory), OciLayerMetadataBuilder::descriptor) .map { it.build() } - private fun createMetadataLayerDescriptor(providerFactory: ProviderFactory): Provider { + private fun createLayerDescriptor(providerFactory: ProviderFactory): Provider { val task = providerFactory.provider { getTask() }.flatMap { it } - return providerFactory.provider { OciMetadataLayerDescriptorBuilder() } - .zip(metadata.annotations.orElse(emptyMap()), OciMetadataLayerDescriptorBuilder::annotations) - .zipAbsentAsNull(task.flatMap { it.mediaType }, OciMetadataLayerDescriptorBuilder::mediaType) - .zipAbsentAsNull(task.flatMap { it.digest }, OciMetadataLayerDescriptorBuilder::digest) - .zipAbsentAsNull(task.flatMap { it.size }, OciMetadataLayerDescriptorBuilder::size) - .zipAbsentAsNull(task.flatMap { it.diffId }, OciMetadataLayerDescriptorBuilder::diffId) + return providerFactory.provider { OciLayerDescriptorBuilder() } + .zip(metadata.annotations.orElse(emptyMap()), OciLayerDescriptorBuilder::annotations) + .zipAbsentAsNull(task.flatMap { it.mediaType }, OciLayerDescriptorBuilder::mediaType) + .zipAbsentAsNull(task.flatMap { it.digest }, OciLayerDescriptorBuilder::digest) + .zipAbsentAsNull(task.flatMap { it.size }, OciLayerDescriptorBuilder::size) + .zipAbsentAsNull(task.flatMap { it.diffId }, OciLayerDescriptorBuilder::diffId) .map { it.build() } } } diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciImageMetadataRegistry.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciImageMetadataRegistry.kt index 7e7fbdd6..469778bc 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciImageMetadataRegistry.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciImageMetadataRegistry.kt @@ -242,13 +242,13 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) { } var i = 0 val layers = history?.map { historyEntry -> - OciMetadata.Layer( + OciLayerMetadata( if (historyEntry.emptyLayer) null else run { // TODO index check val descriptor = layerDescriptors[i] val diffId = diffIds[i] i++ - OciMetadata.Layer.Descriptor( + OciLayerDescriptor( normalizeLayerMediaType(descriptor.mediaType), descriptor.digest, descriptor.size, @@ -263,8 +263,8 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) { ) // TODO check i == layerDescriptors.size } ?: layerDescriptors.zip(diffIds) { descriptor, diffId -> - OciMetadata.Layer( - OciMetadata.Layer.Descriptor( + OciLayerMetadata( + OciLayerDescriptor( normalizeLayerMediaType(descriptor.mediaType), descriptor.digest, descriptor.size, diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadata.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadata.kt index 75c8cdc1..6d7e2bf9 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadata.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadata.kt @@ -24,23 +24,21 @@ data class OciMetadata( val manifestAnnotations: SortedMap, val manifestDescriptorAnnotations: SortedMap, val indexAnnotations: SortedMap, - val layers: List, -) : Serializable { + val layers: List, +) : Serializable - data class Layer( - val descriptor: Descriptor?, - val creationTime: Instant?, - val author: String?, - val createdBy: String?, - val comment: String?, - ) : Serializable { +data class OciLayerMetadata( + val descriptor: OciLayerDescriptor?, + val creationTime: Instant?, + val author: String?, + val createdBy: String?, + val comment: String?, +) : Serializable - data class Descriptor( - override val mediaType: String, - override val digest: OciDigest, - override val size: Long, - val diffId: OciDigest, - override val annotations: SortedMap, - ) : OciDescriptor, Serializable - } -} +data class OciLayerDescriptor( + override val mediaType: String, + override val digest: OciDigest, + override val size: Long, + val diffId: OciDigest, + override val annotations: SortedMap, +) : OciDescriptor, Serializable diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataBuilder.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataBuilder.kt index 0a457118..6df6398c 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataBuilder.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataBuilder.kt @@ -23,7 +23,7 @@ internal class OciMetadataBuilder : Serializable { private var manifestAnnotations: Map = emptyMap() private var manifestDescriptorAnnotations: Map = emptyMap() private var indexAnnotations: Map = emptyMap() - private var layers: List = emptyList() + private var layers: List = emptyList() fun imageReference(v: OciImageReference) = apply { imageReference = v } fun creationTime(v: Instant?) = apply { creationTime = v?.toSerializableInstant() } @@ -41,7 +41,7 @@ internal class OciMetadataBuilder : Serializable { fun manifestAnnotations(v: Map) = apply { manifestAnnotations = v } fun manifestDescriptorAnnotations(v: Map) = apply { manifestDescriptorAnnotations = v } fun indexAnnotations(v: Map) = apply { indexAnnotations = v } - fun layers(v: List) = apply { layers = v } + fun layers(v: List) = apply { layers = v } fun build() = OciMetadata( imageReference!!, @@ -64,23 +64,23 @@ internal class OciMetadataBuilder : Serializable { ) } -internal class OciMetadataLayerBuilder : Serializable { - private var descriptor: OciMetadata.Layer.Descriptor? = null +internal class OciLayerMetadataBuilder : Serializable { + private var descriptor: OciLayerDescriptor? = null private var creationTime: SerializableInstant? = null private var author: String? = null private var createdBy: String? = null private var comment: String? = null - fun descriptor(v: OciMetadata.Layer.Descriptor?) = apply { descriptor = v } + fun descriptor(v: OciLayerDescriptor?) = apply { descriptor = v } fun creationTime(v: Instant?) = apply { creationTime = v?.toSerializableInstant() } fun author(v: String?) = apply { author = v } fun createdBy(v: String?) = apply { createdBy = v } fun comment(v: String?) = apply { comment = v } - fun build() = OciMetadata.Layer(descriptor, creationTime?.toInstant(), author, createdBy, comment) + fun build() = OciLayerMetadata(descriptor, creationTime?.toInstant(), author, createdBy, comment) } -internal class OciMetadataLayerDescriptorBuilder : Serializable { +internal class OciLayerDescriptorBuilder : Serializable { private var mediaType: String? = null private var digest: OciDigest? = null private var size: Long? = null @@ -95,7 +95,7 @@ internal class OciMetadataLayerDescriptorBuilder : Serializable { fun build() = when { (mediaType == null) && (digest == null) && (size == null) && (diffId == null) && annotations.isEmpty() -> null - else -> OciMetadata.Layer.Descriptor( + else -> OciLayerDescriptor( mediaType!!, digest!!, size!!, diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataJsonCodec.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataJsonCodec.kt index e04c0b02..9d11e5a4 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataJsonCodec.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciMetadataJsonCodec.kt @@ -21,12 +21,12 @@ private fun JsonObjectStringBuilder.encodeOciMetadata(metadata: OciMetadata) { addObjectIfNotEmpty("manifestAnnotations", metadata.manifestAnnotations) addObjectIfNotEmpty("manifestDescriptorAnnotations", metadata.manifestDescriptorAnnotations) addObjectIfNotEmpty("indexAnnotations", metadata.indexAnnotations) - addArrayIfNotEmpty("layers", metadata.layers) { addObject { encodeLayer(it) } } + addArrayIfNotEmpty("layers", metadata.layers) { addObject { encodeOciLayerMetadata(it) } } } -private fun JsonObjectStringBuilder.encodeLayer(layer: OciMetadata.Layer) { +private fun JsonObjectStringBuilder.encodeOciLayerMetadata(layer: OciLayerMetadata) { layer.descriptor?.let { descriptor -> - addObject("descriptor") { encodeLayerDescriptor(descriptor) } + addObject("descriptor") { encodeOciLayerDescriptor(descriptor) } } addStringIfNotNull("creationTime", layer.creationTime?.toString()) addStringIfNotNull("author", layer.author) @@ -34,7 +34,7 @@ private fun JsonObjectStringBuilder.encodeLayer(layer: OciMetadata.Layer) { addStringIfNotNull("comment", layer.comment) } -private fun JsonObjectStringBuilder.encodeLayerDescriptor(descriptor: OciMetadata.Layer.Descriptor) { +private fun JsonObjectStringBuilder.encodeOciLayerDescriptor(descriptor: OciLayerDescriptor) { if (descriptor.mediaType != GZIP_COMPRESSED_LAYER_MEDIA_TYPE) { addString("mediaType", descriptor.mediaType) } @@ -63,18 +63,18 @@ private fun JsonObject.decodeOciMetadata() = OciMetadata( getStringMapOrEmpty("manifestAnnotations"), getStringMapOrEmpty("manifestDescriptorAnnotations"), getStringMapOrEmpty("indexAnnotations"), - getOrNull("layers") { asArray().toList { asObject().decodeLayer() } } ?: emptyList(), + getOrNull("layers") { asArray().toList { asObject().decodeOciLayerMetadata() } } ?: emptyList(), ) -private fun JsonObject.decodeLayer() = OciMetadata.Layer( - getOrNull("descriptor") { asObject().decodeLayerDescriptor() }, +private fun JsonObject.decodeOciLayerMetadata() = OciLayerMetadata( + getOrNull("descriptor") { asObject().decodeOciLayerDescriptor() }, getInstantOrNull("creationTime"), getStringOrNull("author"), getStringOrNull("createdBy"), getStringOrNull("comment"), ) -private fun JsonObject.decodeLayerDescriptor() = OciMetadata.Layer.Descriptor( +private fun JsonObject.decodeOciLayerDescriptor() = OciLayerDescriptor( getStringOrNull("metadata") ?: GZIP_COMPRESSED_LAYER_MEDIA_TYPE, getOciDigest("digest"), getLong("size"),