Search in sources :

Example 1 with YTPropertyAssignment

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();
}
Also used : YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) YTMapObject(org.eclipse.winery.model.tosca.yaml.support.YTMapObject) YTListString(org.eclipse.winery.model.tosca.yaml.support.YTListString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) YTPropertyAssignment(org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with YTPropertyAssignment

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;
}
Also used : AbstractParameter(org.eclipse.winery.model.tosca.yaml.visitor.AbstractParameter) YTMapObject(org.eclipse.winery.model.tosca.yaml.support.YTMapObject) List(java.util.List) ArrayList(java.util.ArrayList) YTListString(org.eclipse.winery.model.tosca.yaml.support.YTListString) Map(java.util.Map) YTPropertyAssignment(org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment)

Aggregations

Map (java.util.Map)2 YTPropertyAssignment (org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment)2 YTListString (org.eclipse.winery.model.tosca.yaml.support.YTListString)2 YTMapObject (org.eclipse.winery.model.tosca.yaml.support.YTMapObject)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 YTArtifactDefinition (org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition)1 AbstractParameter (org.eclipse.winery.model.tosca.yaml.visitor.AbstractParameter)1