Search in sources :

Example 1 with ASTDynamicSchemaContext

use of org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext 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)

Aggregations

Map (java.util.Map)1 ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)1 ASTDynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext)1 DynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext)1 YType (org.springframework.ide.vscode.commons.yaml.schema.YType)1 YTypedProperty (org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)1 Constraint (org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)1 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)1