Skip to content

Commit 84678d9

Browse files
authored
Remove ListOfJsonTemplate class (#1023)
* Remove ListOfJsonTemplate class * ManifestTemplate --> ManifestEntryTemplate
1 parent 8b7b11a commit 84678d9

10 files changed

+93
-90
lines changed

jib-core/src/main/java/com/google/cloud/tools/jib/docker/ImageToTarballTranslator.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import com.google.cloud.tools.jib.blob.Blob;
2020
import com.google.cloud.tools.jib.blob.Blobs;
2121
import com.google.cloud.tools.jib.cache.CachedLayer;
22-
import com.google.cloud.tools.jib.docker.json.DockerLoadManifestTemplate;
22+
import com.google.cloud.tools.jib.docker.json.DockerLoadManifestEntryTemplate;
2323
import com.google.cloud.tools.jib.image.Image;
2424
import com.google.cloud.tools.jib.image.ImageReference;
2525
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
2626
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
2727
import com.google.cloud.tools.jib.tar.TarStreamBuilder;
2828
import java.io.IOException;
2929
import java.nio.file.Path;
30+
import java.util.Collections;
3031
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
3132

3233
/** Translates an {@link Image} to a tarball that can be loaded into Docker. */
@@ -50,7 +51,7 @@ public ImageToTarballTranslator(Image<CachedLayer> image) {
5051

5152
public Blob toTarballBlob(ImageReference imageReference) throws IOException {
5253
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
53-
DockerLoadManifestTemplate manifestTemplate = new DockerLoadManifestTemplate();
54+
DockerLoadManifestEntryTemplate manifestTemplate = new DockerLoadManifestEntryTemplate();
5455

5556
// Adds all the layers to the tarball and manifest.
5657
for (CachedLayer layer : image.getLayers()) {
@@ -71,7 +72,8 @@ public Blob toTarballBlob(ImageReference imageReference) throws IOException {
7172
// Adds the manifest to tarball.
7273
manifestTemplate.setRepoTags(imageReference.toStringWithTag());
7374
tarStreamBuilder.addByteEntry(
74-
Blobs.writeToByteArray(JsonTemplateMapper.toBlob(manifestTemplate)),
75+
Blobs.writeToByteArray(
76+
JsonTemplateMapper.toBlob(Collections.singletonList(manifestTemplate))),
7577
MANIFEST_JSON_FILE_NAME);
7678

7779
return tarStreamBuilder.toBlob();

jib-core/src/main/java/com/google/cloud/tools/jib/docker/json/DockerLoadManifestTemplate.java jib-core/src/main/java/com/google/cloud/tools/jib/docker/json/DockerLoadManifestEntryTemplate.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,35 @@
1717
package com.google.cloud.tools.jib.docker.json;
1818

1919
import com.google.cloud.tools.jib.json.JsonTemplate;
20-
import com.google.cloud.tools.jib.json.ListOfJsonTemplate;
2120
import java.util.ArrayList;
2221
import java.util.Collections;
2322
import java.util.List;
2423

2524
/**
26-
* JSON Template for loadable Docker Manifest. The repoTags property requires a tag; i.e. if a tag
27-
* is missing, it explicitly should use "latest".
25+
* JSON Template for a loadable Docker Manifest entry. The repoTags property requires a tag; i.e. if
26+
* a tag is missing, it explicitly should use "latest".
2827
*
29-
* <p>Example manifest JSON:
28+
* <p>Note that this is a template for a single Manifest entry, while the entire Docker Manifest
29+
* should be {@code List<DockerLoadManifestEntryTemplate>}.
30+
*
31+
* <p>Example manifest entry JSON:
3032
*
3133
* <pre>{@code
32-
* [{
34+
* {
3335
* "Config":"config.json",
3436
* "RepoTags":["repository:tag"]
3537
* "Layers": [
3638
* "eb05f3dbdb543cc610527248690575bacbbcebabe6ecf665b189cf18b541e3ca.tar.gz",
3739
* "ba7c544469e514f1a9a4dec59ab640540d50992b288adbb34a1a63c45bf19a24.tar.gz",
3840
* "15705ab016593987662839b40f5a22fd1032996c90808d4a1371eb46974017d5.tar.gz"
3941
* ]
40-
* }]
42+
* }
4143
* }</pre>
4244
*
4345
* @see <a href="https://github.com/moby/moby/blob/master/image/tarexport/load.go">Docker load
4446
* source</a>
4547
*/
46-
public class DockerLoadManifestTemplate implements ListOfJsonTemplate {
48+
public class DockerLoadManifestEntryTemplate implements JsonTemplate {
4749

4850
private final String config = "config.json";
4951
private List<String> repoTags = Collections.singletonList(null);
@@ -56,9 +58,4 @@ public void setRepoTags(String repoTags) {
5658
public void addLayerFile(String layer) {
5759
layers.add(layer);
5860
}
59-
60-
@Override
61-
public List<JsonTemplate> getList() {
62-
return Collections.singletonList(this);
63-
}
6461
}

jib-core/src/main/java/com/google/cloud/tools/jib/json/JsonTemplateMapper.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.tools.jib.json;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.type.CollectionType;
2021
import com.google.cloud.tools.jib.blob.Blob;
2122
import com.google.cloud.tools.jib.blob.Blobs;
2223
import java.io.IOException;
@@ -103,17 +104,17 @@ public static <T extends JsonTemplate> T readJson(String jsonString, Class<T> te
103104
/**
104105
* Deserializes a JSON object list from a JSON string.
105106
*
106-
* @param <T> child type of {@link ListOfJsonTemplate}
107+
* @param <T> child type of {@link JsonTemplate}
107108
* @param jsonString a JSON string
108109
* @param templateClass the template to deserialize the string to
109-
* @return the template filled with the values parsed from {@code jsonFile}
110+
* @return the template filled with the values parsed from {@code jsonString}
110111
* @throws IOException if an error occurred during parsing the JSON
111112
*/
112-
public static <T extends ListOfJsonTemplate<? extends JsonTemplate>> List<T> readListOfJson(
113+
public static <T extends JsonTemplate> List<T> readListOfJson(
113114
String jsonString, Class<T> templateClass) throws IOException {
114-
return objectMapper.readValue(
115-
jsonString,
116-
objectMapper.getTypeFactory().constructCollectionType(List.class, templateClass));
115+
CollectionType listType =
116+
objectMapper.getTypeFactory().constructCollectionType(List.class, templateClass);
117+
return objectMapper.readValue(jsonString, listType);
117118
}
118119

119120
/**
@@ -127,13 +128,13 @@ public static Blob toBlob(JsonTemplate template) {
127128
}
128129

129130
/**
130-
* Convert a {@link ListOfJsonTemplate} to a {@link Blob} of the JSON string.
131+
* Convert a list of {@link JsonTemplate} to a {@link Blob} of the JSON string.
131132
*
132-
* @param template the list of JSON templates to convert
133+
* @param templates the list of JSON templates to convert
133134
* @return a {@link Blob} of the JSON string
134135
*/
135-
public static Blob toBlob(ListOfJsonTemplate template) {
136-
return toBlob(template.getList());
136+
public static Blob toBlob(List<? extends JsonTemplate> templates) {
137+
return toBlob((Object) templates);
137138
}
138139

139140
private static Blob toBlob(Object template) {

jib-core/src/main/java/com/google/cloud/tools/jib/json/ListOfJsonTemplate.java

-31
This file was deleted.

jib-core/src/main/java/com/google/cloud/tools/jib/ncache/LayerEntriesSelector.java

+14-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.tools.jib.image.LayerEntry;
2121
import com.google.cloud.tools.jib.json.JsonTemplate;
2222
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
23-
import com.google.cloud.tools.jib.json.ListOfJsonTemplate;
2423
import com.google.common.annotations.VisibleForTesting;
2524
import com.google.common.collect.ImmutableList;
2625
import com.google.common.io.ByteStreams;
@@ -92,30 +91,21 @@ public int hashCode() {
9291
}
9392
}
9493

95-
/** Serialized form of a list of {@link LayerEntry}s. */
94+
/**
95+
* Converts a list of {@link LayerEntry}s into a list of {@link LayerEntriesTemplate}. The list is
96+
* sorted by source file first, then extraction path (see {@link LayerEntryTemplate#compareTo}).
97+
*
98+
* @param layerEntries
99+
* @return list of {@link LayerEntryTemplate} after sorting
100+
*/
96101
@VisibleForTesting
97-
static class LayerEntriesTemplate implements ListOfJsonTemplate<LayerEntryTemplate> {
98-
99-
private final List<LayerEntryTemplate> layerEntryTemplates = new ArrayList<>();
100-
101-
@Override
102-
public List<LayerEntryTemplate> getList() {
103-
return layerEntryTemplates;
104-
}
105-
106-
/**
107-
* Serializes a list of {@link LayerEntry}s into a new {@link LayerEntriesTemplate}. The list is
108-
* sorted by source file first, then extraction path (see {@link LayerEntryTemplate#compareTo}).
109-
*
110-
* @param layerEntries the list of {@link LayerEntry}s
111-
*/
112-
@VisibleForTesting
113-
LayerEntriesTemplate(ImmutableList<LayerEntry> layerEntries) {
114-
for (LayerEntry layerEntry : layerEntries) {
115-
layerEntryTemplates.add(new LayerEntryTemplate(layerEntry));
116-
}
117-
Collections.sort(layerEntryTemplates);
102+
static List<LayerEntryTemplate> toSortedJsonTemplates(List<LayerEntry> layerEntries) {
103+
List<LayerEntryTemplate> jsonTemplates = new ArrayList<>();
104+
for (LayerEntry entry : layerEntries) {
105+
jsonTemplates.add(new LayerEntryTemplate(entry));
118106
}
107+
Collections.sort(jsonTemplates);
108+
return jsonTemplates;
119109
}
120110

121111
/**
@@ -128,7 +118,7 @@ public List<LayerEntryTemplate> getList() {
128118
*/
129119
static DescriptorDigest generateSelector(ImmutableList<LayerEntry> layerEntries)
130120
throws IOException {
131-
return JsonTemplateMapper.toBlob(new LayerEntriesTemplate(layerEntries))
121+
return JsonTemplateMapper.toBlob(toSortedJsonTemplates(layerEntries))
132122
.writeTo(ByteStreams.nullOutputStream())
133123
.getDigest();
134124
}

jib-core/src/test/java/com/google/cloud/tools/jib/docker/ImageToTarballTranslatorTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.cloud.tools.jib.blob.BlobDescriptor;
2121
import com.google.cloud.tools.jib.blob.Blobs;
2222
import com.google.cloud.tools.jib.cache.CachedLayer;
23-
import com.google.cloud.tools.jib.docker.json.DockerLoadManifestTemplate;
23+
import com.google.cloud.tools.jib.docker.json.DockerLoadManifestEntryTemplate;
2424
import com.google.cloud.tools.jib.image.DescriptorDigest;
2525
import com.google.cloud.tools.jib.image.Image;
2626
import com.google.cloud.tools.jib.image.ImageReference;
@@ -103,7 +103,7 @@ public void testToTarballBlob()
103103
String manifestJson =
104104
CharStreams.toString(
105105
new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
106-
JsonTemplateMapper.readListOfJson(manifestJson, DockerLoadManifestTemplate.class);
106+
JsonTemplateMapper.readListOfJson(manifestJson, DockerLoadManifestEntryTemplate.class);
107107
}
108108
}
109109
}

jib-core/src/test/java/com/google/cloud/tools/jib/docker/json/DockerLoadManifestTemplateTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
2828
import java.nio.file.Paths;
29+
import java.util.Collections;
30+
import java.util.List;
2931
import org.junit.Assert;
3032
import org.junit.Test;
3133

32-
/** Tests for {@link DockerLoadManifestTemplate}. */
34+
/** Tests for {@link DockerLoadManifestEntryTemplate}. */
3335
public class DockerLoadManifestTemplateTest {
3436

3537
@Test
@@ -38,13 +40,14 @@ public void testToJson() throws URISyntaxException, IOException {
3840
Path jsonFile = Paths.get(Resources.getResource("json/loadmanifest.json").toURI());
3941
String expectedJson = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
4042

41-
DockerLoadManifestTemplate template = new DockerLoadManifestTemplate();
43+
DockerLoadManifestEntryTemplate template = new DockerLoadManifestEntryTemplate();
4244
template.setRepoTags(
4345
ImageReference.of("testregistry", "testrepo", "testtag").toStringWithTag());
4446
template.addLayerFile("layer1.tar.gz");
4547
template.addLayerFile("layer2.tar.gz");
4648
template.addLayerFile("layer3.tar.gz");
4749

48-
Assert.assertEquals(expectedJson, Blobs.writeToString(JsonTemplateMapper.toBlob(template)));
50+
List<DockerLoadManifestEntryTemplate> loadManifest = Collections.singletonList(template);
51+
Assert.assertEquals(expectedJson, Blobs.writeToString(JsonTemplateMapper.toBlob(loadManifest)));
4952
}
5053
}

jib-core/src/test/java/com/google/cloud/tools/jib/json/JsonTemplateMapperTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,45 @@ public void testReadJsonWithLock() throws IOException, URISyntaxException, Diges
119119
"4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236"))));
120120
// ignore testJson.list
121121
}
122+
123+
@Test
124+
public void testReadListOfJson() throws IOException, URISyntaxException, DigestException {
125+
Path jsonFile = Paths.get(Resources.getResource("json/basic_list.json").toURI());
126+
127+
String jsonString = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
128+
List<TestJson> listofJsons = JsonTemplateMapper.readListOfJson(jsonString, TestJson.class);
129+
TestJson json1 = listofJsons.get(0);
130+
TestJson json2 = listofJsons.get(1);
131+
132+
DescriptorDigest digest1 =
133+
DescriptorDigest.fromDigest(
134+
"sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10");
135+
DescriptorDigest digest2 =
136+
DescriptorDigest.fromDigest(
137+
"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");
138+
139+
Assert.assertEquals(1, json1.number);
140+
Assert.assertEquals(2, json2.number);
141+
Assert.assertEquals("text1", json1.text);
142+
Assert.assertEquals("text2", json2.text);
143+
Assert.assertEquals(digest1, json1.digest);
144+
Assert.assertEquals(digest2, json2.digest);
145+
Assert.assertEquals(10, json1.innerObject.number);
146+
Assert.assertEquals(20, json2.innerObject.number);
147+
Assert.assertEquals(2, json1.list.size());
148+
Assert.assertTrue(json2.list.isEmpty());
149+
}
150+
151+
@Test
152+
public void testToBlob_listOfJson() throws IOException, URISyntaxException, DigestException {
153+
Path jsonFile = Paths.get(Resources.getResource("json/basic_list.json").toURI());
154+
155+
String jsonString = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
156+
List<TestJson> listOfJson = JsonTemplateMapper.readListOfJson(jsonString, TestJson.class);
157+
158+
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
159+
JsonTemplateMapper.toBlob(listOfJson).writeTo(jsonStream);
160+
161+
Assert.assertEquals(jsonString, jsonStream.toString());
162+
}
122163
}

jib-core/src/test/java/com/google/cloud/tools/jib/ncache/LayerEntriesSelectorTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.tools.jib.image.DescriptorDigest;
2121
import com.google.cloud.tools.jib.image.LayerEntry;
2222
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
23-
import com.google.cloud.tools.jib.ncache.LayerEntriesSelector.LayerEntriesTemplate;
2423
import com.google.cloud.tools.jib.ncache.LayerEntriesSelector.LayerEntryTemplate;
2524
import com.google.common.collect.ImmutableList;
2625
import com.google.common.io.ByteStreams;
@@ -64,16 +63,16 @@ public void testLayerEntryTemplate_compareTo() {
6463
}
6564

6665
@Test
67-
public void testLayerEntriesTemplate_getList() {
66+
public void testToSortedJsonTemplates() {
6867
Assert.assertEquals(
6968
toLayerEntryTemplates(IN_ORDER_LAYER_ENTRIES),
70-
new LayerEntriesTemplate(OUT_OF_ORDER_LAYER_ENTRIES).getList());
69+
LayerEntriesSelector.toSortedJsonTemplates(OUT_OF_ORDER_LAYER_ENTRIES));
7170
}
7271

7372
@Test
7473
public void testGenerateSelector_empty() throws IOException {
7574
DescriptorDigest expectedSelector =
76-
JsonTemplateMapper.toBlob(new LayerEntriesTemplate(ImmutableList.of()))
75+
JsonTemplateMapper.toBlob(ImmutableList.of())
7776
.writeTo(ByteStreams.nullOutputStream())
7877
.getDigest();
7978
Assert.assertEquals(
@@ -83,7 +82,7 @@ public void testGenerateSelector_empty() throws IOException {
8382
@Test
8483
public void testGenerateSelector() throws IOException {
8584
DescriptorDigest expectedSelector =
86-
JsonTemplateMapper.toBlob(new LayerEntriesTemplate(IN_ORDER_LAYER_ENTRIES))
85+
JsonTemplateMapper.toBlob(toLayerEntryTemplates(IN_ORDER_LAYER_ENTRIES))
8786
.writeTo(ByteStreams.nullOutputStream())
8887
.getDigest();
8988
Assert.assertEquals(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"number":1,"text":"text1","digest":"sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10","innerObject":{"number":10},"list":[{"number":11},{"number":12}]},{"number":2,"text":"text2","digest":"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad","innerObject":{"number":20},"list":[]}]

0 commit comments

Comments
 (0)