Search in sources :

Example 6 with ValueParser

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

the class SchemaBasedYamlASTReconciler method reconcile.

private void reconcile(YamlFileAST ast, YamlPath path, Node parent, Node node, YType _type) {
    // IDocument doc = ast.getDocument();
    if (_type != null && !skipReconciling(node)) {
        DynamicSchemaContext schemaContext = new ASTDynamicSchemaContext(ast, path, node);
        YType type = typeUtil.inferMoreSpecificType(_type, schemaContext);
        if (typeCollector != null) {
            typeCollector.accept(node, type);
        }
        checkConstraints(parent, node, type, schemaContext);
        switch(getNodeId(node)) {
            case mapping:
                MappingNode map = (MappingNode) node;
                checkForDuplicateKeys(map);
                if (typeUtil.isMap(type)) {
                    for (NodeTuple entry : map.getValue()) {
                        String key = NodeUtil.asScalar(entry.getKeyNode());
                        reconcile(ast, keyAt(path, key), map, entry.getKeyNode(), typeUtil.getKeyType(type));
                        reconcile(ast, valueAt(path, key), map, entry.getValueNode(), typeUtil.getDomainType(type));
                    }
                } else if (typeUtil.isBean(type)) {
                    Map<String, YTypedProperty> beanProperties = typeUtil.getPropertiesMap(type);
                    checkRequiredProperties(parent, map, type, beanProperties, schemaContext);
                    for (NodeTuple entry : map.getValue()) {
                        Node keyNode = entry.getKeyNode();
                        String key = NodeUtil.asScalar(keyNode);
                        if (key == null) {
                            expectScalar(node);
                        } else {
                            YTypedProperty prop = beanProperties.get(key);
                            if (prop == null) {
                                unknownBeanProperty(keyNode, type, key);
                            } else {
                                if (prop.isDeprecated()) {
                                    String msg = prop.getDeprecationMessage();
                                    if (StringUtil.hasText(msg)) {
                                        problems.accept(YamlSchemaProblems.deprecatedProperty(msg, keyNode));
                                    } else {
                                        problems.accept(YamlSchemaProblems.deprecatedProperty(keyNode, type, prop));
                                    }
                                }
                                reconcile(ast, valueAt(path, key), map, entry.getValueNode(), prop.getType());
                            }
                        }
                    }
                } else {
                    expectTypeButFoundMap(type, node);
                }
                break;
            case sequence:
                SequenceNode seq = (SequenceNode) node;
                if (typeUtil.isSequencable(type)) {
                    for (int i = 0; i < seq.getValue().size(); i++) {
                        Node el = seq.getValue().get(i);
                        reconcile(ast, valueAt(path, i), seq, el, typeUtil.getDomainType(type));
                    }
                } else {
                    expectTypeButFoundSequence(type, node);
                }
                break;
            case scalar:
                if (typeUtil.isAtomic(type)) {
                    SchemaContextAware<ValueParser> parserProvider = typeUtil.getValueParser(type);
                    if (parserProvider != null) {
                        // Take care not to execute parserProvider early just to check how long it should be delayed.
                        delayedConstraints.add(() -> {
                            parserProvider.safeWithContext(schemaContext).ifPresent(parser -> {
                                if (parser.longRunning()) {
                                    slowDelayedConstraints.add(() -> {
                                        parse(ast, node, type, parser);
                                    });
                                } else {
                                    parse(ast, node, type, parser);
                                }
                            });
                        });
                    }
                } else {
                    expectTypeButFoundScalar(type, node);
                }
                break;
            default:
        }
    }
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) DynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext) ASTDynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ASTDynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Map(java.util.Map) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)

Example 7 with ValueParser

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

the class BoshValueParserTest method urlGarbage.

