Skip to content

Commit

Permalink
refactor!: move constants
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Dec 29, 2023
1 parent 1286713 commit abed335
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 143 deletions.
59 changes: 0 additions & 59 deletions src/OrasProject.Oras/Constants/OCIMediaTypes .cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/OrasProject.Oras/Content/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Constants;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Interfaces;
using OrasProject.Oras.Oci;
Expand Down Expand Up @@ -39,17 +38,17 @@ public static async Task<IList<Descriptor>> SuccessorsAsync(IFetcher fetcher, De
{
switch (node.MediaType)
{
case DockerMediaTypes.Manifest:
case OCIMediaTypes.ImageManifest:
case Docker.MediaType.Manifest:
case Oci.MediaType.ImageManifest:
{
var content = await FetchAllAsync(fetcher, node, cancellationToken);
var manifest = JsonSerializer.Deserialize<Manifest>(content);
var descriptors = new List<Descriptor>() { manifest.Config };
descriptors.AddRange(manifest.Layers);
return descriptors;
}
case DockerMediaTypes.ManifestList:
case OCIMediaTypes.ImageIndex:
case Docker.MediaType.ManifestList:
case Oci.MediaType.ImageIndex:
{
var content = await FetchAllAsync(fetcher, node, cancellationToken);
// docker manifest list and oci index are equivalent for successors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace OrasProject.Oras.Constants
namespace OrasProject.Oras.Docker;

/// <summary>
/// Docker media types
/// </summary>
public static class MediaType
{
public static class DockerMediaTypes
{
// Docker media types
public const string Config = "application/vnd.docker.container.image.v1+json";
public const string ManifestList = "application/vnd.docker.distribution.manifest.list.v2+json";
public const string Manifest = "application/vnd.docker.distribution.manifest.v2+json";
public const string ForeignLayer = "application/vnd.docker.image.rootfs.foreign.diff.tar.gzip";
}
public const string Config = "application/vnd.docker.container.image.v1+json";
public const string ManifestList = "application/vnd.docker.distribution.manifest.list.v2+json";
public const string Manifest = "application/vnd.docker.distribution.manifest.v2+json";
public const string ForeignLayer = "application/vnd.docker.image.rootfs.foreign.diff.tar.gzip";
}
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Oci/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class Index : Versioned
// MediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.index.v1+json`
[JsonPropertyName("mediaType")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? MediaType { get; set; }

public string? MediaType { get; set; }

[JsonPropertyName("artifactType")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? ArtifactType { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/OrasProject.Oras/Oci/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class Manifest : Versioned

[JsonPropertyName("artifactType")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? ArtifactType { get; set; }

public string? ArtifactType { get; set; }

[JsonPropertyName("config")]
public required Descriptor Config { get; set; }

[JsonPropertyName("layers")]
public required IList<Descriptor> Layers { get; set; }
public required IList<Descriptor> Layers { get; set; }

[JsonPropertyName("subject")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
Expand Down
84 changes: 84 additions & 0 deletions src/OrasProject.Oras/Oci/MediaType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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.Oci;

public static class MediaType
{
/// <summary>
/// Descriptor specifies the media type for a content descriptor.
/// </summary>
public const string Descriptor = "application/vnd.oci.descriptor.v1+json";

/// <summary>
/// LayoutHeader specifies the media type for the oci-layout.
/// </summary>
public const string LayoutHeader = "application/vnd.oci.layout.header.v1+json";

/// <summary>
/// ImageIndex specifies the media type for an image index.
/// </summary>
public const string ImageIndex = "application/vnd.oci.image.index.v1+json";

/// <summary>
/// ImageManifest specifies the media type for an image manifest.
/// </summary>
public const string ImageManifest = "application/vnd.oci.image.manifest.v1+json";

/// <summary>
/// ImageConfig specifies the media type for the image configuration.
/// </summary>
public const string ImageConfig = "application/vnd.oci.image.config.v1+json";

/// <summary>
/// EmptyJSON specifies the media type for an unused blob containing the value "{}".
/// </summary>
public const string EmptyJson = "application/vnd.oci.empty.v1+json";

/// <summary>
/// ImageLayer is the media type used for layers referenced by the manifest.
/// </summary>
public const string ImageLayer = "application/vnd.oci.image.layer.v1.tar";

/// <summary>
/// ImageLayerGzip is the media type used for gzipped layers
/// referenced by the manifest.
/// </summary>
public const string ImageLayerGzip = "application/vnd.oci.image.layer.v1.tar+gzip";

/// <summary>
/// ImageLayerZstd is the media type used for zstd compressed
/// layers referenced by the manifest.
/// </summary>
public const string ImageLayerZstd = "application/vnd.oci.image.layer.v1.tar+zstd";

/// <summary>
/// ImageLayerNonDistributable is the media type for layers referenced by
/// the manifest but with distribution restrictions.
/// </summary>
public const string ImageLayerNonDistributable = "application/vnd.oci.image.layer.nondistributable.v1.tar";

/// <summary>
/// ImageLayerNonDistributableGzip is the media type for
/// gzipped layers referenced by the manifest but with distribution
/// restrictions.
/// </summary>
public const string ImageLayerNonDistributableGzip = "application/vnd.oci.image.layer.nondistributable.v1.tar+gzip";

/// <summary>
/// ImageLayerNonDistributableZstd is the media type for zstd
/// compressed layers referenced by the manifest but with distribution
/// restrictions.
/// </summary>
public const string ImageLayerNonDistributableZstd = "application/vnd.oci.image.layer.nondistributable.v1.tar+zstd";
}
9 changes: 4 additions & 5 deletions src/OrasProject.Oras/Remote/ManifestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Constants;
using OrasProject.Oras.Oci;
using System.Linq;

Expand All @@ -21,10 +20,10 @@ internal static class ManifestUtility
{
internal static string[] DefaultManifestMediaTypes = new[]
{
DockerMediaTypes.Manifest,
DockerMediaTypes.ManifestList,
OCIMediaTypes.ImageIndex,
OCIMediaTypes.ImageManifest
Docker.MediaType.Manifest,
Docker.MediaType.ManifestList,
Oci.MediaType.ImageIndex,
Oci.MediaType.ImageManifest
};

/// <summary>
Expand Down
17 changes: 8 additions & 9 deletions tests/OrasProject.Oras.Tests/CopyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Constants;
using OrasProject.Oras.Memory;
using OrasProject.Oras.Oci;
using System.Text;
Expand Down Expand Up @@ -54,12 +53,12 @@ public async Task CanCopyBetweenMemoryTargetsWithTaggedNode()
Layers = layers
};
var manifestBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(manifest));
appendBlob(OCIMediaTypes.ImageManifest, manifestBytes);
appendBlob(MediaType.ImageManifest, manifestBytes);
};
var getBytes = (string data) => Encoding.UTF8.GetBytes(data);
appendBlob(OCIMediaTypes.ImageConfig, getBytes("config")); // blob 0
appendBlob(OCIMediaTypes.ImageLayer, getBytes("foo")); // blob 1
appendBlob(OCIMediaTypes.ImageLayer, getBytes("bar")); // blob 2
appendBlob(MediaType.ImageConfig, getBytes("config")); // blob 0
appendBlob(MediaType.ImageLayer, getBytes("foo")); // blob 1
appendBlob(MediaType.ImageLayer, getBytes("bar")); // blob 2
generateManifest(descs[0], descs.GetRange(1, 2)); // blob 3

for (var i = 0; i < blobs.Count; i++)
Expand Down Expand Up @@ -117,12 +116,12 @@ public async Task CanCopyBetweenMemoryTargets()
Layers = layers
};
var manifestBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(manifest));
appendBlob(OCIMediaTypes.ImageManifest, manifestBytes);
appendBlob(MediaType.ImageManifest, manifestBytes);
};
var getBytes = (string data) => Encoding.UTF8.GetBytes(data);
appendBlob(OCIMediaTypes.ImageConfig, getBytes("config")); // blob 0
appendBlob(OCIMediaTypes.ImageLayer, getBytes("foo")); // blob 1
appendBlob(OCIMediaTypes.ImageLayer, getBytes("bar")); // blob 2
appendBlob(MediaType.ImageConfig, getBytes("config")); // blob 0
appendBlob(MediaType.ImageLayer, getBytes("foo")); // blob 1
appendBlob(MediaType.ImageLayer, getBytes("bar")); // blob 2
generateManifest(descs[0], descs.GetRange(1, 2)); // blob 3

for (var i = 0; i < blobs.Count; i++)
Expand Down
13 changes: 6 additions & 7 deletions tests/OrasProject.Oras.Tests/MemoryTest/MemoryTargetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Constants;
using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Memory;
using OrasProject.Oras.Oci;
Expand Down Expand Up @@ -193,7 +192,7 @@ public async Task ShouldReturnPredecessorsOfNodes()
Layers = layers
};
var manifestBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(manifest));
appendBlob(OCIMediaTypes.ImageManifest, manifestBytes);
appendBlob(MediaType.ImageManifest, manifestBytes);
};

var generateIndex = (List<Descriptor> manifests) =>
Expand All @@ -203,13 +202,13 @@ public async Task ShouldReturnPredecessorsOfNodes()
Manifests = manifests
};
var indexBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(index));
appendBlob(OCIMediaTypes.ImageIndex, indexBytes);
appendBlob(MediaType.ImageIndex, indexBytes);
};
var getBytes = (string data) => Encoding.UTF8.GetBytes(data);
appendBlob(OCIMediaTypes.ImageConfig, getBytes("config")); // blob 0
appendBlob(OCIMediaTypes.ImageLayer, getBytes("foo")); // blob 1
appendBlob(OCIMediaTypes.ImageLayer, getBytes("bar")); // blob 2
appendBlob(OCIMediaTypes.ImageLayer, getBytes("hello")); // blob 3
appendBlob(MediaType.ImageConfig, getBytes("config")); // blob 0
appendBlob(MediaType.ImageLayer, getBytes("foo")); // blob 1
appendBlob(MediaType.ImageLayer, getBytes("bar")); // blob 2
appendBlob(MediaType.ImageLayer, getBytes("hello")); // blob 3
generateManifest(descs[0], descs.GetRange(1, 2)); // blob 4
generateManifest(descs[0], new() { descs[3] }); // blob 5
generateManifest(descs[0], descs.GetRange(1, 3)); // blob 6
Expand Down
Loading

0 comments on commit abed335

Please sign in to comment.