|
| 1 | +package land.oras; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import land.oras.utils.JsonUtils; |
| 7 | +import org.jspecify.annotations.NullMarked; |
| 8 | + |
| 9 | +/** |
| 10 | + * Record for annotations |
| 11 | + * |
| 12 | + * @param manifestAnnotations Annotations for the manifest |
| 13 | + * @param configAnnotations Annotations for the config |
| 14 | + * @param filesAnnotations Annotations for the layers/files |
| 15 | + */ |
| 16 | +@NullMarked |
| 17 | +public record Annotations( |
| 18 | + Map<String, String> configAnnotations, |
| 19 | + Map<String, String> manifestAnnotations, |
| 20 | + Map<String, Map<String, String>> filesAnnotations) { |
| 21 | + |
| 22 | + /** |
| 23 | + * Create a new annotations record with only manifest annotations |
| 24 | + * @param manifestAnnotations The manifest annotations |
| 25 | + */ |
| 26 | + public static Annotations ofManifest(Map<String, String> manifestAnnotations) { |
| 27 | + return new Annotations(new HashMap<>(), manifestAnnotations, new HashMap<>()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a new annotations record with only config annotations |
| 32 | + * @param configAnnotations The config annotations |
| 33 | + * @return The annotations |
| 34 | + */ |
| 35 | + public static Annotations ofConfig(Map<String, String> configAnnotations) { |
| 36 | + return new Annotations(configAnnotations, new HashMap<>(), new HashMap<>()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Empty annotations |
| 41 | + * @return The empty annotations |
| 42 | + */ |
| 43 | + public static Annotations empty() { |
| 44 | + return new Annotations(new HashMap<>(), new HashMap<>(), new HashMap<>()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the manifest annotations for a file |
| 49 | + * @param key The key |
| 50 | + * @return The annotations |
| 51 | + */ |
| 52 | + public Map<String, String> getFileAnnotations(String key) { |
| 53 | + return this.filesAnnotations().getOrDefault(key, new HashMap<>()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Annotations file format |
| 58 | + */ |
| 59 | + private static class AnnotationFile extends HashMap<String, Map<String, String>> { |
| 60 | + /** |
| 61 | + * Get the manifest annotations |
| 62 | + * |
| 63 | + * @return The manifest annotations |
| 64 | + */ |
| 65 | + public Map<String, String> getManifestAnnotations() { |
| 66 | + return this.getOrDefault("$manifest", new HashMap<>()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the config annotations |
| 71 | + * |
| 72 | + * @return The config annotations |
| 73 | + */ |
| 74 | + public Map<String, String> getConfigAnnotations() { |
| 75 | + return this.getOrDefault("$config", new HashMap<>()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the files annotations without the manifest and config annotations |
| 80 | + * |
| 81 | + * @return The files annotations |
| 82 | + */ |
| 83 | + public Map<String, Map<String, String>> getFilesAnnotations() { |
| 84 | + return this.entrySet().stream() |
| 85 | + .filter(entry -> !"$manifest".equals(entry.getKey()) |
| 86 | + && !"$config".equals(entry.getKey())) // Filter out $manifest and $config |
| 87 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the annotations for a file |
| 92 | + * @param key The key |
| 93 | + * @return The annotations |
| 94 | + */ |
| 95 | + public Map<String, String> getFileAnnotations(String key) { |
| 96 | + return this.getOrDefault(key, new HashMap<>()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Convert the annotations from a JSON string |
| 102 | + * |
| 103 | + * @param json The JSON string |
| 104 | + * @return The annotations |
| 105 | + */ |
| 106 | + public static Annotations fromJson(String json) { |
| 107 | + AnnotationFile file = JsonUtils.fromJson(json, AnnotationFile.class); |
| 108 | + return new Annotations(file.getConfigAnnotations(), file.getManifestAnnotations(), file.getFilesAnnotations()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Convert the annotations to a JSON string |
| 113 | + * |
| 114 | + * @return The JSON string |
| 115 | + */ |
| 116 | + public String toJson() { |
| 117 | + AnnotationFile file = new AnnotationFile(); |
| 118 | + file.put("$manifest", manifestAnnotations()); |
| 119 | + file.put("$config", configAnnotations()); |
| 120 | + file.putAll(filesAnnotations()); |
| 121 | + return JsonUtils.toJson(file); |
| 122 | + } |
| 123 | +} |
0 commit comments