Skip to content

Add string based time duration support. #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

guyboertje
Copy link
Contributor

Fixes #187

def self.coerce_in_seconds(value, unit)
matched = NUMBERS_RE.match(value)
if matched.nil?
return "Value '#{value}' is not a valid duration string e.g. 200 usec, 250ms, 60 sec, 18h, 21.5d, 1 day, 2w, 6 weeks"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand the reason for returning a string to represent an error condition. why not just raise an exception? I think it will make the error handling code much easier to read and follow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just followed the pattern from validate_value in the config dsl mixin.
I will improve it.

@colinsurprenant
Copy link
Contributor

This is neat. I am wondering what the plan would be to make this generic and usable from other plugins?

@guyboertje
Copy link
Contributor Author

@colinsurprenant
It is neat. The Slow logger settings validations has something similar but you can't set a "units" hint to retain BWC when an number means a non-seconds value and the coerced number is in nanoseconds.

As to making it reusable, I would like to do so but it depends on what the core team do with the Java Plugin API WRT custom validation and having an interface to implement for a such a class.

@colinsurprenant
Copy link
Contributor

@guyboertje

As to making it reusable, I would like to do so but it depends on what the core team do with the Java Plugin API WRT custom validation and having an interface to implement for a such a class.

Or the generic part of could be extracted as a gem, and then it could be reused elsewhere too. I'll let you followup on this if you feel this could bring value.

So the only conceptual problem I see is that this relies on swapping implementation details and could possibly break upon upgrading core. I wonder if we could avoid this here?

@guyboertje
Copy link
Contributor Author

So there are three parts to the mechanism (same with the custom validators in jdbc_static).

  1. the alias_method and alternative method patch
  2. the validator class(es)
  3. the 'special' return type indicating pass/fail

1 Is definitely susceptible to a change in core.
2 Is immune but should probably be a jruby extension.
3 Might be immune in that it is a simple Struct. The receiver can use it as desired.

IMO, 1 need to be in the Plugin API with a validation hooks style registry.

@colinsurprenant
Copy link
Contributor

@guyboertje WDYT in opening a followup issue for this - I think we should keep tab on this and have some kind of strategy to prevent future regressions because no one realized that some plugins were depending on the validation implementation.

@guyboertje guyboertje force-pushed the feature/string_durations branch from 74b3d6a to 78651a8 Compare June 12, 2018 08:50
@guyboertje
Copy link
Contributor Author

@colinsurprenant
elastic/logstash#9733

@colinsurprenant
Copy link
Contributor

LGTM

@guyboertje guyboertje merged commit 28c0534 into logstash-plugins:master Jun 13, 2018
@guyboertje guyboertje deleted the feature/string_durations branch June 13, 2018 10:39
@fbaligand
Copy link

Hi @guyboertje

Thanks for this great enhancement !

Given that for some options, types and default values have changed, it would be great to also update documentation, I mean : docs/index.asciidoc

@fbaligand
Copy link

Sorry for my remark : I just realized it is already done in the following PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants