Skip to content

Commit

Permalink
Improve how arguments are handled internally
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGiles committed Jul 12, 2024
1 parent 5059415 commit 9365f61
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public class Project<T extends Project<T>> extends Resource<T>
@RelativePath
private String path;

@JsonProperty("args")
@Valid
private final List<String> arguments = new ArrayList<>();

public Project(String name) {
this(ResourceType.PROJECT, name);
}
Expand All @@ -87,24 +83,11 @@ public T withPath(String path) {
return self();
}

@Override
@JsonIgnore
public T withArgument(String argument) {
arguments.add(argument);
return self();
}

@JsonIgnore
public final String getPath() {
return path;
}

@Override
@JsonIgnore
public final List<String> getArguments() {
return Collections.unmodifiableList(arguments);
}

@Override
public T self() {
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.microsoft.aspire.implementation.ResourceUtilities;
import com.microsoft.aspire.resources.DockerFile;
import com.microsoft.aspire.resources.Resource;
import com.microsoft.aspire.resources.annotations.ArgsAnnotation;
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;
import com.microsoft.aspire.resources.traits.ResourceWithArguments;
import com.microsoft.aspire.resources.traits.ResourceWithConnectionString;
import com.microsoft.aspire.resources.traits.ValueWithReferences;

Expand Down Expand Up @@ -47,7 +49,11 @@ public void serialize(Resource<?> resource, JsonGenerator gen, SerializerProvide
writeBindings(resource, gen);

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

if (resource instanceof ResourceWithArguments<?>) {
writeObjectField("args", collectValueAnnotations(resource, "args"), gen);
}

gen.writeEndObject();
Expand Down Expand Up @@ -114,10 +120,11 @@ private void writeEnvironmentVariables(Resource<?> resource, JsonGenerator gen)
}
}

private void writeBuildArgs(DockerFile<?> dockerResource, JsonGenerator gen) throws IOException {
writeObjectField("buildArgs",
collectKeyValueAnnotations(dockerResource, "DockerFile_buildArg"),
gen);
private List<Object> collectValueAnnotations(Resource<?> resource, String type) {
return resource.getAnnotations().stream()
.filter(a -> a instanceof ArgsAnnotation argsAnnotation && type.equals(argsAnnotation.getType()))
.map(a -> ((ArgsAnnotation) a).getArgs().stream())
.collect(Collectors.toList());
}

private Map<String, Object> collectKeyValueAnnotations(Resource<?> resource, String type) {
Expand All @@ -134,6 +141,13 @@ private void writeObjectField(String name, Map<?,?> map, JsonGenerator gen) thro
}
}

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

private void tryAddDependentResources(Object value) {
if (value instanceof Resource<?> resource) {
referencedResources.put(resource.getName(), resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public class Container<T extends Container<T>> extends Resource<T>
@JsonProperty("entrypoint")
private String entryPoint;

@JsonProperty("args")
@Valid
private final List<String> arguments = new ArrayList<>();

@JsonProperty("volumes")
@Valid
private final List<Volume> volumes = new ArrayList<>();
Expand Down Expand Up @@ -136,19 +132,6 @@ public T withEntryPoint(String entryPoint) {
return self();
}

@Override
@JsonIgnore
public T withArgument(String argument) {
arguments.add(argument);
return self();
}

@Override
@JsonIgnore
public List<String> getArguments() {
return Collections.unmodifiableList(arguments);
}

@JsonIgnore
public T withVolume(Volume volume) {
volumes.add(volume);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public T withContext(String context) {
}

public T withBuildArg(String key, String value) {
withAnnotation(new KeyValueAnnotation("DockerFile_buildArg", key, value));
withAnnotation(new KeyValueAnnotation("buildArgs", key, value));
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public class Executable<T extends Executable<T>> extends Resource<T>
@NotEmpty(message = "Executable.command cannot be an empty string")
private String command;

@JsonProperty("args")
@Valid
private final List<String> arguments = new ArrayList<>();

public Executable(String name) {
this(name, null, null);
}
Expand All @@ -92,19 +88,6 @@ public T withCommand(String command) {
return self();
}

@Override
@JsonIgnore
public T withArgument(String argument) {
arguments.add(argument);
return self();
}

@Override
@JsonIgnore
public List<String> getArguments() {
return Collections.unmodifiableList(arguments);
}

@Override
public T self() {
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.microsoft.aspire.resources.annotations;

import java.util.List;
import java.util.Objects;

public class ArgsAnnotation implements ResourceAnnotation {
private final String type;
private final List<Object> args;

public static ArgsAnnotation createArgs(Object value) {
return createArgs(List.of(value));
}

public static ArgsAnnotation createArgs(List<Object> values) {
return new ArgsAnnotation("args", values);
}

private ArgsAnnotation(String type, List<Object> values) {
this.type = Objects.requireNonNull(type, "Type cannot be null");
Objects.requireNonNull(values, "Value cannot be null");
this.args = values;
}

public String getType() {
return type;
}

public List<Object> getArgs() {
return args;
}

@Override
public String toString() {
return "ArgsAnnotation{" +
"type='" + type + '\'' +
", args='" + args.toString() + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.microsoft.aspire.resources.traits;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.microsoft.aspire.implementation.ResourceUtilities;
import com.microsoft.aspire.resources.Resource;
import com.microsoft.aspire.resources.annotations.ArgsAnnotation;

import java.util.List;

// TODO update with CommandLineArgsCallbackAnnotation support
public interface ResourceWithArguments<T extends Resource<T> & ResourceWithArguments<T>> extends SelfAware<T> {

T withArgument(String argument);

/**
* Returns an unmodifiable list of arguments.
*/
@JsonIgnore
List<String> getArguments();
default T withArgument(String argument) {
ResourceUtilities.applyAnnotation(self(), ArgsAnnotation.createArgs(argument));
return self();
}

default T withArguments(String... arguments) {
for (String argument : arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
public interface ResourceWithConnectionString<T extends Resource<T> & ResourceWithConnectionString<T>>
extends ManifestExpressionProvider, ValueProvider, ValueWithReferences {

// T withConnectionString(String connectionString);


@Override
default String getValue() {
return getConnectionString();
Expand Down

0 comments on commit 9365f61

Please sign in to comment.