Skip to content

Commit

Permalink
internal/core/adt: hoist unifyValidator
Browse files Browse the repository at this point in the history
We will make various changes to it for which it needs
to be in the adt package.

We also rename it to Unify, as this is pretty much
what it is. We may even have cue.Unify call into this
function eventually.

Issue #3649

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: Ie7c297f203e6446d23555e30b77c5bb513ef4564
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1208002
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mpvl committed Jan 30, 2025
1 parent bd087f6 commit 7fd7f6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 10 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,16 @@ func (v *Vertex) Bottom() *Bottom {

// func (v *Vertex) Evaluate()

// Unify unifies two values and returns the result.
func Unify(c *OpContext, a, b Value) *Vertex {
v := &Vertex{}
closeInfo := c.CloseInfo()
v.AddConjunct(MakeConjunct(nil, a, closeInfo))
v.AddConjunct(MakeConjunct(nil, b, closeInfo))
v.Finalize(c)
return v
}

func (v *Vertex) Finalize(c *OpContext) {
// Saving and restoring the error context prevents v from panicking in
// case the caller did not handle existing errors in the context.
Expand Down
11 changes: 6 additions & 5 deletions internal/core/compile/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var matchNBuiltin = &adt.Builtin{

var count, possibleCount int64
for _, check := range constraints {
v := unifyValidator(c, self, check)
v := adt.Unify(c, self, check)
if err := validate.Validate(c, v, finalCfg); err == nil {
// TODO: is it always true that the lack of an error signifies
// success?
Expand Down Expand Up @@ -99,14 +99,14 @@ var matchIfBuiltin = &adt.Builtin{
return &adt.Bool{B: false}
}
ifSchema, thenSchema, elseSchema := args[1], args[2], args[3]
v := unifyValidator(c, self, ifSchema)
v := adt.Unify(c, self, ifSchema)
var chosenSchema adt.Value
if err := validate.Validate(c, v, finalCfg); err == nil {
chosenSchema = thenSchema
} else {
chosenSchema = elseSchema
}
v = unifyValidator(c, self, chosenSchema)
v = adt.Unify(c, self, chosenSchema)
err := validate.Validate(c, v, finalCfg)
if err == nil {
return &adt.Bool{B: true}
Expand All @@ -128,7 +128,8 @@ func finalizeSelf(c *adt.OpContext, self adt.Value) adt.Value {
return self
}

func unifyValidator(c *adt.OpContext, self, check adt.Value) *adt.Vertex {
// TODO: use adt.Unify instead.
func unifyScalar(c *adt.OpContext, self, check adt.Value) *adt.Vertex {
v := &adt.Vertex{}
closeInfo := c.CloseInfo()
v.AddConjunct(adt.MakeConjunct(nil, self, closeInfo))
Expand All @@ -139,7 +140,7 @@ func unifyValidator(c *adt.OpContext, self, check adt.Value) *adt.Vertex {

func checkNum(ctx *adt.OpContext, bound adt.Value, count, maxCount int64) *adt.Bottom {
cnt := ctx.NewInt64(count)
n := unifyValidator(ctx, bound, cnt)
n := unifyScalar(ctx, bound, cnt)
b, _ := n.BaseValue.(*adt.Bottom)
if b != nil {
b := ctx.NewErrf("%d matched, expected %v", count, bound)
Expand Down

0 comments on commit 7fd7f6d

Please sign in to comment.