Skip to content

Commit e56e3ed

Browse files
authored
Better support for Index artifact type (#594)
Signed-off-by: Valentin Delaye <jonesbusy@users.noreply.github.com>
1 parent 9ddb42c commit e56e3ed

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

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

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package land.oras;
2222

2323
import com.fasterxml.jackson.annotation.JsonCreator;
24+
import com.fasterxml.jackson.annotation.JsonIgnore;
2425
import com.fasterxml.jackson.annotation.JsonInclude;
2526
import com.fasterxml.jackson.annotation.JsonProperty;
2627
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@@ -32,6 +33,7 @@
3233
import java.util.Objects;
3334
import land.oras.utils.Const;
3435
import land.oras.utils.JsonUtils;
36+
import org.jspecify.annotations.NonNull;
3537
import org.jspecify.annotations.Nullable;
3638

3739
/**
@@ -70,7 +72,42 @@ private Index(
7072
@JsonProperty(Const.JSON_PROPERTY_MANIFESTS) List<ManifestDescriptor> manifests,
7173
@JsonProperty(Const.JSON_PROPERTY_ANNOTATIONS) Map<String, String> annotations,
7274
@JsonProperty(Const.JSON_PROPERTY_SUBJECT) Subject subject) {
73-
this(schemaVersion, mediaType, artifactType, manifests, annotations, subject, null, null, null);
75+
super(
76+
null,
77+
null,
78+
mediaType,
79+
annotations != null && !annotations.isEmpty() ? Map.copyOf(annotations) : null,
80+
artifactType,
81+
null,
82+
null);
83+
this.schemaVersion = schemaVersion;
84+
this.manifests = manifests;
85+
this.descriptor = null;
86+
this.subject = subject;
87+
}
88+
89+
private Index(
90+
int schemaVersion,
91+
String mediaType,
92+
ArtifactType artifactType,
93+
List<ManifestDescriptor> manifests,
94+
Map<String, String> annotations,
95+
Subject subject,
96+
ManifestDescriptor descriptor,
97+
String registry,
98+
String json) {
99+
super(
100+
null,
101+
null,
102+
mediaType,
103+
annotations,
104+
artifactType != null ? artifactType.getMediaType() : null,
105+
registry,
106+
json);
107+
this.schemaVersion = schemaVersion;
108+
this.descriptor = descriptor;
109+
this.subject = subject;
110+
this.manifests = manifests;
74111
}
75112

76113
private Index(
@@ -139,6 +176,15 @@ public List<ManifestDescriptor> unspecifiedPlatforms() {
139176
.orElse(null);
140177
}
141178

179+
@Override
180+
@JsonIgnore
181+
public @NonNull ArtifactType getArtifactType() {
182+
if (artifactType != null) {
183+
return ArtifactType.from(artifactType);
184+
}
185+
return ArtifactType.unknown();
186+
}
187+
142188
/**
143189
* Get the artifact type as string for JSON serialization
144190
* @return The artifact type as string
@@ -185,7 +231,15 @@ public Index withNewManifests(ManifestDescriptor manifest) {
185231
}
186232
newManifests.add(manifest);
187233
return new Index(
188-
schemaVersion, mediaType, artifactType, newManifests, annotations, subject, descriptor, registry, json);
234+
schemaVersion,
235+
mediaType,
236+
ArtifactType.from(artifactType),
237+
newManifests,
238+
annotations,
239+
subject,
240+
descriptor,
241+
registry,
242+
json);
189243
}
190244

191245
@Override
@@ -210,7 +264,7 @@ public ManifestDescriptor getDescriptor() {
210264
* @param artifactType The artifact type
211265
* @return The index
212266
*/
213-
public Index withArtifactType(String artifactType) {
267+
public Index withArtifactType(ArtifactType artifactType) {
214268
return new Index(
215269
schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json);
216270
}
@@ -274,7 +328,8 @@ public static Index fromPath(Path path) {
274328
* @return The index
275329
*/
276330
public static Index fromManifests(List<ManifestDescriptor> descriptors) {
277-
return new Index(2, Const.DEFAULT_INDEX_MEDIA_TYPE, null, descriptors, null, null, null, null, null);
331+
return new Index(
332+
2, Const.DEFAULT_INDEX_MEDIA_TYPE, (ArtifactType) null, descriptors, null, null, null, null, null);
278333
}
279334

280335
@Override

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void shouldReadAndWriteIndex() {
4141
assertNull(index.getMediaType());
4242
assertEquals(2, index.getSchemaVersion());
4343
assertEquals(1, index.getManifests().size());
44-
assertNull(index.getArtifactType());
44+
assertNull(index.getArtifactTypeAsString());
45+
assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType());
4546
assertNull(index.getAnnotations());
4647
assertNull(index.getDescriptor());
4748
assertEquals(
@@ -58,7 +59,7 @@ void shouldReadAndWriteIndex() {
5859
@Test
5960
void shouldAddArtifactType() {
6061
Index index = Index.fromManifests(List.of());
61-
index = index.withArtifactType("application/vnd.opentofu.provider");
62+
index = index.withArtifactType(ArtifactType.from("application/vnd.opentofu.provider"));
6263
assertNotNull(index.getArtifactType());
6364
assertEquals(
6465
"application/vnd.opentofu.provider", index.getArtifactType().getMediaType());
@@ -75,7 +76,8 @@ void shouldReadAndWriteIndexWithAnnotations() {
7576
assertNull(index.getMediaType());
7677
assertEquals(2, index.getSchemaVersion());
7778
assertEquals(1, index.getManifests().size());
78-
assertNull(index.getArtifactType());
79+
assertNull(index.getArtifactTypeAsString());
80+
assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType());
7981
assertNull(index.getAnnotations());
8082
assertNull(index.getDescriptor());
8183
assertEquals(

0 commit comments

Comments
 (0)