use of org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment in project winery by eclipse.
the class YamlBuilder method buildArtifactDefinition.
@Nullable
public YTArtifactDefinition buildArtifactDefinition(Object object, Parameter<YTArtifactDefinition> parameter) {
if (Objects.isNull(object)) {
return null;
}
if (object instanceof String) {
String file = stringValue(object);
if (Objects.isNull(file)) {
return null;
}
// TODO infer artifact type and mime type from file URI
String type = file.substring(file.lastIndexOf("."), file.length());
return new YTArtifactDefinition.Builder(buildQName(type), file).build();
}
if (!validate(YTArtifactDefinition.class, object, parameter)) {
return null;
}
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) object;
String file;
if (map.get(YamlSpecKeywords.FILE) instanceof String) {
file = stringValue(map.get(YamlSpecKeywords.FILE));
} else {
file = null;
assert false;
}
return new YTArtifactDefinition.Builder(buildQName(stringValue(map.get(YamlSpecKeywords.TYPE))), file).setRepository(stringValue(map.get(YamlSpecKeywords.REPOSITORY))).setDescription(buildDescription(map.get(YamlSpecKeywords.DESCRIPTION))).setDeployPath(stringValue(map.get(YamlSpecKeywords.DEPLOY_PATH))).setProperties(buildMap(map.get(YamlSpecKeywords.PROPERTIES), new Parameter<YTPropertyAssignment>().addContext(YamlSpecKeywords.PROPERTIES).setBuilderOO(this::buildPropertyAssignment))).build();
}
use of org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment in project winery by eclipse.
the class YamlWriter method visit.
public YamlPrinter visit(YTPropertyAssignment node, Parameter parameter) {
// nested assignments are implemented by calling #printMap for Map values that are not property functions
YamlPrinter printer = new YamlPrinter(parameter.getIndent());
if (node.getValue() instanceof Map) {
@SuppressWarnings("unchecked") Map<String, YTPropertyAssignment> value = (Map<String, YTPropertyAssignment>) node.getValue();
// special casing for property functions to always be a single-line map value
if (value.size() == 1 && Arrays.stream(PROPERTY_FUNCTIONS).anyMatch(value::containsKey)) {
String key = value.keySet().iterator().next();
final Object rawFunctionArg = value.get(key).getValue();
final String functionArg;
if (rawFunctionArg instanceof List) {
final String list = ((List<?>) rawFunctionArg).stream().map(String::valueOf).collect(Collectors.joining(", "));
// get_operation_output value is not in brackets, compare Section 4.6.1 of YAML-Standard 1.3
if (key.equals("get_operation_output")) {
functionArg = list;
} else {
functionArg = "[ " + list + " ]";
}
} else if (rawFunctionArg instanceof YTPropertyAssignment) {
functionArg = visit((YTPropertyAssignment) rawFunctionArg, new Parameter(0)).toString();
} else if (rawFunctionArg instanceof String) {
functionArg = (String) rawFunctionArg;
} else {
// TODO
LOGGER.warn("Unexpected value type [{}] in property function definition", rawFunctionArg.getClass().getName());
functionArg = "";
}
printer.print(parameter.getKey()).print(":").print(" { ").print(key).print(": ").print(functionArg).print(" }").printNewLine();
} else if (value.isEmpty()) {
// printMap would skip an empty map
printer.printKeyValue(parameter.getKey(), "{}");
} else {
printer.print(printMap(parameter.getKey(), value, parameter));
}
} else if (node.getValue() instanceof List) {
if (((List<?>) node.getValue()).isEmpty()) {
// printList would skip an empty list
printer.printKeyValue(parameter.getKey(), "[]");
} else {
printer.print(printList(parameter.getKey(), (List<?>) node.getValue(), parameter));
}
} else {
// printKeyObject skips null and empty values, which is a reasonable default
// therefore we serialize null values ourselves here
final String value = Objects.toString(node.getValue());
final boolean isStringValue = node.getValue() instanceof String;
printer.printKeyValue(parameter.getKey(), value, isStringValue, false);
}
return printer;
}
Aggregations