This commit is contained in:
Kris De Volder
2019-07-24 12:29:55 -07:00
parent d30a82d04f
commit 89d79d9067
3 changed files with 44 additions and 31 deletions

View File

@@ -257,4 +257,8 @@ public class StringUtil {
return queryindex == queryChars.length;
}
public static String snakeCaseToHyphens(String snakeString) {
return snakeString.replace('_', '-').toLowerCase();
}
}

View File

@@ -11,11 +11,13 @@
package org.springframework.ide.vscode.boot.yaml.reconcile;
import static org.springframework.ide.vscode.boot.yaml.reconcile.ApplicationYamlProblemType.*;
import static org.springframework.ide.vscode.boot.yaml.reconcile.ApplicationYamlProblemType.YAML_DEPRECATED_ERROR;
import static org.springframework.ide.vscode.boot.yaml.reconcile.ApplicationYamlProblemType.YAML_DEPRECATED_WARNING;
import static org.springframework.ide.vscode.boot.yaml.reconcile.ApplicationYamlProblemType.YAML_DUPLICATE_KEY;
import static org.springframework.ide.vscode.commons.yaml.ast.NodeUtil.asScalar;
import static org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST.getChildren;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -60,6 +62,8 @@ import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import com.google.common.collect.ImmutableSet;
/**
* @author Kris De Volder
*/
@@ -139,24 +143,21 @@ public class ApplicationYamlASTReconciler implements YamlASTReconciler {
private void reconcile(YamlFileAST root, NodeTuple entry, IndexNavigator nav) {
Node keyNode = entry.getKeyNode();
String key = asScalar(keyNode);
if (key==null) {
String _key = asScalar(keyNode);
if (_key==null) {
expectScalar(keyNode);
} else {
IndexNavigator subNav = nav.selectSubProperty(key);
PropertyInfo match = subNav.getExactMatch();
PropertyInfo extension = subNav.getExtensionCandidate();
if (match==null && extension==null) {
//nothing found for this key. Maybe user is using camelCase variation of the key?
String keyAlias = StringUtil.camelCaseToHyphens(key);
IndexNavigator subNavAlias = nav.selectSubProperty(keyAlias);
IndexNavigator subNav = null;
PropertyInfo match = null;
PropertyInfo extension = null;
//Try different 'relaxed' variants for this key. Maybe user is using camelCase or snake case?
for (String key : keyAliases(_key)) {
IndexNavigator subNavAlias = nav.selectSubProperty(key);
match = subNavAlias.getExactMatch();
extension = subNavAlias.getExtensionCandidate();
if (match!=null || extension!=null) {
//Got something for the alias, so use that instead.
//Note: do not swap for alias unless we actually found something.
// This gives more logical errors (in terms of user's key, not its canonical alias)
subNav = subNavAlias;
break; //stop at first alias that gives a result.
}
}
if (match!=null && extension!=null) {
@@ -183,6 +184,14 @@ public class ApplicationYamlASTReconciler implements YamlASTReconciler {
}
}
private Collection<String> keyAliases(String originalKey) {
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
builder.add(originalKey);
builder.add(StringUtil.camelCaseToHyphens(originalKey));
builder.add(StringUtil.snakeCaseToHyphens(originalKey));
return builder.build();
}
/**
* Reconcile a node given the type that we expect the node to be.
*/

View File

@@ -71,37 +71,37 @@ public class ApplicationYamlEditorTest extends AbstractPropsEditorTest {
////////////////////////////////////////////////////////////////////////////////////////
@Ignore //Ignored because this bug is not yet fixed so the test fails.
@Test public void bug_GH_327() throws Exception {
//See https://github.com/spring-projects/sts4/issues/327
data("spring.resources.static-locations", "java.lang.String[]", null, "Blah");
data("spring.devtools.restart.additional-paths", "java.util.List<java.lang.String>", null, "Blah blah");
data("spring.resources.static-locations", "java.lang.Boolean", null, "Blah");
data("spring.devtools.restart.additional-paths", "java.lang.Boolean", null, "Blah blah");
Editor editor;
editor = harness.newEditor(
"spring:\n" +
" resources:\n" +
" static_locations: []\n" +
" devtools:\n" +
" restart:\n" +
" additional_paths: []\n"
);
editor.assertProblems(/*NONE*/);
//Also check whether reconciler understands the structure when inside of a 'relaxed' name key
editor = harness.newEditor(
"spring:\n" +
" resources:\n" +
" static_locations: not-a-list-1\n" +
" static_locations: bad\n" +
" devtools:\n" +
" restart:\n" +
" additional_paths: not-a-list-2\n"
" additional_paths: wrong\n"
);
editor.assertProblems(
"not-a-list-1|XXXX", //TODO: fill in proper expected message instead of XXXX
"not-a-list-2|XXXX" //TODO: fill in proper expected message instead of XXXX
"bad|boolean", //TODO: fill in proper expected message instead of XXXX
"wrong|boolean" //TODO: fill in proper expected message instead of XXXX
);
// basic check
editor = harness.newEditor(
"spring:\n" +
" resources:\n" +
" static_locations: true\n" +
" devtools:\n" +
" restart:\n" +
" additional_paths: false\n"
);
editor.assertProblems(/*NONE*/);
}
@Test public void bug_165724475() throws Exception {