Skip to content

Commit 74b3d6a

Browse files
author
Guy Boertje
committed
redo the returned object as a Struct with a to_a method.
1 parent a68195e commit 74b3d6a

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

Diff for: lib/logstash/inputs/file.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ class << self
229229

230230
def validate_value(value, validator)
231231
if validator.is_a?(Array) && validator.size == 2 && validator.first.respond_to?(:call)
232-
callable, units, val = *validator, value
233-
validation_errors = callable.call(value, units){|coerced| val = coerced}
234-
return validation_errors.nil? ? [true, val] : [false, validation_errors]
232+
callable, units = *validator
233+
# returns a ValidatedStruct having a `to_a` method suitable to return to the config mixin caller
234+
return callable.call(value, units).to_a
235235
end
236236
old_validate_value(value, validator)
237237
end

Diff for: lib/logstash/inputs/friendly_durations.rb

+17-18
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,37 @@ module FriendlyDurations
88
MEGA = 10**6
99
KILO = 10**3
1010

11-
def self.call(value, unit = "sec")
12-
val_string = value.to_s.strip
13-
result = coerce_in_seconds(val_string, unit)
14-
return result if result.is_a?(String)
15-
yield result
16-
nil
11+
ValidatedStruct = Struct.new(:value, :error_message) do
12+
def to_a
13+
error_message.nil? ? [true, value] : [false, error_message]
14+
end
1715
end
1816

19-
private
20-
21-
def self.coerce_in_seconds(value, unit)
22-
matched = NUMBERS_RE.match(value)
17+
def self.call(value, unit = "sec")
18+
# coerce into seconds
19+
val_string = value.to_s.strip
20+
matched = NUMBERS_RE.match(val_string)
2321
if matched.nil?
24-
return "Value '#{value}' is not a valid duration string e.g. 200 usec, 250ms, 60 sec, 18h, 21.5d, 1 day, 2w, 6 weeks"
22+
failed_message = "Value '#{val_string}' is not a valid duration string e.g. 200 usec, 250ms, 60 sec, 18h, 21.5d, 1 day, 2w, 6 weeks"
23+
return ValidatedStruct.new(nil, failed_message)
2524
end
2625
multiplier = matched[:units] || unit
2726
numeric = matched[:number].to_f
2827
case multiplier
2928
when "m","min","mins","minute","minutes"
30-
numeric * 60
29+
ValidatedStruct.new(numeric * 60, nil)
3130
when "h","hour","hours"
32-
numeric * HOURS
31+
ValidatedStruct.new(numeric * HOURS, nil)
3332
when "d","day","days"
34-
numeric * DAYS
33+
ValidatedStruct.new(numeric * DAYS, nil)
3534
when "w","week","weeks"
36-
numeric * 7 * DAYS
35+
ValidatedStruct.new(numeric * 7 * DAYS, nil)
3736
when "ms","msec","msecs"
38-
numeric / KILO
37+
ValidatedStruct.new(numeric / KILO, nil)
3938
when "us","usec","usecs"
40-
numeric / MEGA
39+
ValidatedStruct.new(numeric / MEGA, nil)
4140
else
42-
numeric
41+
ValidatedStruct.new(numeric, nil)
4342
end
4443
end
4544
end

Diff for: spec/inputs/friendly_durations_spec.rb

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
context "unacceptable strings" do
88
it "gives an error message for 'foobar'" do
99
result = LogStash::Inputs::FriendlyDurations.call("foobar","sec")
10-
expect(result).to start_with("Value 'foobar' is not a valid duration string e.g. 200 usec")
10+
expect(result.error_message).to start_with("Value 'foobar' is not a valid duration string e.g. 200 usec")
1111
end
1212
it "gives an error message for '5 5 days'" do
1313
result = LogStash::Inputs::FriendlyDurations.call("5 5 days","sec")
14-
expect(result).to start_with("Value '5 5 days' is not a valid duration string e.g. 200 usec")
14+
expect(result.error_message).to start_with("Value '5 5 days' is not a valid duration string e.g. 200 usec")
1515
end
1616
end
1717

1818
context "when a unit is not specified, a unit override will affect the result" do
19-
it "coerces 14 to 1209600.0 as days with a block callback" do
20-
result = LogStash::Inputs::FriendlyDurations.call(14,"d"){|value| expect(value).to eq(1209600.0)}
21-
expect(result).to eq(nil)
19+
it "coerces 14 to 1209600.0s as days" do
20+
result = LogStash::Inputs::FriendlyDurations.call(14,"d")
21+
expect(result.error_message).to eq(nil)
22+
expect(result.value).to eq(1209600.0)
2223
end
23-
it "coerces '30' to 1800 as minutes with a block callback" do
24-
result = LogStash::Inputs::FriendlyDurations.call("30","minutes"){|value| expect(value).to eq(1800.0)}
25-
expect(result).to eq(nil)
24+
it "coerces '30' to 1800.0s as minutes" do
25+
result = LogStash::Inputs::FriendlyDurations.call("30","minutes")
26+
expect(result.to_a).to eq([true, 1800.0])
2627
end
2728
end
2829

@@ -61,9 +62,9 @@
6162
["2 weeks", 1209600.0],
6263
["1.5 weeks", 907200.0],
6364
].each do |input, coerced|
64-
it "coerces #{input.inspect.rjust(16)} to #{coerced.inspect} with a block callback" do
65-
result = LogStash::Inputs::FriendlyDurations.call(input,"sec"){|value| expect(value).to eq(coerced)}
66-
expect(result).to eq(nil)
65+
it "coerces #{input.inspect.rjust(16)} to #{coerced.inspect}" do
66+
result = LogStash::Inputs::FriendlyDurations.call(input,"sec")
67+
expect(result.to_a).to eq([true, coerced])
6768
end
6869
end
6970
end

0 commit comments

Comments
 (0)