Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions core/src/main/java/org/elasticsearch/common/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeMap;
import java.util.ListIterator;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -414,7 +415,7 @@ public List<String> getAsList(String key, List<String> defaultValue, Boolean com
final Object valueFromPrefix = settings.get(key);
if (valueFromPrefix != null) {
if (valueFromPrefix instanceof List) {
return ((List<String>) valueFromPrefix); // it's already unmodifiable since the builder puts it as a such
return Collections.unmodifiableList((List<String>) valueFromPrefix);
} else if (commaDelimited) {
String[] strings = Strings.splitStringByCommaToArray(get(key));
if (strings.length > 0) {
Expand Down Expand Up @@ -1042,7 +1043,7 @@ public Builder putList(String setting, String... values) {
*/
public Builder putList(String setting, List<String> values) {
remove(setting);
map.put(setting, Collections.unmodifiableList(new ArrayList<>(values)));
map.put(setting, new ArrayList<>(values));
return this;
}

Expand Down Expand Up @@ -1210,10 +1211,23 @@ public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
Iterator<Map.Entry<String, Object>> entryItr = map.entrySet().iterator();
while (entryItr.hasNext()) {
Map.Entry<String, Object> entry = entryItr.next();
if (entry.getValue() == null || entry.getValue() instanceof List) {
if (entry.getValue() == null) {
// a null value obviously can't be replaced
continue;
}
if (entry.getValue() instanceof List) {
List<String> ls = (List<String>) entry.getValue();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ls is never used again after the next line; do we need this local variable?

ListIterator<String> li = ls.listIterator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a local final variable?

Copy link
Contributor Author

@mayya-sharipova mayya-sharipova Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for final. I am not very clear what you meant by local ? Do you recommend to declare li before the while loop on the line 1213?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not being clear. What I mean is that these variables (which are local variables, they only have scope of the current) block I think would clearer if they were final because they I know by the declaration they are never mutated instead of having to keep checking the code "is this the same object as when the variable was first declared?".

while(li.hasNext()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: spacing between while and (.

String value = li.next();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a clearer name than value?

String value2 = propertyPlaceholder.replacePlaceholders(value, placeholderResolver);
if (! value.equals(value2)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: spacing between ! and value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a clearer name than value2?

li.set(value2);
}
}
continue;
}

String value = propertyPlaceholder.replacePlaceholders(Settings.toString(entry.getValue()), placeholderResolver);
// if the values exists and has length, we should maintain it in the map
// otherwise, the replace process resolved into removing it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public void testReplacePropertiesPlaceholderSystemProperty() {
assertThat(settings.get("setting1"), equalTo(value));
}

public void testReplacePropertiesPlaceholderSystemPropertyList() {
String value = System.getProperty("java.home");
assertFalse(value.isEmpty());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will be needed if you take the suggestion below, but in general assertFalse and assertTrue should be avoided because they do not give good error messages (a failing expectation would only say AssertionError). Here, we could use assertThat(value, not(isEmptyOrNullString()) as then the expectation would say

Expected: not (null or an empty string)
     but: was ""

which is a lot more helpful!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, this is very helpful to know

Settings settings = Settings.builder()
.put("property.placeholder", value)
.putList("setting1", "${property.placeholder}", "${property.placeholder}")
.replacePropertyPlaceholders()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have replacePropertyPlaceholders(Function<String, String>); we use this rather than pulling from the same settings object as I think this will be a more realistic test?

.build();
assertThat(settings.getAsList("setting1"), contains(value, value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test replacing different placeholders?

}

public void testReplacePropertiesPlaceholderSystemVariablesHaveNoEffect() {
final String value = System.getProperty("java.home");
assertNotNull(value);
Expand Down