cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.09k stars 290 forks source link

spec: phase out len on string values #2438

Open mvdan opened 1 year ago

mvdan commented 1 year ago

https://cuelang.org/docs/references/spec/#len says:

Argument type    Result

string            string length in bytes
bytes             length of byte sequence

Per @mpvl, we have wanted to remove len support for strings for a while. We borrowed this behavior from Go, although it doesn't make a lot of sense in general:

We should remove len(string) from the spec, and phase out its uses in CUE code by having cue fix rewrite it to strings.ByteLen(string). If a user intended something other than the number of bytes, they could swap ByteLen for a different API like RuneLen.

cc @myitcv @mpvl

mvdan commented 1 year ago

We'll add the new APIs and the cue fix in the upcoming release. We can remove support for the old behavior at a later point in time.

mvdan commented 4 months ago

Per Marcel, add an API in the strings package called Bytes to convert a string to a byte slice.

myitcv commented 1 month ago

One thing we need to consider here is how to constrain the length of something with a validator.

We already have https://pkg.go.dev/cuelang.org/go@v0.10.0/pkg/strings#MaxRunes and MinRunes, but that to my mind is unnecessary.

Because we could have something like list.Length which is a validator, that takes a cue.Value constraint such that:

import "list"
l: list.Length(>4 & <10)
l: [1,2,3,4,5]

This would then work for constraining the list of strings in terms of runes or bytes, or indeed anything else that can be made into a list.

The only problem is that list.Length is not, to my mind, clearly a validator. len() as a builtin function already exists. But perhaps this confusion between builtins functions that are validators and regular functions (that return values) is unavoidable.