Skip to content

Commit 1d963dc

Browse files
andseldonoghuc
andauthored
Reimplemented Ruby's CoercibleString setting into Java (#18187)
Translates CoercibleString setting class into plain Java, adding unit test to cover the functionality. Co-authored-by: Cas Donoghue <cas.donoghue@gmail.com>
1 parent 7e9c384 commit 1d963dc

File tree

4 files changed

+123
-23
lines changed

4 files changed

+123
-23
lines changed

logstash-core/lib/logstash/environment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def self.as_java_range(r)
5858
Setting::BooleanSetting.new("pipeline.reloadable", true),
5959
Setting::BooleanSetting.new("pipeline.plugin_classloaders", false),
6060
Setting::BooleanSetting.new("pipeline.separate_logs", false),
61-
Setting::CoercibleString.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]),
62-
Setting::CoercibleString.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)),
61+
Setting::CoercibleStringSetting.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]),
62+
Setting::CoercibleStringSetting.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)),
6363
Setting.new("path.plugins", Array, []),
6464
Setting::NullableStringSetting.new("interactive", nil, false),
6565
Setting::BooleanSetting.new("config.debug", false),

logstash-core/lib/logstash/settings.rb

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -448,27 +448,7 @@ def validate(value)
448448
java_import org.logstash.settings.PasswordSetting
449449
java_import org.logstash.settings.ValidatedPasswordSetting
450450

451-
# The CoercibleString allows user to enter any value which coerces to a String.
452-
# For example for true/false booleans; if the possible_strings are ["foo", "true", "false"]
453-
# then these options in the config file or command line will be all valid: "foo", true, false, "true", "false"
454-
#
455-
class CoercibleString < Coercible
456-
def initialize(name, default = nil, strict = true, possible_strings = [], &validator_proc)
457-
@possible_strings = possible_strings
458-
super(name, Object, default, strict, &validator_proc)
459-
end
460-
461-
def coerce(value)
462-
value.to_s
463-
end
464-
465-
def validate(value)
466-
super(value)
467-
unless @possible_strings.empty? || @possible_strings.include?(value)
468-
raise ArgumentError.new("Invalid value \"#{value}\". Options are: #{@possible_strings.inspect}")
469-
end
470-
end
471-
end
451+
java_import org.logstash.settings.CoercibleStringSetting
472452

473453
class ExistingFilePath < Setting
474454
def initialize(name, default = nil, strict = true)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.logstash.settings;
21+
22+
import java.util.List;
23+
24+
public class CoercibleStringSetting extends Coercible<Object> {
25+
26+
private final List<String> possibleStrings;
27+
28+
@SuppressWarnings("this-escape")
29+
public CoercibleStringSetting(String name, Object defaultValue, boolean strict, List<String> possibleStrings) {
30+
// this super doesn't call validate, with strict false it permits to set
31+
// possibleStrings field used in validate later.
32+
super(name, defaultValue, false, noValidator());
33+
this.possibleStrings = possibleStrings;
34+
35+
if (strict) {
36+
String coercedDefault = coerce(defaultValue);
37+
validate(coercedDefault);
38+
this.defaultValue = coercedDefault;
39+
} else {
40+
this.defaultValue = defaultValue;
41+
}
42+
}
43+
44+
@Override
45+
public String coerce(Object value) {
46+
if (value == null) {
47+
return "";
48+
}
49+
return value.toString();
50+
}
51+
52+
@Override
53+
public void validate(Object input) throws IllegalArgumentException {
54+
super.validate(input);
55+
String coerced = coerce(input);
56+
staticValidate(coerced, possibleStrings, this.getName());
57+
}
58+
59+
private static void staticValidate(String input, List<String> possibleStrings, String name) {
60+
if (!possibleStrings.isEmpty() && !possibleStrings.contains(input)) {
61+
throw new IllegalArgumentException(String.format("Invalid value \"%s\". Options are: %s", input, possibleStrings));
62+
}
63+
}
64+
65+
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.logstash.settings;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.util.List;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
public class CoercibleStringSettingTest {
29+
30+
private static final List<String> POSSIBLE_VALUES = List.of("foo", "true", "false");
31+
private CoercibleStringSetting sut;
32+
33+
@Before
34+
public void setUp() {
35+
sut = new CoercibleStringSetting("my.setting", "foo", true, POSSIBLE_VALUES);
36+
}
37+
38+
@Test
39+
public void givenNativeBooleanFalseWhenSetThenValueIsCoercedToStringFalse() {
40+
sut.set(false);
41+
42+
assertEquals("false", sut.value());
43+
}
44+
@Test(expected = IllegalArgumentException.class)
45+
public void givenInvalidValueWhenSetThenThrowsException() {
46+
sut.set("invalid");
47+
}
48+
@Test
49+
public void givenNativeBooleanTrueWhenSetThenValueIsCoercedToStringTrue() {
50+
sut.set(true);
51+
52+
assertEquals("true", sut.value());
53+
}
54+
}

0 commit comments

Comments
 (0)