zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
445 stars 54 forks source link

Semantic version comparison #7

Closed Archetypically closed 3 years ago

Archetypically commented 3 years ago

Hi @zeroSteiner ! Love the library, I've used it in a couple of personal projects.

Wondering if there's any thought or TODO item regarding semantic version comparison? Only the numbers, ignoring the label extensions (like -beta or -rc).

This can be done using using pure python:

(forgive my hasty two line example)

>>> [int(x) for x in "1.0.0".split(".")] <= [int(x) for x in "1.0.1".split(".")]
True
>>> [int(x) for x in "1.1.0".split(".")] <= [int(x) for x in "1.0.1".split(".")]
False

or using packaging.version.parse which is packaged with setuptools.

I believe I can accomplish the same goal using custom resolvers (which I am planning on doing for my current project), but am curious as to the status of a first-class solution.

zeroSteiner commented 3 years ago

No I hadn't considered a first class solution for semantic version strings. It kind of seems like a pretty specific use case. The easiest thing might be to support comparisons of arrays so you could do [1, 0, 1] < [2, 0] which is supported by Python but not exposed through Rule Engine. I hadn't noticed until just now but you can't even do equality comparisons on arrays at this time (which are still pretty new). Issue with this solution is you'd need to convert your semantic version to an array of integers though.

Archetypically commented 3 years ago

That sounds like a reasonable, more widely applicable solution. I think needing to split my semver into an array of ints, then pass that input to rule-engine is totally acceptable, as long as the array comparison is supported by rule-engine. If I could +1 a to-do item then, that would totally satisfy my use case.

zeroSteiner commented 3 years ago

I'll see what I can do but it may not happen until November because October is Hacktoberfest which will take most of my FOSS dev time. Thanks for the suggestion!

zeroSteiner commented 3 years ago

This is done and available for testing in the feat/v2.3 branch. It'll be in the 2.3 version release which I'll probably finalize and publish within the next 1-3 weeks.

In [1]: import rule_engine; rule_engine.Rule('version > [1, 0, 0]').matches({'version': tuple(map(int, '1.2.0'.split('.')))})                                                                                                                 
Out[1]: True
zeroSteiner commented 3 years ago

This has been completed in version 2.3.0 which has now been published.