All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.helm.deployment.utils.YamlExpressionParserUtils Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package io.quarkiverse.helm.deployment.utils;

import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import io.dekorate.ConfigReference;
import io.github.yamlpath.YamlExpressionParser;

public final class YamlExpressionParserUtils {

    public static final String SEPARATOR_TOKEN = ":LINE_SEPARATOR:";
    public static final String SEPARATOR_QUOTES = ":DOUBLE_QUOTES";
    public static final String START_EXPRESSION_TOKEN = ":START:";
    public static final String END_EXPRESSION_TOKEN = ":END:";
    public static final String START_TAG = "{{";
    public static final String END_TAG = "}}";
    public static final String EMPTY = "";
    public static final String VALUES_START_TAG = START_TAG + " .Values.";
    public static final String VALUES_END_TAG = " " + END_TAG;

    private YamlExpressionParserUtils() {

    }

    public static void set(YamlExpressionParser parser, String path, String expression) {
        parser.write(path, adaptExpression(expression));
    }

    public static Object read(YamlExpressionParser parser, String path) {
        Set found = parser.read(path);
        return found.stream().findFirst().orElse(null);
    }

    public static Object readAndSet(YamlExpressionParser parser, String path, String expression) {
        Set found = parser.readAndReplace(path, adaptExpression(expression));
        return found.stream().findFirst().orElse(null);
    }

    public static String toExpression(String property, Object provided, Object found, ConfigReference valueReference) {
        Optional expressionProvided = Optional.ofNullable(valueReference.getExpression())
                .filter(StringUtils::isNotEmpty);

        if (expressionProvided.isPresent()) {
            return expressionProvided.get();
        }

        String conversion = EMPTY;
        // we only need to quote when the found value in the generated resources is a string, but the provided type isn't.
        if (provided != null && !(provided instanceof String) && found instanceof String) {
            // we need conversion
            conversion = " | quote";
        }

        return VALUES_START_TAG + property + conversion + VALUES_END_TAG;
    }

    private static String adaptExpression(String expression) {
        return START_EXPRESSION_TOKEN +
                expression.replaceAll(Pattern.quote(System.lineSeparator()), SEPARATOR_TOKEN)
                        .replaceAll(Pattern.quote("\""), SEPARATOR_QUOTES)
                + END_EXPRESSION_TOKEN;
    }
}