Skip to content

Commit 395122f

Browse files
authored
Add utils to remove one descriptor from Index (#614)
Signed-off-by: Valentin Delaye <jonesbusy@users.noreply.github.com>
1 parent d3f9225 commit 395122f

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/main/java/land/oras/Index.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.Objects;
3434
import java.util.function.BiPredicate;
35+
import land.oras.exception.OrasException;
3536
import land.oras.utils.Const;
3637
import land.oras.utils.JsonUtils;
3738
import org.jspecify.annotations.NonNull;
@@ -203,6 +204,36 @@ public String getArtifactTypeAsString() {
203204
return artifactType;
204205
}
205206

207+
/**
208+
* Return a new index with the given manifest descriptor removed from the index
209+
* @param descriptor The manifest descriptor to remove
210+
* @return The index with the manifest descriptor removed
211+
*/
212+
public Index withRemovedDescriptor(ManifestDescriptor descriptor) {
213+
List<ManifestDescriptor> newManifests = new LinkedList<>(manifests);
214+
boolean found = false;
215+
for (ManifestDescriptor d : manifests) {
216+
if (d.getDigest().equals(descriptor.getDigest())) {
217+
found = true;
218+
newManifests.remove(d);
219+
}
220+
}
221+
if (!found) {
222+
throw new OrasException("Cannot remove manifest descriptor with digest " + descriptor.getDigest()
223+
+ " because it does not exist in the index");
224+
}
225+
return new Index(
226+
schemaVersion,
227+
mediaType,
228+
ArtifactType.from(artifactType),
229+
newManifests,
230+
annotations,
231+
subject,
232+
this.descriptor,
233+
registry,
234+
json);
235+
}
236+
206237
/**
207238
* Return a new index with new manifest added to index
208239
* @param manifest The manifest

src/test/java/land/oras/IndexTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.List;
2626
import java.util.Map;
27+
import land.oras.exception.OrasException;
2728
import land.oras.utils.Const;
2829
import land.oras.utils.SupportedAlgorithm;
2930
import org.junit.jupiter.api.Test;
@@ -118,6 +119,28 @@ void shouldReadAndWriteIndexWithArtifactType() {
118119
assertEquals(json, index.toJson());
119120
}
120121

122+
@Test
123+
void shouldRemoveManifest() {
124+
125+
// Create 2 descriptors
126+
Manifest newManifest1 = Manifest.empty().withAnnotations(Map.of("foo", "bar"));
127+
ManifestDescriptor descriptor1 = ManifestDescriptor.of(newManifest1);
128+
Manifest newManifest2 = Manifest.empty().withAnnotations(Map.of("foo1", "bar2"));
129+
ManifestDescriptor descriptor2 = ManifestDescriptor.of(newManifest2);
130+
131+
Index index = Index.fromManifests(List.of(descriptor1, descriptor2));
132+
assertEquals(2, index.getManifests().size());
133+
134+
final Index indexFinal = index.withRemovedDescriptor(descriptor1);
135+
assertEquals(1, indexFinal.getManifests().size());
136+
assertEquals(descriptor2.getDigest(), indexFinal.getManifests().get(0).getDigest());
137+
138+
OrasException e = assertThrows(OrasException.class, () -> indexFinal.withRemovedDescriptor(descriptor1));
139+
assertEquals(
140+
"Cannot remove manifest descriptor with digest sha256:d37ee97188d29dd838306ac7c5d35c03cc4cdac37c6a9e98529fee5aa0d65635 because it does not exist in the index",
141+
e.getMessage());
142+
}
143+
121144
@Test
122145
void shouldAddManifests() {
123146
Index index = Index.fromManifests(List.of());

0 commit comments

Comments
 (0)