Skip to content

Commit

Permalink
Allow map keys to be surrounded by '[' in .properties file
Browse files Browse the repository at this point in the history
See: #534

Signed-off-by: Kris De Volder <kdevolder@pivotal.io>
  • Loading branch information
kdvolder committed Sep 17, 2020
1 parent 10f30d7 commit 197298e
Show file tree
Hide file tree
Showing 18 changed files with 860 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/org.eclipse.m2e.core.maven2Builder.launch</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
Expand All @@ -17,7 +22,6 @@
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.FeatureNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -263,30 +263,32 @@ public Object parse(String str) {
}

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) {
//Note, technically if 'enumValues is empty array' this means something different
// from when it is null. An empty array means a type that has no values, so
// assigning anything to it is an error.
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
}
if (isMap(type)) {
//Trying to parse map types from scalars is not possible. Thus we
// 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);
if (type!=null) {
ValueParser simpleParser = VALUE_PARSERS.get(type.getErasure());
if (simpleParser!=null) {
return simpleParser;
}
Collection<StsValueHint> enumValues = getAllowedValues(type, EnumCaseMode.ALIASED);
if (enumValues!=null) {
//Note, technically if 'enumValues is empty array' this means something different
// from when it is null. An empty array means a type that has no values, so
// assigning anything to it is an error.
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
}
if (isMap(type)) {
//Trying to parse map types from scalars is not possible. Thus we
// 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);
}
}
}
}
Expand Down Expand Up @@ -443,16 +445,18 @@ public boolean isAtomic(Type type) {
}

/**
* Check if it is valid to
* use the notation <name>[<index>]=<value> in property file
* Check if it is valid to use the notation <name>[<index>]=<value> in property file
* for properties of this type.
* <p>
* Note it is also possible to use 'bracket' notation with other types such as Map's for example.
* This method returns true only for types that support/expect using of integer indexes.
*/
public boolean isBracketable(Type type) {
//Note array types where once not considered 'Bracketable'
public boolean isIndexable(Type type) {
//Note array types where once not considered 'indexable'
//see: STS-4031

//However...
//Seems that in Boot 1.3 arrays are now 'Bracketable' and funcion much equivalnt to list (even including 'autogrowing' them).
//Seems that in Boot 1.3 arrays are now 'Indexable' and funcion much equivalnt to list (even including 'autogrowing' them).
//This is actually more logical too.
//So '[' notation in props file can be used for either list or arrays (at least in recent versions of boot).
//Note also 'Set' are now considered bracketable. See: https://www.pivotaltracker.com/story/show/154644992
Expand All @@ -463,7 +467,7 @@ public boolean isBracketable(Type type) {
* Check if type can be treated / represented as a sequence node in .yml file
*/
public boolean isSequencable(Type type) {
return isBracketable(type);
return isIndexable(type);
}

private static boolean isArray(Type type) {
Expand Down Expand Up @@ -595,7 +599,7 @@ public boolean isAssignableType(Type type) {
}

private boolean isAssignableCollection(Type type) {
if (isBracketable(type)) {
if (isIndexable(type)) {
Type domainType = getDomainType(type);
return isAtomic(domainType);
}
Expand Down Expand Up @@ -1049,6 +1053,11 @@ public static boolean isClass(Type type) {
}


public boolean isBracketable(Type type) {
return isMap(type) || isIndexable(type);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////
// Addapting our interface so it is compatible with YTypeUtil
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected static String propertyCompletionPostfix(TypeUtil typeUtil, Type type)
if (type!=null) {
if (typeUtil.isAssignableType(type)) {
postfix = "=";
} else if (typeUtil.isBracketable(type)) {
} else if (typeUtil.isIndexable(type)) {
postfix = "[";
} else if (typeUtil.isDotable(type)) {
postfix = ".";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ private Type bracketNavigate(int offset, Type type) {
} else {
String indexStr = textBetween(lbrack+1, rbrack);
if (!indexStr.contains("${")) {
try {
Integer.parseInt(indexStr);
} catch (Exception e) {
problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_NON_INTEGER_IN_BRACKETS,
"Expecting 'Integer' for '[...]' notation '"+textBetween(region.getStart(), lbrack)+"'",
lbrack+1, rbrack-lbrack-1
));
Type keytype = typeUtil.getKeyType(type);
ValueParser parser = typeUtil.getValueParser(keytype);
if (parser!=null) {
try {
parser.parse(indexStr);
} catch (Exception e) {
problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH,
"Expecting '"+typeUtil.niceTypeName(keytype)+"' for '[...]' notation '"+textBetween(region.getStart(), lbrack)+"'",
lbrack+1, rbrack-lbrack-1
));
}
}
}
Type domainType = TypeUtil.getDomainType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
import org.springframework.ide.vscode.boot.configurationmetadata.Deprecation.Level;
import org.springframework.ide.vscode.boot.configurationmetadata.ValueHint;
import org.springframework.ide.vscode.boot.configurationmetadata.ValueProvider;
import org.springframework.ide.vscode.boot.metadata.PropertyInfo;
import org.springframework.ide.vscode.boot.metadata.SpringPropertyIndex;
import org.springframework.ide.vscode.boot.metadata.SpringPropertyIndexProvider;
import org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.util.FuzzyMap;
import org.springframework.ide.vscode.commons.util.text.IDocument;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Properties;

import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.Diagnostic;
Expand Down Expand Up @@ -575,13 +576,41 @@ public void testReconcileCatchesParseError() throws Exception {
"spring.thymeleaf.view-names[1]=hello" //This is okay now. Boot handles this notation for arrays
);
editor.assertProblems(
"bork|Integer",
"bork|'int'",
"[|matching ']'",
"crap|'.' or '['",
"[0]|Can't use '[..]'"
//no other problems
);
}

@Test public void test_GH_534() throws Exception {
IJavaProject p = createPredefinedMavenProject("map-of-pojo");
useProject(p);
data("my.props", "java.util.Properties", null, "Properties in java.util.Properties");
data("my.arr", "java.lang.String[]", null, "Array");

Editor editor = newEditor(
"my.arr[index]=something\n" +
"my.props[foo.bar]=something\n" +
"my.map[foo.bar].name=Freddy\n" +
"my.map[foo.bar].age=the-age\n"
);
editor.assertProblems(
"index|'int'",
"the-age|'int'"
);

//Also check if completion engine understands the convention
editor = newEditor(
"my.map[foo.bar].<*>"
);
editor.assertContextualCompletions("<*>",
// ==>
"age=<*>",
"name=<*>"
);
}

@Test public void testRelaxedNameReconciling() throws Exception {
data("connection.remote-host", "java.lang.String", "service.net", null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;

public class MavenWrapperDownloader {

private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";

/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";

/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";

public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

0 comments on commit 197298e

Please sign in to comment.