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 23, 2020
1 parent 1d0d08e commit db55924
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class Setting

class Coercible < Setting
def initialize(name, klass, default=nil, strict=true, &validator_proc)
super(name, klass, default, strict, &validator_proc)
@name = name
unless klass.is_a?(Class)
raise ArgumentError.new("Setting \"#{@name}\" must be initialized with a class (received #{klass})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Setting {

private final String name;
private final Class<? extends Object> klass;
final Class<? extends Object> klass;
private final Object defaultValue;
private boolean valueIsSet;
private Object value;
Expand Down
60 changes: 55 additions & 5 deletions logstash-core/src/main/java/org/logstash/settings/SettingExt.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.logstash.settings;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
Expand All @@ -14,7 +15,12 @@
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.RubyUtil;
import org.logstash.util.CloudSettingAuth;
import org.logstash.util.CloudSettingId;
import org.logstash.util.ModulesSettingArray;
import org.logstash.util.TimeValue;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.Predicate;

Expand All @@ -37,15 +43,17 @@ public SettingExt(Ruby runtime, RubyClass metaClass) {
public SettingExt initialize(ThreadContext context, IRubyObject[] args, final Block block) {
RubyString name = (RubyString) args[0];
RubyModule clazz = (RubyModule) args[1];
Object defaultValue = null;
RubyObject defaultValue = null;
if (args.length >= 3) {
defaultValue = args[2];
defaultValue = (RubyObject) args[2];
}
boolean strict = true;
if (args.length >= 4) {
strict = args[3].toJava(Boolean.class);
}

System.out.println("DNADBG >> creating setting for " + name.asJavaString());
final Class<?> javaClass = rubyClassToJava(clazz);
if (block.isGiven()) {
Predicate<Object> validator = new Predicate<Object>() {
@Override
Expand All @@ -54,13 +62,55 @@ public boolean test(Object o) {
}
};

setting = new Setting(name.asJavaString(), clazz.getJavaClass(), defaultValue, strict, validator);
setting = new Setting(name.asJavaString(), javaClass, convertTo(defaultValue, javaClass), strict, validator);
} else {
setting = new Setting(name.asJavaString(), clazz.getJavaClass(), defaultValue, strict);
final Object coercedValue = convertTo(defaultValue, javaClass);
setting = new Setting(name.asJavaString(), javaClass, coercedValue, strict);
}
return this;
}

@SuppressWarnings({"rawtypes", "unchecked"})
private Object convertTo(RubyObject defaultValue, Class<?> javaClass) {
if (defaultValue instanceof RubyArray) {
final RubyArray castedValue = (RubyArray) defaultValue;
return new ArrayList(castedValue);
}
if (TimeValue.class.equals(javaClass)) {
return TimeValue.fromValue(defaultValue.asJavaString());
}
return defaultValue.toJava(javaClass);
}

private Class<?> rubyClassToJava(RubyModule rclass) {
if ("String".equals(rclass.getName())) {
return String.class;
}
if ("Array".equals(rclass.getName())) {
return ArrayList.class;
}
if ("Java::OrgLogstashUtil::ModulesSettingArray".equals(rclass.getName())) {
return ModulesSettingArray.class;
}
if ("Java::OrgLogstashUtil::CloudSettingId".equals(rclass.getName())) {
return CloudSettingId.class;
}
if ("Java::OrgLogstashUtil::CloudSettingAuth".equals(rclass.getName())) {
return CloudSettingAuth.class;
}
if ("Java::OrgLogstashUtil::TimeValue".equals(rclass.getName())) {
return TimeValue.class;
}
// this cover Boolean (Ruby doesn't have a class for it) and StringCoercible
if ("Object".equals(rclass.getName())) {
return Object.class;
}
if ("Integer".equals(rclass.getName())) {
return Integer.class;
}
throw new IllegalArgumentException("Cannot find matching Java class for: " + rclass.getName());
}

@JRubyMethod
public IRubyObject value() {
return (RubyObject) setting.getValue();
Expand Down Expand Up @@ -114,7 +164,7 @@ public IRubyObject validateValue() {

@JRubyMethod(visibility = Visibility.PROTECTED)
public IRubyObject validate(IRubyObject input) {
setting.validate(input);
setting.validate(input.toJava(setting.klass));
return null; // TODO
}
}

0 comments on commit db55924

Please sign in to comment.