Search in sources :

Example 1 with ValueParser

use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.

the class BoshValueParserTest method urlOkay.

@Test
public void urlOkay() throws Exception {
    ValueParser urlParser = BoshValueParsers.url("http", "https", "file");
    urlParser.parse("http://foobar.com/munhings.tar.gz");
    urlParser.parse("https://foobar.com/munhings.tar.gz");
    urlParser.parse("hTTp://foobar.com/munhings.tar.gz");
    urlParser.parse("HTTPS://foobar.com/munhings.tar.gz");
    urlParser.parse("file://local/file");
    urlParser.parse("file:///local/file");
    urlParser.parse("FILE:///local/file");
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) Test(org.junit.Test)

Example 2 with ValueParser

use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.

the class TypeUtil method getValueParser.

public ValueParser getValueParser(Type type) {
    ValueParser simpleParser = VALUE_PARSERS.get(type.getErasure());
    if (simpleParser != null) {
        return simpleParser;
    }
    Collection<StsValueHint> enumValues = getAllowedValues(type, EnumCaseMode.ALIASED);
    if (enumValues != null) {
        // assigning anything to it is an error.
        return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
    }
    if (isMap(type)) {
        // provide a parser that allows throws
        return new AlwaysFailingParser(niceTypeName(type));
    }
    if (isSequencable(type)) {
        // Trying to parse list from scalars is possible if the domain type is parseable. Spring boot
        // will try to interpret the string as a comma-separated list
        Type elType = getDomainType(type);
        if (elType != null) {
            ValueParser elParser = getValueParser(elType);
            if (elParser != null) {
                return new DelimitedStringParser(elParser);
            }
        }
    }
    return null;
}
Also used : IType(org.springframework.ide.vscode.commons.java.IType) EnumValueParser(org.springframework.ide.vscode.commons.util.EnumValueParser) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) EnumValueParser(org.springframework.ide.vscode.commons.util.EnumValueParser) AlwaysFailingParser(org.springframework.ide.vscode.commons.util.AlwaysFailingParser) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)

Example 3 with ValueParser

use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.

the class PropertyNavigator method dotNavigate.

/**
 * Handle dot navigation into given type, after a '.' was
 * was found at given offset. Assumes the type has already been
 * checked to be 'dotable'.
 */
private Type dotNavigate(int offset, Type type) {
    if (TypeUtil.isMap(type)) {
        int keyStart = offset + 1;
        Type domainType = TypeUtil.getDomainType(type);
        int keyEnd = -1;
        if (typeUtil.isDotable(domainType)) {
            // '.' should be interpreted as navigation.
            keyEnd = nextNavOp(".[", offset + 1);
        } else {
            // '.' should *not* be interpreted as navigation.
            keyEnd = nextNavOp("[", offset + 1);
        }
        String key = textBetween(keyStart, keyEnd);
        Type keyType = typeUtil.getKeyType(type);
        if (keyType != null) {
            ValueParser keyParser = typeUtil.getValueParser(keyType);
            if (keyParser != null) {
                try {
                    keyParser.parse(key);
                } catch (Exception e) {
                    problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, "Expecting " + typeUtil.niceTypeName(keyType), keyStart, keyEnd - keyStart));
                }
            }
        }
        return navigate(keyEnd, domainType);
    } else {
        // dot navigation into object properties
        int keyStart = offset + 1;
        int keyEnd = nextNavOp(".[", offset + 1);
        if (keyEnd < 0) {
            keyEnd = region.getEnd();
        }
        String key = StringUtil.camelCaseToHyphens(textBetween(keyStart, keyEnd));
        List<TypedProperty> properties = typeUtil.getProperties(type, EnumCaseMode.ALIASED, BeanPropertyNameMode.ALIASED);
        if (properties != null) {
            TypedProperty prop = null;
            for (TypedProperty p : properties) {
                if (p.getName().equals(key)) {
                    prop = p;
                    break;
                }
            }
            if (prop == null) {
                problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_INVALID_BEAN_PROPERTY, "Type '" + typeUtil.niceTypeName(type) + "' has no property '" + key + "'", keyStart, keyEnd - keyStart));
            } else {
                if (prop.isDeprecated()) {
                    problemCollector.accept(problemDeprecated(type, prop, keyStart, keyEnd - keyStart));
                }
                return navigate(keyEnd, prop.getType());
            }
        }
    }
    return null;
}
Also used : Type(org.springframework.ide.vscode.boot.metadata.types.Type) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 4 with ValueParser

