cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
5 stars 1 forks source link

docs/howto/ensure-struct-list-elements-contain-field-unique-value: Ensuring structs in a list contain a field with a unique value #108

Open jpluscplusm opened 3 months ago

jpluscplusm commented 3 months ago

From a slack thread: https://cuelang.slack.com/archives/CLT3ULF6C/p1708094593391999?thread_ts=1708093684.648429&cid=CLT3ULF6C (not persisted here as the thread is wider than just this doc)

! exec cue vet file.cue
-- file.cue --
people: [
    {name: "alice"},
    {name: "bob"},
    {name: "bob"},
]

_uniqueNames: {
    for i, person in people { (person.name): i }
}

... or maybe this, which allows a set of specific fields to be checked whilst ignoring others:

! exec cue vet file.cue
-- file.cue --
import "list"

people: [
    {name: "alice", someOtherField: 1},
    {name: "bob", someOtherField: 2},
    {name: "bob", someOtherOtherField: 3},
]

_uniqueNames: list.UniqueItems & [
    for e in people {name:e.name}
]

The thread this came from was perf-related, so perhaps some tests at scale on the above 2 options might be useful to help decide the approach to be documented.