Skip to content

Commit

Permalink
add Index constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Pan <[email protected]>
  • Loading branch information
Patrick Pan committed Nov 25, 2024
1 parent ddbf048 commit d6e8499
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/OrasProject.Oras/Content/Digest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal static string Validate(string? digest)

return digest;
}

/// <summary>
/// Generates a SHA-256 digest from a byte array.
/// </summary>
Expand All @@ -58,4 +59,4 @@ internal static string ComputeSHA256(byte[] content)
var output = $"sha256:{BitConverter.ToString(hash).Replace("-", "")}";
return output.ToLower();
}
}
}
18 changes: 12 additions & 6 deletions src/OrasProject.Oras/Oci/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -43,14 +44,19 @@ public class Index : Versioned
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public IDictionary<string, string>? Annotations { get; set; }

public Index() {}

[SetsRequiredMembers]
public Index(IList<Descriptor> manifests)
{
Manifests = manifests;
MediaType = Oci.MediaType.ImageIndex;
SchemaVersion = 2;
}

internal static (Descriptor, byte[]) GenerateIndex(IList<Descriptor> manifests)
{
var index = new Index()
{
Manifests = manifests,
MediaType = Oci.MediaType.ImageIndex,
SchemaVersion = 2
};
var index = new Index(manifests);
var indexContent = JsonSerializer.SerializeToUtf8Bytes(index);
return (Descriptor.Create(indexContent, Oci.MediaType.ImageIndex), indexContent);
}
Expand Down

0 comments on commit d6e8499

Please sign in to comment.