Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions src/main/java/land/oras/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package land.oras;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
Expand All @@ -32,6 +33,7 @@
import java.util.Objects;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -70,7 +72,42 @@ private Index(
@JsonProperty(Const.JSON_PROPERTY_MANIFESTS) List<ManifestDescriptor> manifests,
@JsonProperty(Const.JSON_PROPERTY_ANNOTATIONS) Map<String, String> annotations,
@JsonProperty(Const.JSON_PROPERTY_SUBJECT) Subject subject) {
this(schemaVersion, mediaType, artifactType, manifests, annotations, subject, null, null, null);
super(
null,
null,
mediaType,
annotations != null && !annotations.isEmpty() ? Map.copyOf(annotations) : null,
artifactType,
null,
null);
this.schemaVersion = schemaVersion;
this.manifests = manifests;
this.descriptor = null;
this.subject = subject;
}

private Index(
int schemaVersion,
String mediaType,
ArtifactType artifactType,
List<ManifestDescriptor> manifests,
Map<String, String> annotations,
Subject subject,
ManifestDescriptor descriptor,
String registry,
String json) {
super(
null,
null,
mediaType,
annotations,
artifactType != null ? artifactType.getMediaType() : null,
registry,
json);
this.schemaVersion = schemaVersion;
this.descriptor = descriptor;
this.subject = subject;
this.manifests = manifests;
}

private Index(
Expand Down Expand Up @@ -139,6 +176,15 @@ public List<ManifestDescriptor> unspecifiedPlatforms() {
.orElse(null);
}

@Override
@JsonIgnore
public @NonNull ArtifactType getArtifactType() {
if (artifactType != null) {
return ArtifactType.from(artifactType);
}
return ArtifactType.unknown();
}

/**
* Get the artifact type as string for JSON serialization
* @return The artifact type as string
Expand Down Expand Up @@ -185,7 +231,15 @@ public Index withNewManifests(ManifestDescriptor manifest) {
}
newManifests.add(manifest);
return new Index(
schemaVersion, mediaType, artifactType, newManifests, annotations, subject, descriptor, registry, json);
schemaVersion,
mediaType,
ArtifactType.from(artifactType),
newManifests,
annotations,
subject,
descriptor,
registry,
json);
}

@Override
Expand All @@ -210,7 +264,7 @@ public ManifestDescriptor getDescriptor() {
* @param artifactType The artifact type
* @return The index
*/
public Index withArtifactType(String artifactType) {
public Index withArtifactType(ArtifactType artifactType) {
return new Index(
schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json);
}
Expand Down Expand Up @@ -274,7 +328,8 @@ public static Index fromPath(Path path) {
* @return The index
*/
public static Index fromManifests(List<ManifestDescriptor> descriptors) {
return new Index(2, Const.DEFAULT_INDEX_MEDIA_TYPE, null, descriptors, null, null, null, null, null);
return new Index(
2, Const.DEFAULT_INDEX_MEDIA_TYPE, (ArtifactType) null, descriptors, null, null, null, null, null);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/land/oras/IndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void shouldReadAndWriteIndex() {
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertNull(index.getArtifactType());
assertNull(index.getArtifactTypeAsString());
assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType());
assertNull(index.getAnnotations());
assertNull(index.getDescriptor());
assertEquals(
Expand All @@ -58,7 +59,7 @@ void shouldReadAndWriteIndex() {
@Test
void shouldAddArtifactType() {
Index index = Index.fromManifests(List.of());
index = index.withArtifactType("application/vnd.opentofu.provider");
index = index.withArtifactType(ArtifactType.from("application/vnd.opentofu.provider"));
assertNotNull(index.getArtifactType());
assertEquals(
"application/vnd.opentofu.provider", index.getArtifactType().getMediaType());
Expand All @@ -75,7 +76,8 @@ void shouldReadAndWriteIndexWithAnnotations() {
assertNull(index.getMediaType());
assertEquals(2, index.getSchemaVersion());
assertEquals(1, index.getManifests().size());
assertNull(index.getArtifactType());
assertNull(index.getArtifactTypeAsString());
assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType());
assertNull(index.getAnnotations());
assertNull(index.getDescriptor());
assertEquals(
Expand Down