Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: allow raw subsumption, taking defaults into account
Browse files Browse the repository at this point in the history
Change-Id: I2046b8967147dd8383bffb45add57a0ad553898e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9262
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 2, 2021
1 parent 3701bef commit 1ea47e0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
15 changes: 10 additions & 5 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1717,11 +1717,14 @@ func (v Value) Template() func(label string) Value {
// Without options, the entire value is considered for assumption, which means
// Subsume tests whether v is a backwards compatible (newer) API version of w.
//
// Use the Final option to check subsumption if a w is known to be final,
// and should assumed to be closed.
// Use the Final option to check subsumption if a w is known to be final, and
// should assumed to be closed.
//
// Value v and w must be obtained from the same build.
// TODO: remove this requirement.
// Use the Raw option to do a low-level subsumption, taking defaults into
// account.
//
// Value v and w must be obtained from the same build. TODO: remove this
// requirement.
func (v Value) Subsume(w Value, opts ...Option) error {
o := getOptions(opts)
p := subsume.CUE
Expand All @@ -1733,7 +1736,9 @@ func (v Value) Subsume(w Value, opts ...Option) error {
case o.ignoreClosedness:
p = subsume.API
}
p.Defaults = true
if !o.raw {
p.Defaults = true
}
ctx := v.ctx().opCtx
return p.Value(ctx, v.v, w.v)
}
Expand Down
36 changes: 36 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,42 @@ func TestSubsume(t *testing.T) {
pathB: b,
options: []Option{Final()},
want: true,
}, {
// default
value: `
a: <5
b: *3 | int
`,
pathA: a,
pathB: b,
want: true,
}, {
// Disable default elimination.
value: `
a: <5
b: *3 | int
`,
pathA: a,
pathB: b,
options: []Option{Raw()},
want: false,
}, {
value: `
#A: {
exact: string
} | {
regex: string
}
#B: {
exact: string
} | {
regex: string
}
`,
pathA: ParsePath("#A"),
pathB: ParsePath("#B"),
options: []Option{},
want: true,
}}
for _, tc := range testCases {
t.Run(tc.value, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/subsume/subsume.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *Profile) Value(ctx *adt.OpContext, a, b adt.Value) errors.Error {
if !s.values(a, b) {
return s.getError()
}
return s.errs
return nil // ignore errors here even if there are some.
}

// Check reports whether b is an instance of a.
Expand Down

0 comments on commit 1ea47e0

Please sign in to comment.