Skip to content
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

cue: Value API should support looking at pattern constraints on structs #3684

Open
mvdan opened this issue Jan 16, 2025 · 2 comments
Open

cue: Value API should support looking at pattern constraints on structs #3684

mvdan opened this issue Jan 16, 2025 · 2 comments
Labels

Comments

@mvdan
Copy link
Member

mvdan commented Jan 16, 2025

Given a CUE value such as:

foo: [string]: #Entry

one can grab the cue.Value for foo and do val.LookupPath(cue.MakePath(cue.AnyString)) and obtain the reference value for #Entry.

However, given:

foo: [=~"^prefix]: #Entry

it is currently not possible to do the same; a lookup of "any string" returns an error because any string does not match the constraint.

In the more general case, it's impossible to inspect a CUE value such as

foo: {
    [string]: #SchemaAnyString
    [=~"^x/"]: #SchemaX
    [=~"^y/"]: #SchemaY

    [int]: #SchemaAnyInt
}

and look at the pattern constraints being applied via the public Go API. Note that some may overlap, for example foo: "x/one": {} would unify with #SchemaAnyString & #SchemaX.

Also note that, per the spec, default constraints like foo: {...string} could also apply, but are not currently implemented.

One idea could be:

type PatternConstraint struct {
    Pattern    Value
    Constraint Value
}

func (Value) Patterns() ([]PatternConstraint, error)

Marcel also briefly mentioned in a recent exchange that he had been toying with the idea of adapting the existing struct field iterator API (https://pkg.go.dev/cuelang.org/go/cue#Value.Fields) to support pattern constraints as well. For that to work, we might need two new APIs to enable iterating over pattern constraints, and then seeing their pattern values, given that Iterator.Selector is not enough:

func Patterns(include bool) Option

func (*Iterator) Pattern() Value
@mvdan mvdan added Triage Requires triage/attention NeedsFix and removed Triage Requires triage/attention labels Jan 16, 2025
@myitcv
Copy link
Member

myitcv commented Jan 16, 2025

Just flagging that we should also bear in mind #3312 when considering this API.

@mvdan
Copy link
Member Author

mvdan commented Jan 16, 2025

I should also note that @cuematthew mentioned that trim needed this. He ended up using the internal APIs because the public APIs were missing features like this one.

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

No branches or pull requests

2 participants