Skip to content

Commit

Permalink
tweak how DockerFile buildArgs are handled internally, and change out…
Browse files Browse the repository at this point in the history
…put to match C# output (as a map, not an array)
  • Loading branch information
JonathanGiles committed Jul 11, 2024
1 parent 0fb202b commit 13a85fd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.microsoft.aspire.resources.annotations.KeyValueAnnotation;
import com.microsoft.aspire.utils.json.RelativePath;
import com.microsoft.aspire.utils.json.RelativePathSerializer;
import com.microsoft.aspire.resources.traits.ResourceWithEndpoints;
Expand Down Expand Up @@ -64,9 +65,6 @@ public class DockerFile<T extends DockerFile<T>> extends Resource<T>
@RelativePath
private String context;

@JsonProperty("buildArgs")
private List<String[]> buildArgs;

public DockerFile(String name) {
this(name, null, null);
}
Expand Down Expand Up @@ -108,10 +106,7 @@ public T withContext(String context) {
}

public T withBuildArg(String key, String value) {
if (buildArgs == null) {
buildArgs = new ArrayList<>();
}
buildArgs.add(new String[] { key, value });
withAnnotation(new KeyValueAnnotation("DockerFile_buildArg", key, value));
return self();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.microsoft.aspire.resources.annotations;

import java.util.Objects;

public class KeyValueAnnotation implements ResourceAnnotation {
private final String type;
private final String key;
private final Object value;

public KeyValueAnnotation(String type, String key, Object value) {
this.type = Objects.requireNonNull(type, "Type cannot be null");
this.key = Objects.requireNonNull(key, "Key cannot be null");
this.value = Objects.requireNonNull(value, "Value cannot be null");
}

public String getType() {
return type;
}

public String getKey() {
return key;
}

public Object getValue() {
return value;
}

@Override
public String toString() {
return "KeyValueAnnotation{" +
"type='" + type + '\'' +
", key='" + key + '\'' +
", value='" + value + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.microsoft.aspire.implementation.ResourceUtilities;
import com.microsoft.aspire.resources.DockerFile;
import com.microsoft.aspire.resources.Resource;
import com.microsoft.aspire.resources.annotations.EndpointAnnotation;
import com.microsoft.aspire.resources.annotations.EnvironmentCallbackAnnotation;
import com.microsoft.aspire.resources.annotations.KeyValueAnnotation;
import com.microsoft.aspire.resources.properties.*;
import com.microsoft.aspire.resources.references.ReferenceExpression;
import com.microsoft.aspire.resources.traits.ManifestExpressionProvider;
Expand Down Expand Up @@ -44,6 +46,10 @@ public void serialize(Resource<?> resource, JsonGenerator gen, SerializerProvide
writeEnvironmentVariables(resource, gen);
writeBindings(resource, gen);

if (resource instanceof DockerFile<?> dockerResource) {
writeBuildArgs(dockerResource, gen);
}

gen.writeEndObject();
}

Expand Down Expand Up @@ -108,6 +114,26 @@ private void writeEnvironmentVariables(Resource<?> resource, JsonGenerator gen)
}
}

private void writeBuildArgs(DockerFile<?> dockerResource, JsonGenerator gen) throws IOException {
writeObjectField("buildArgs",
collectKeyValueAnnotations(dockerResource, "DockerFile_buildArg"),
gen);
}

private Map<String, Object> collectKeyValueAnnotations(Resource<?> resource, String type) {
return resource.getAnnotations().stream()
.filter(a -> a instanceof KeyValueAnnotation keyValueAnnotation && type.equals(keyValueAnnotation.getType()))
.map(a -> (KeyValueAnnotation) a)
.collect(Collectors.toMap(KeyValueAnnotation::getKey, KeyValueAnnotation::getValue));
}

private void writeObjectField(String name, Map<?,?> map, JsonGenerator gen) throws IOException {
Objects.requireNonNull(map);
if (!map.isEmpty()) {
gen.writeObjectField(name, map);
}
}

private void tryAddDependentResources(Object value) {
if (value instanceof Resource<?> resource) {
referencedResources.put(resource.getName(), resource);
Expand Down

0 comments on commit 13a85fd

Please sign in to comment.