@Test
public void urlGarbage() throws Exception {
    ValueParser urlParser = BoshValueParsers.url("http", "https", "file");
    assertProblem(urlParser, "<*>woot<*>://foobar.com", "Url scheme must be one of [http, https, file]");
    assertProblem(urlParser, "<*>wOOt<*>://foobar.com", "Url scheme must be one of [http, https, file]");
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) Test(org.junit.Test)

Example 8 with ValueParser

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

the class BoshValueParsers method url.

public static ValueParser url(String... _schemes) {
    return new ValueParser() {

        private final ImmutableList<String> validSchemes = ImmutableList.copyOf(_schemes);

        @Override
        public Object parse(String s) throws Exception {
            URI uri = new URI(s);
            String scheme = uri.getScheme();
            if (scheme == null) {
                throw new ValueParseException(message());
            } else if (!validSchemes.contains(scheme.toLowerCase())) {
                int start = s.indexOf(scheme);
                if (start >= 0) {
                    int end = start + scheme.length();
                    throw new ValueParseException(message(), start, end);
                } else {
                    // Trouble finding exact location of underlined region so underline whole url
                    throw new ValueParseException(message());
                }
            }
            return uri;
        }

        private String message() {
            return "Url scheme must be one of " + validSchemes;
        }
    };
}
Also used : ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) ImmutableList(com.google.common.collect.ImmutableList) URI(java.net.URI) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 9 with ValueParser

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

the class GithubValueParsers method uri.

public static ValueParser uri(GithubInfoProvider github) {
    return new ValueParser() {

        @Override
        public GithubRepoReference parse(String _str) throws Exception {
            TextDocument doc = new TextDocument(null, LanguageId.PLAINTEXT);
            doc.setText(_str);
            DocumentRegion str = new DocumentRegion(doc);
            GithubRepoReference repo = parseFormat(str);
            if (repo != null) {
                Collection<String> knownRepos;
                try {
                    knownRepos = github.getReposForOwner(repo.owner.toString());
                } catch (Exception e) {
                    // Couldn't read info from github. Ignore this in reconciler context.
                    return repo;
                }
                if (knownRepos == null) {
                    throw unknownEntity("User or Organization not found: '" + repo.owner + "'", repo.owner);
                } else {
                    if (!knownRepos.contains(repo.name.toString())) {
                        throw unknownEntity("Repo not found: '" + repo.name + "'", repo.name);
                    }
                }
            }
            return repo;
        }

        private GithubRepoReference parseFormat(DocumentRegion str) throws Exception {
            String prefix = checkPrefix(str);
            if (prefix != null) {
                DocumentRegion ownerAndName = str.subSequence(prefix.length());
                // Should end with '.git'
                if (ownerAndName.endsWith(".git")) {
                    ownerAndName = ownerAndName.subSequence(0, ownerAndName.length() - 4);
                } else {
                    DocumentRegion highlight = ownerAndName.textAtEnd(1);
                    throw new ValueParseException("GitHub repo uri should end with '.git'", highlight.getStart(), highlight.getEnd());
                }
                int slash = ownerAndName.indexOf('/');
                if (slash >= 0) {
                    return new GithubRepoReference(ownerAndName.subSequence(0, slash), ownerAndName.subSequence(slash + 1));
                } else {
                    throw new ValueParseException("Expecting something of the form '${owner}/${repo}'.", ownerAndName.getStart(), ownerAndName.getEnd());
                }
            }
            return null;
        }

        private String checkPrefix(DocumentRegion str) throws ValueParseException {
            for (String expectedPrefix : GithubRepoContentAssistant.URI_PREFIXES) {
                int lastChar = expectedPrefix.length() - 1;
                if (str.startsWith(expectedPrefix.substring(0, lastChar))) {
                    if (str.charAt(lastChar) == expectedPrefix.charAt(lastChar)) {
                        return expectedPrefix;
                    }
                    throw new ValueParseException("Expecting a '" + expectedPrefix.charAt(lastChar) + "'", lastChar, lastChar + 1);
                }
            }
            return null;
        }
    };
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

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