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.01k stars 284 forks source link

pkg/structs: support ToDefaults() #576

Open cueckoo opened 3 years ago

cueckoo commented 3 years ago

Originally opened by @myitcv in https://github.com/cuelang/cue/issues/576

Is your feature request related to a problem? Please describe.

Consider:

#Person: {
    Name: string
    Address: string
    Age: int
}

x: #Person & {
    Name: "Gopher"
    Address: "Gopher Land"
    Age: 42
}

Now consider how we would declare y such that it should be a #Person with default values taken from those of x:

y: #Person & {
    Name: *x.Name | _
    Address: *x.Address | _
    Age: *x.Age | _
}

i.e. we end up repeating the structure of #Person.

Describe the solution you'd like

Something like structs.ToDefault(v cue.Value) which takes a struct value (closed or otherwise) and returns a struct value res where for each field x on v, res: x: *v.x | _.

Questions to answer:

Describe alternatives you've considered

As above, writing everything out "by hand"

Additional context

n/a

myitcv commented 3 years ago

Whilst the use case behind this felt compelling at the time, I really haven't found another use case yet. Which is perhaps telling, so closing this for now.

myitcv commented 1 year ago

Re-opening per an exchange with @rogpeppe and @sdboyer on Slack. Not because the approach above is what we need/want, but as a place for further discussion about concrete examples, designs etc.