filters: add comparison operators#307
Conversation
Signed-off-by: Omkar Ugalmugle <omkarugalmugle285@gmail.com>
| return false, fmt.Errorf("cannot compare %q and %q", lhs, rhs) | ||
| } | ||
|
|
||
| func parseSize(s string) (float64, error) { |
There was a problem hiding this comment.
I don't love that this package needs to include these helpers - ideally, downstream, in the cli, we'd be able to define custom handlers for comparison.
Here's my idea for that - let me know if this seems reasonable:
- Update the
Adaptor, so that it includes aCompare(any)function (returns -1 for less-than, 0 for equal, and +1 for larger than, similar tocmp.Compare. It should also return a boolean, which should be false if the comparison was invalid, e.g. you can't dorunning>stopped).- While you're there, maybe actually change
Valueto returnany. But keep aStringmethod, since we should use that for regexp comparisons.
- While you're there, maybe actually change
- Then, in
selector.Match, use theAdaptor.Compareto compare against the value for all normal comparisons. If the comparison is invalid, it doesn't match. If it turns out that the equal comparison is invalid - then just callback to comparingAdaptor.String. You should also fallback Compare<and>comparisons ofint/float/etc by usingcmp.Comparedirectly. - Finally, in the cli, is the place to actually put the custom comparators for sizes + dates + such. Probably into the custom
internal/typespackage.
Because of this, it's a bit more complicated, but means that the actual comparisons are correctly implemented in the CLI. I want to avoid CLI-specific abstractions and helpers from leaking into this more generic package.
There was a problem hiding this comment.
Hey @jedevc, just wanted to make sure I'm on the right track.
From your review, my understanding is that selector.Match should call Adaptor.Compare, and this package shouldn't contain helpers like parseSize anymore.
The only thing I'm unsure about is Compare(any). Since the parser passes comparison values as strings (like "50"), should Adaptor.Compare handle parsing them before comparing, or were you thinking of a different approach?
Just wanted to check before I continue.
Summary