Skip to content

Commit 972c168

Browse files
authored
Convenience methods for layout ref (#287)
2 parents c207f9a + 329df75 commit 972c168

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/main/generated/Versions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
@NullMarked
3232
public final class Versions {
33+
34+
private Versions() {
35+
// Prevent instantiation
36+
}
37+
3338
/**
3439
* User agent value
3540
*/

src/main/java/land/oras/LayoutRef.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import land.oras.exception.OrasException;
2626
import land.oras.utils.SupportedAlgorithm;
2727
import org.jspecify.annotations.NullMarked;
28+
import org.jspecify.annotations.Nullable;
2829

2930
/**
3031
* A referer of a container on a {@link OCILayout}.
@@ -43,7 +44,7 @@ public final class LayoutRef extends Ref<LayoutRef> {
4344
* Private constructor
4445
* @param tag The tag.
4546
*/
46-
private LayoutRef(Path folder, String tag) {
47+
private LayoutRef(Path folder, @Nullable String tag) {
4748
super(tag);
4849
this.folder = folder;
4950
}
@@ -85,6 +86,25 @@ public static LayoutRef parse(String name) {
8586
return new LayoutRef(path, tag);
8687
}
8788

89+
/**
90+
* Return a new layout reference for a path and digest or tag
91+
* @param layout The OCI layout
92+
* @param digest The digest
93+
* @return The layout ref
94+
*/
95+
public static LayoutRef of(OCILayout layout, String digest) {
96+
return new LayoutRef(layout.getPath(), digest);
97+
}
98+
99+
/**
100+
* Return a new layout reference for a path and digest or tag
101+
* @param layout The OCI layout
102+
* @return The layout ref
103+
*/
104+
public static LayoutRef of(OCILayout layout) {
105+
return new LayoutRef(layout.getPath(), null);
106+
}
107+
88108
/**
89109
* Return a new layout reference for a path
90110
* @param path The path

src/main/java/land/oras/Ref.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract sealed class Ref<T extends Ref<T>> permits ContainerRef, LayoutR
4040
* Constructor
4141
* @param tag The tag
4242
*/
43-
protected Ref(String tag) {
43+
protected Ref(@Nullable String tag) {
4444
this.tag = tag;
4545
}
4646

src/test/java/land/oras/OCILayoutTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ void shouldPushBlob() throws IOException {
599599
byte[] content = "hi".getBytes(StandardCharsets.UTF_8);
600600
String digest = SupportedAlgorithm.SHA256.digest(content);
601601

602-
LayoutRef layoutRef = LayoutRef.parse("%s@%s".formatted(path.toString(), digest));
603602
OCILayout ociLayout = OCILayout.Builder.builder().defaults(path).build();
603+
LayoutRef layoutRef = LayoutRef.of(ociLayout, digest);
604604

605605
// Push more blobs
606606
ociLayout.pushBlob(layoutRef, "hi".getBytes(StandardCharsets.UTF_8));
@@ -621,11 +621,12 @@ void cannotPushBlobWithoutTagOrDigest() throws IOException {
621621

622622
Path invalidBlobPushDir = layoutPath.resolve("shouldPushArtifact");
623623

624-
LayoutRef noTagLayout = LayoutRef.parse("%s".formatted(invalidBlobPushDir.toString()));
625-
LayoutRef noDigestLayout = LayoutRef.parse("%s:latest".formatted(invalidBlobPushDir.toString()));
626624
OCILayout ociLayout =
627625
OCILayout.Builder.builder().defaults(invalidBlobPushDir).build();
628626

627+
LayoutRef noTagLayout = LayoutRef.of(ociLayout);
628+
LayoutRef noDigestLayout = LayoutRef.of(ociLayout, "latest");
629+
629630
// Push more blobs
630631
assertThrows(OrasException.class, () -> {
631632
ociLayout.pushBlob(noTagLayout, "hi".getBytes(StandardCharsets.UTF_8));
@@ -665,7 +666,7 @@ void testShouldCopyArtifactFromRegistryIntoOciLayout() throws IOException {
665666
.build();
666667

667668
OCILayout ociLayout = OCILayout.builder().defaults(layoutPath).build();
668-
LayoutRef layoutRef = LayoutRef.parse("%s".formatted(ociLayout.getPath()));
669+
LayoutRef layoutRef = LayoutRef.of(ociLayout);
669670

670671
ContainerRef containerRef =
671672
ContainerRef.parse("%s/library/artifact-oci-layout".formatted(this.registry.getRegistry()));

0 commit comments

Comments
 (0)