Skip to content

Commit 130cb84

Browse files
committed
Reimplemented Ruby's CoercibleString setting into Java
1 parent cd71a4b commit 130cb84

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
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: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -499,28 +499,8 @@ def build_password_policies
499499
policies
500500
end
501501
end
502-
503-
# The CoercibleString allows user to enter any value which coerces to a String.
504-
# For example for true/false booleans; if the possible_strings are ["foo", "true", "false"]
505-
# then these options in the config file or command line will be all valid: "foo", true, false, "true", "false"
506-
#
507-
class CoercibleString < Coercible
508-
def initialize(name, default = nil, strict = true, possible_strings = [], &validator_proc)
509-
@possible_strings = possible_strings
510-
super(name, Object, default, strict, &validator_proc)
511-
end
512-
513-
def coerce(value)
514-
value.to_s
515-
end
516-
517-
def validate(value)
518-
super(value)
519-
unless @possible_strings.empty? || @possible_strings.include?(value)
520-
raise ArgumentError.new("Invalid value \"#{value}\". Options are: #{@possible_strings.inspect}")
521-
end
522-
end
523-
end
502+
503+
java_import org.logstash.settings.CoercibleStringSetting
524504

525505
class ExistingFilePath < Setting
526506
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+
56+
staticValidate(input.toString(), 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+
}

0 commit comments

Comments
 (0)