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:
}
}
}
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]");
}
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;
}
};
}
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;
}
};
}
Aggregations