cue icon indicating copy to clipboard operation
cue copied to clipboard

Semver as a basic type like number or string

Open cueckoo opened this issue 4 years ago • 6 comments

Originally opened by @verdverm in https://github.com/cuelang/cue/issues/407

Configuration is often dealing with versions and there is not an easy way to compare version identifiers during validation and evaluation.

Having a semver basic type (not sure the right term here) like number and string.

schema: {
  version: semver
}

foo: schema & {
  version: v0.3.4
}

range: {
  version: >v0.3.0 & < v1.0.0
}

fail: {
   version: >v0.4.0
}

ex1: foo & range // => foo
ex2: foo & config // => _|_

This seems like a good library to use: https://pkg.go.dev/golang.org/x/[email protected]/semver

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @rvadhavk in https://github.com/cuelang/cue/issues/407#issuecomment-638372642

I don't think this should be a bulitin. It'd be better to have a generic method of creating user-defined types with these semantics. Like maybe define the comparison operator on lists be a lexicographic ordering.

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @rvadhavk in https://github.com/cuelang/cue/issues/407#issuecomment-638656581

Another similar problem is expressing date and time ranges, which I don't think is currently possible in cue (unless you just using unix epoch seconds or something).

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @verdverm in https://github.com/cuelang/cue/issues/407#issuecomment-639001241

I think you are right, so I'll update this to be semver as a built in package in Cue.

See the 'pkg/**' packages available. There are things for dates, ips, and more. Semver comparison can likely fit here via functions.

I'm not sure there is a path for user defined types which can be used in builin operations like < and other Boolean ops. @mpvl ?

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @myitcv in https://github.com/cuelang/cue/issues/407#issuecomment-639254536

The simplicity of not allowing re-definition of < and friends is, I think, a massive benefit that outweighs the cost of complexity, readability that allowing it would introduce.

Instead, this situation (and many others) sound like a use case for the previously proposed constrain() build in.

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @mpvl in https://github.com/cuelang/cue/issues/407#issuecomment-760496843

@rvadhavk: time.Time enforces a proper string type and you can just use string comparison for ordering. There is no need to redefine <, it is just regular string ordering.

Semver can easily be expressed as a fairly simple regexp. The only thing I can imagine that would be useful is numeric sorting (e.g. using x/text/collate). Again, this can be solved with a more generic package. I don't think it warrants a builtin package. It can easily be provided as a native CUE package externally. I don't think semver is at the same level of standardization as ip or dates.

cueckoo avatar Jul 03 '21 10:07 cueckoo

Semver can easily be expressed as a fairly simple regexp.

As regular expressions go, it's possible, but I wouldn't say that it was entirely trivial:

^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

The only thing I can imagine that would be useful is numeric sorting (e.g. using x/text/collate). Again, this can be solved with a more generic package.

Unfortunately AFAICS there's no way to make that kind of sorting work for semver strings, because of prerelease versions, which order:

v.1.2.3-alpha1

before:

v1.2.3

even though most collation algorithms would order them the other way around. @mpvl have you got an idea for a way to solve that in a generic package?

I don't think that semver should be a basic type, but I do think there's room for a semver package in the standard library. It's a common enough problem in configuration to use semver that I think its presence would be justified.

rogpeppe avatar Sep 02 '22 14:09 rogpeppe