Skip to content

Commit

Permalink
[WIP] running rubyTests gives errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andsel committed Dec 31, 2020
1 parent 1d0d08e commit 96604e2
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 54 deletions.
39 changes: 18 additions & 21 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def flatten_hash(h,f="",g={})
end
end

# java_import org.logstash.settings.Setting

class Setting
# include LogStash::Util::Loggable
Expand Down Expand Up @@ -304,30 +303,28 @@ class Setting

class Coercible < Setting
def initialize(name, klass, default=nil, strict=true, &validator_proc)
@name = name
# this call is needed to instantiate the Java Setting in parent SettingExt
coercible_init(name, klass, default, strict, &validator_proc)
unless klass.is_a?(Class)
raise ArgumentError.new("Setting \"#{@name}\" must be initialized with a class (received #{klass})")
raise ArgumentError.new("Setting \"#{name}\" must be initialized with a class (received #{klass})")
end
@klass = klass
@validator_proc = validator_proc
@value = nil
@value_is_set = false

if strict
coerced_default = coerce(default)
validate(coerced_default)
@default = coerced_default
new_default = coerced_default
else
@default = default
new_default = default
end
set_default(new_default)
end

def set(value)
coerced_value = coerce(value)
validate(coerced_value)
@value = coerce(coerced_value)
@value_is_set = true
@value
new_value = coerce(coerced_value)
assign_value(new_value)
new_value
end

def coerce(value)
Expand Down Expand Up @@ -623,16 +620,16 @@ def coerce(value)

protected
def validate(input)
if !input.is_a?(@klass)
raise ArgumentError.new("Setting \"#{@name}\" must be a #{@klass}. Received: #{input} (#{input.class})")
if !input.is_a?(klass)
raise ArgumentError.new("Setting \"#{name}\" must be a #{klass}. Received: #{input} (#{input.class})")
end

unless input.all? {|el| el.kind_of?(@element_class) }
raise ArgumentError.new("Values of setting \"#{@name}\" must be #{@element_class}. Received: #{input.map(&:class)}")
raise ArgumentError.new("Values of setting \"#{name}\" must be #{@element_class}. Received: #{input.map(&:class)}")
end

if @validator_proc && !@validator_proc.call(input)
raise ArgumentError.new("Failed to validate setting \"#{@name}\" with value: #{input}")
raise ArgumentError.new("Failed to validate setting \"#{name}\" with value: #{input}")
end
end
end
Expand Down Expand Up @@ -663,16 +660,16 @@ def initialize(name, klass, default = nil)
end

def set(value)
@value = coerce(value)
@value_is_set = true
@value
coerced_value = coerce(value)
assign_value(coerced_value)
coerced_value
end

def coerce(value)
if value.is_a?(@klass)
if value.is_a?(klass)
return value
end
@klass.new(value)
klass.new(value)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion logstash-core/spec/logstash/settings/numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
context "float" do
it "should coerce that string to the number" do
subject.set("1.1")
expect(subject.value).to eq(1.1)
expect(subject.value).to be_within(0.01).of(1.1)
end
end
context "int" do
Expand Down
2 changes: 1 addition & 1 deletion logstash-core/src/main/java/org/logstash/RubyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public final class RubyUtil {

// private static final RubyClass SETTINGS_CLASS;

private static final RubyClass SETTING_CLASS;
public static final RubyClass SETTING_CLASS;

static {
RUBY = Ruby.getGlobalRuntime();
Expand Down
24 changes: 24 additions & 0 deletions logstash-core/src/main/java/org/logstash/settings/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.logstash.settings;

public class Range<T> {
private T min;
private T max;

public Range(T min, T max) {
this.min = min;
this.max = max;
}

public T getMin() {
return min;
}

public T getMax() {
return max;
}

@Override
public String toString() {
return "Range: [" + min + ".." + max + ']';
}
}
30 changes: 25 additions & 5 deletions logstash-core/src/main/java/org/logstash/settings/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,40 @@
public class Setting {

private final String name;
private final Class<? extends Object> klass;
private final Object defaultValue;
final Class<? extends Object> klass;
protected Object defaultValue;
private boolean valueIsSet;
private Object value;
private final boolean strict;
private final Predicate<Object> validator;

public Setting(String name, Class<? extends Object> klass, Object defaultValue, boolean strict) {
Setting(String name, Class<? extends Object> klass, Object defaultValue, boolean strict) {
this(name, klass, defaultValue, strict, null);
}

public Setting(String name, Class<? extends Object> klass, Object defaultValue, boolean strict, Predicate<Object> validator) {
Setting(String name, Class<? extends Object> klass, Object defaultValue, boolean strict, Predicate<Object> validator) {
this.name = name;
this.klass = klass;
this.strict = strict;
this.validator = validator;
this.value = null;
this.valueIsSet = false;
this.defaultValue = defaultValue;
}

void init() {
if (strict) {
validate(defaultValue);
}
this.defaultValue = defaultValue;
}

// Copy constructor
Setting(Setting copy) {
this.name = copy.name;
this.klass = copy.klass;
this.defaultValue = copy.defaultValue;
this.strict = copy.strict;
this.validator = copy.validator;
}

public String getName() {
Expand Down Expand Up @@ -68,6 +80,14 @@ public Object set(Object value) {
return value;
}

/**
* Used by Ruby subclasses to assign value without any validation
* */
protected void assignValue(Object value) {
this.value = value;
this.valueIsSet = true;
}

public void reset() {
value = null;
valueIsSet = false;
Expand Down
Loading

0 comments on commit 96604e2

Please sign in to comment.