From ce21442e1833eec934fabf54a306cc05516664a1 Mon Sep 17 00:00:00 2001 From: nhu1997 Date: Mon, 11 Nov 2024 22:05:53 -0800 Subject: [PATCH] update code based on comments Signed-off-by: nhu1997 --- src/OrasProject.Oras/Oci/Descriptor.cs | 11 ++++++--- src/OrasProject.Oras/PackManifestOptions.cs | 2 +- src/OrasProject.Oras/Packer.cs | 12 ++++++---- src/OrasProject.Oras/Utils/MediaType.cs | 21 ----------------- tests/OrasProject.Oras.Tests/PackerTest.cs | 26 +++++++-------------- 5 files changed, 25 insertions(+), 47 deletions(-) delete mode 100644 src/OrasProject.Oras/Utils/MediaType.cs diff --git a/src/OrasProject.Oras/Oci/Descriptor.cs b/src/OrasProject.Oras/Oci/Descriptor.cs index 8d1ea0f..12fe348 100644 --- a/src/OrasProject.Oras/Oci/Descriptor.cs +++ b/src/OrasProject.Oras/Oci/Descriptor.cs @@ -56,13 +56,18 @@ public static Descriptor Create(Span data, string mediaType) return new Descriptor { MediaType = mediaType, - Data = byteData, - Digest = OrasProject.Oras.Content.Digest.ComputeSHA256(byteData), + Digest = Content.Digest.ComputeSHA256(byteData), Size = byteData.Length }; } - public static Descriptor Empty => Descriptor.Create(new byte[] { 0x7B, 0x7D }, OrasProject.Oras.Oci.MediaType.EmptyJson); + public static Descriptor Empty => new() + { + MediaType = Oci.MediaType.EmptyJson, + Digest = "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", + Size = 2, + Data = [0x7B, 0x7D] + }; internal BasicDescriptor BasicDescriptor => new BasicDescriptor(MediaType, Digest, Size); } diff --git a/src/OrasProject.Oras/PackManifestOptions.cs b/src/OrasProject.Oras/PackManifestOptions.cs index 4d67e9f..cd859c8 100644 --- a/src/OrasProject.Oras/PackManifestOptions.cs +++ b/src/OrasProject.Oras/PackManifestOptions.cs @@ -26,7 +26,7 @@ public struct PackManifestOptions /// Config references a configuration object for a container, by digest /// For more details: https://github.com/opencontainers/image-spec/blob/v1.1.0/manifest.md#image-manifest-property-descriptions. /// - public Descriptor Config { get; set; } + public Descriptor? Config { get; set; } /// /// Layers is an array of objects, and each object id a Content Descriptor (or simply Descriptor) diff --git a/src/OrasProject.Oras/Packer.cs b/src/OrasProject.Oras/Packer.cs index 88669c0..b526789 100644 --- a/src/OrasProject.Oras/Packer.cs +++ b/src/OrasProject.Oras/Packer.cs @@ -14,12 +14,10 @@ using OrasProject.Oras.Oci; using OrasProject.Oras.Content; using OrasProject.Oras.Exceptions; -using OrasProject.Oras.Utils; using System; using System.Collections.Generic; using System.IO; -using System.Text; using System.Text.RegularExpressions; using System.Text.Json; using System.Threading; @@ -45,6 +43,10 @@ public static class Packer /// private const string _errMissingArtifactType = "missing artifact type"; + public const string UnknownConfig = "application/vnd.unknown.config.v1+json"; + + public const string UnknownArtifact = "application/vnd.unknown.artifact.v1"; + /// /// ManifestVersion represents the manifest version used for PackManifest /// @@ -141,7 +143,7 @@ private static async Task PackManifestV1_0Async(IPushable pusher, st { if (string.IsNullOrEmpty(artifactType)) { - artifactType = Utils.MediaType.UnknownConfig; + artifactType = UnknownConfig; } ValidateMediaType(artifactType); configDescriptor = await PushCustomEmptyConfigAsync(pusher, artifactType, options.ConfigAnnotations, cancellationToken); @@ -171,7 +173,7 @@ private static async Task PackManifestV1_0Async(IPushable pusher, st /// private static async Task PackManifestV1_1Async(IPushable pusher, string? artifactType, PackManifestOptions options = default, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(artifactType) && (options.Config == null || options.Config.MediaType == Oci.MediaType.EmptyJson)) + if (string.IsNullOrEmpty(artifactType) && (options.Config == null || options.Config.MediaType == MediaType.EmptyJson)) { throw new MissingArtifactTypeException(_errMissingArtifactType); } else if (!string.IsNullOrEmpty(artifactType)) { @@ -205,7 +207,7 @@ private static async Task PackManifestV1_1Async(IPushable pusher, st var manifest = new Manifest { SchemaVersion = 2, - MediaType = Oci.MediaType.ImageManifest, + MediaType = MediaType.ImageManifest, ArtifactType = artifactType, Subject = options.Subject, Config = options.Config, diff --git a/src/OrasProject.Oras/Utils/MediaType.cs b/src/OrasProject.Oras/Utils/MediaType.cs deleted file mode 100644 index 802817b..0000000 --- a/src/OrasProject.Oras/Utils/MediaType.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright The ORAS Authors. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace OrasProject.Oras.Utils; - -public static class MediaType -{ - public const string UnknownConfig = "application/vnd.unknown.config.v1+json"; - - public const string UnknownArtifact = "application/vnd.unknown.artifact.v1"; -} diff --git a/tests/OrasProject.Oras.Tests/PackerTest.cs b/tests/OrasProject.Oras.Tests/PackerTest.cs index 3311c75..51211cc 100644 --- a/tests/OrasProject.Oras.Tests/PackerTest.cs +++ b/tests/OrasProject.Oras.Tests/PackerTest.cs @@ -17,7 +17,6 @@ using System.Text; using System.Text.Json; using Xunit; -using OrasProject.Oras.Utils; namespace OrasProject.Oras.Tests; @@ -54,8 +53,7 @@ public async Task TestPackManifestImageV1_0() { MediaType = artifactType, Digest = Digest.ComputeSHA256(expectedConfigData), - Size = expectedConfigData.Length, - Data = expectedConfigData + Size = expectedConfigData.Length }; var expectedConfigBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(expectedConfig)); var incomingConfig = manifest?.Config; @@ -202,8 +200,7 @@ public async Task TestPackManifestImageV1_0_WithOptions() { MediaType = expectedManifest.MediaType, Digest = Digest.ComputeSHA256(expectedManifestBytes), - Size = expectedManifestBytes.Length, - Data = expectedManifestBytes + Size = expectedManifestBytes.Length }; expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType; expectedManifestDesc.Annotations = expectedManifest.Annotations; @@ -224,9 +221,8 @@ public async Task TestPackManifestImageV1_0_WithOptions() { MediaType = artifactType, Digest = Digest.ComputeSHA256(configBytes), - Size = configBytes.Length, Annotations = configAnnotations, - Data = configBytes + Size = configBytes.Length }; expectedManifest = new Manifest { @@ -249,8 +245,7 @@ public async Task TestPackManifestImageV1_0_WithOptions() { MediaType = expectedManifest.MediaType, Digest = Digest.ComputeSHA256(expectedManifestBytes), - Size = expectedManifestBytes.Length, - Data = expectedManifestBytes + Size = expectedManifestBytes.Length }; expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType; expectedManifestDesc.Annotations = expectedManifest.Annotations; @@ -302,8 +297,8 @@ public async Task TestPackManifestImageV1_0_NoArtifactType() // Verify artifact type and config media type - Assert.Equal(Utils.MediaType.UnknownConfig, manifestDesc.ArtifactType); - Assert.Equal(Utils.MediaType.UnknownConfig, manifest!.Config.MediaType); + Assert.Equal(Packer.UnknownConfig, manifestDesc.ArtifactType); + Assert.Equal(Packer.UnknownConfig, manifest!.Config.MediaType); } [Fact] @@ -521,8 +516,7 @@ public async Task TestPackManifestImageV1_1_WithOptions() { MediaType = expectedManifest.MediaType, Digest = Digest.ComputeSHA256(expectedManifestBytes), - Size = expectedManifestBytes.Length, - Data = expectedManifestBytes + Size = expectedManifestBytes.Length }; expectedManifestDesc.ArtifactType = expectedManifest.Config.MediaType; expectedManifestDesc.Annotations = expectedManifest.Annotations; @@ -552,8 +546,7 @@ public async Task TestPackManifestImageV1_1_WithOptions() { MediaType = expectedManifest.MediaType, Digest = Digest.ComputeSHA256(expectedManifestBytes), - Size = expectedManifestBytes.Length, - Data = expectedManifestBytes + Size = expectedManifestBytes.Length }; expectedManifestDesc.Annotations = expectedManifest.Annotations; Assert.Equal(JsonSerializer.SerializeToUtf8Bytes(expectedManifestDesc), JsonSerializer.SerializeToUtf8Bytes(manifestDesc)); @@ -589,8 +582,7 @@ public async Task TestPackManifestImageV1_1_WithOptions() { MediaType = expectedManifest.MediaType, Digest = Digest.ComputeSHA256(expectedManifestBytes), - Size = expectedManifestBytes.Length, - Data = expectedManifestBytes + Size = expectedManifestBytes.Length }; expectedManifestDesc.ArtifactType = artifactType; expectedManifestDesc.Annotations = expectedManifest.Annotations;