use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.

the class SpringPropertiesReconcileEngine method reconcileType.

private void reconcileType(DocumentRegion escapedValue, Type expectType, IProblemCollector problems) {
    TypeUtil typeUtil = typeUtilProvider.getTypeUtil(escapedValue.getDocument());
    ValueParser parser = typeUtil.getValueParser(expectType);
    if (parser != null) {
        try {
            String valueStr = PropertiesFileEscapes.unescape(escapedValue.toString());
            if (!valueStr.contains("${")) {
                // Don't check strings that look like they use variable substitution.
                parser.parse(valueStr);
            }
        } catch (ValueParseException e) {
            problems.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, ExceptionUtil.getMessage(e), e.getHighlightRegion(escapedValue)));
        } catch (Exception e) {
            problems.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, "Expecting '" + typeUtil.niceTypeName(expectType) + "'", escapedValue));
        }
    }
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) TypeUtil(org.springframework.ide.vscode.boot.metadata.types.TypeUtil) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 5 with ValueParser

use of org.springframework.ide.vscode.commons.util.ValueParser in project sts4 by spring-projects.

the class ManifestYmlValueParsers method healthCheckEndpointPath.

/**
 * Parses an HTTP health check endpoint path. Note that this not parse the path portion of an HTTP URI, but verifies
 * that the WHOLE value is a valid HTTP path, and therefore needs to start with an '/'
 * <p/>
 * Example: /appPath, /?check=true, /appPath/test.txt
 * @return
 */
public static ValueParser healthCheckEndpointPath() {
    return new ValueParser() {

        @Override
        public Object parse(String pathVal) throws Exception {
            String parsed = pathVal;
            if (!StringUtil.hasText(pathVal)) {
                throw new IllegalArgumentException("Path requires a value staring with '/'");
            } else {
                URI uri = URI.create(pathVal);
                if (uri.getScheme() != null) {
                    throw new IllegalArgumentException("Path contains scheme: " + uri.getScheme());
                }
                if (uri.getHost() != null) {
                    throw new IllegalArgumentException("Path contains host: " + uri.getHost());
                }
                if (uri.getPort() != -1) {
                    throw new IllegalArgumentException("Path contains port: " + uri.getPort());
                }
                if (uri.getAuthority() != null) {
                    throw new IllegalArgumentException("Path contains authority: " + uri.getAuthority());
                }
                Path path = Paths.get(uri.getPath());
                if (!path.startsWith("/")) {
                    throw new IllegalArgumentException("Path must start with a '/'");
                }
            }
            return parsed;
        }
    };
}
Also used : Path(java.nio.file.Path) EnumValueParser(org.springframework.ide.vscode.commons.util.EnumValueParser) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) URI(java.net.URI)

Aggregations

ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)9 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)3 URI (java.net.URI)2 Test (org.junit.Test)2 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)2 EnumValueParser (org.springframework.ide.vscode.commons.util.EnumValueParser)2 ImmutableList (com.google.common.collect.ImmutableList)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)1 Type (org.springframework.ide.vscode.boot.metadata.types.Type)1 TypeUtil (org.springframework.ide.vscode.boot.metadata.types.TypeUtil)1 TypedProperty (org.springframework.ide.vscode.boot.metadata.types.TypedProperty)1 IType (org.springframework.ide.vscode.commons.java.IType)1 ReconcileException (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)1 AlwaysFailingParser (org.springframework.ide.vscode.commons.util.AlwaysFailingParser)1 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)1 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)1 ASTDynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext)1 DynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext)1