Search in sources :

Example 1 with EnumValueParser

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

the class YTypeFactory method yenum.

public YAtomicType yenum(String name, SchemaContextAware<BiFunction<String, Collection<String>, String>> errorMessageFormatter, SchemaContextAware<Collection<String>> values) {
    YAtomicType t = yatomic(name);
    t.setHintProvider((dc) -> {
        return PartialCollection.compute(() -> values.withContext(dc)).map(BasicYValueHint::new);
    });
    t.parseWith((DynamicSchemaContext dc) -> {
        EnumValueParser enumParser = new EnumValueParser(name, values.withContext(dc)) {

            @Override
            protected String createErrorMessage(String parseString, Collection<String> values) {
                try {
                    return errorMessageFormatter.withContext(dc).apply(parseString, values);
                } catch (Exception e) {
                    return super.createErrorMessage(parseString, values);
                }
            }
        };
        return enumParser;
    });
    return t;
}
Also used : EnumValueParser(org.springframework.ide.vscode.commons.util.EnumValueParser) Collection(java.util.Collection) PartialCollection(org.springframework.ide.vscode.commons.util.PartialCollection) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)

Example 2 with EnumValueParser

use of org.springframework.ide.vscode.commons.util.EnumValueParser 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)

Aggregations

EnumValueParser (org.springframework.ide.vscode.commons.util.EnumValueParser)2 Collection (java.util.Collection)1 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)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 PartialCollection (org.springframework.ide.vscode.commons.util.PartialCollection)1 ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)1