Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions logstash-core/lib/logstash/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def self.as_java_range(r)
Setting::BooleanSetting.new("pipeline.reloadable", true),
Setting::BooleanSetting.new("pipeline.plugin_classloaders", false),
Setting::BooleanSetting.new("pipeline.separate_logs", false),
Setting::CoercibleString.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]),
Setting::CoercibleString.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)),
Setting::CoercibleStringSetting.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]),
Setting::CoercibleStringSetting.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)),
Setting.new("path.plugins", Array, []),
Setting::NullableStringSetting.new("interactive", nil, false),
Setting::BooleanSetting.new("config.debug", false),
Expand Down
24 changes: 2 additions & 22 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,28 +499,8 @@ def build_password_policies
policies
end
end

# The CoercibleString allows user to enter any value which coerces to a String.
# For example for true/false booleans; if the possible_strings are ["foo", "true", "false"]
# then these options in the config file or command line will be all valid: "foo", true, false, "true", "false"
#
class CoercibleString < Coercible
def initialize(name, default = nil, strict = true, possible_strings = [], &validator_proc)
@possible_strings = possible_strings
super(name, Object, default, strict, &validator_proc)
end

def coerce(value)
value.to_s
end

def validate(value)
super(value)
unless @possible_strings.empty? || @possible_strings.include?(value)
raise ArgumentError.new("Invalid value \"#{value}\". Options are: #{@possible_strings.inspect}")
end
end
end

java_import org.logstash.settings.CoercibleStringSetting

class ExistingFilePath < Setting
def initialize(name, default = nil, strict = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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
*
* http://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.
*/

package org.logstash.settings;

import java.util.List;

public class CoercibleStringSetting extends Coercible<Object> {

private final List<String> possibleStrings;

@SuppressWarnings("this-escape")
public CoercibleStringSetting(String name, Object defaultValue, boolean strict, List<String> possibleStrings) {
// this super doesn't call validate, with strict false it permits to set
// possibleStrings field used in validate later.
super(name, defaultValue, false, noValidator());
this.possibleStrings = possibleStrings;

if (strict) {
String coercedDefault = coerce(defaultValue);
validate(coercedDefault);
this.defaultValue = coercedDefault;
} else {
this.defaultValue = defaultValue;
}
}

@Override
public String coerce(Object value) {
if (value == null) {
return "";
}
return value.toString();
}

@Override
public void validate(Object input) throws IllegalArgumentException {
super.validate(input);

staticValidate(input.toString(), possibleStrings, this.getName());
}

private static void staticValidate(String input, List<String> possibleStrings, String name) {
if (!possibleStrings.isEmpty() && !possibleStrings.contains(input)) {
throw new IllegalArgumentException(String.format("Invalid value \"%s\". Options are: %s", input, possibleStrings));
}
}


}