1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compile
// This file contains validator and other non-monotonic builtins.
import (
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/validate"
)
// matchN is a validator that checks that the number of schemas in the given
// list that unify with "self" matches the number passed as the first argument
// of the validator. Note that this number may itself be a number constraint
// and does not have to be a concrete number.
var matchNBuiltin = &adt.Builtin{
Name: "matchN",
Params: []adt.Param{topParam, intParam, listParam}, // varargs
Result: adt.BoolKind,
NonConcrete: true,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
if !c.IsValidator {
return c.NewErrf("matchN is a validator and should not be used as a function")
}
self := finalizeSelf(c, args[0])
if err := bottom(c, self); err != nil {
return &adt.Bool{B: false}
}
constraints := c.Elems(args[2])
var count, possibleCount int64
for _, check := range constraints {
v := unifyValidator(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?
count++
} else {
if err.IsIncomplete() {
possibleCount++
}
}
}
bound := args[1]
// TODO: consider a mode to require "all" to pass, for instance by
// supporting the value null or "all".
b := checkNum(c, bound, count, count+possibleCount)
if b != nil {
return b
}
return &adt.Bool{B: true}
},
}
// matchIf is a validator that checks that if the first argument unifies with
// self, the second argument also unifies with self, otherwise the third
// argument unifies with self.
// The same finalization heuristics are applied to self as are applied
// in matchN.
var matchIfBuiltin = &adt.Builtin{
Name: "matchIf",
Params: []adt.Param{topParam, topParam, topParam, topParam},
Result: adt.BoolKind,
NonConcrete: true,
Func: func(c *adt.OpContext, args []adt.Value) adt.Expr {
if !c.IsValidator {
return c.NewErrf("matchIf is a validator and should not be used as a function")
}
self := finalizeSelf(c, args[0])
if err := bottom(c, self); err != nil {
return &adt.Bool{B: false}
}
ifSchema, thenSchema, elseSchema := args[1], args[2], args[3]
v := unifyValidator(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)
err := validate.Validate(c, v, finalCfg)
if err == nil {
return &adt.Bool{B: true}
}
// TODO should we also include in the error something about the fact that
// the if condition passed or failed?
return err
},
}
var finalCfg = &validate.Config{Final: true}
// finalizeSelf ensures a value is fully evaluated and then strips it of any
// of its validators or default values.
func finalizeSelf(c *adt.OpContext, self adt.Value) adt.Value {
if x, ok := self.(*adt.Vertex); ok {
self = x.ToDataAll(c)
}
return self
}
func unifyValidator(c *adt.OpContext, self, check adt.Value) *adt.Vertex {
v := &adt.Vertex{}
closeInfo := c.CloseInfo()
v.AddConjunct(adt.MakeConjunct(nil, self, closeInfo))
v.AddConjunct(adt.MakeConjunct(nil, check, closeInfo))
v.Finalize(c)
return v
}
func checkNum(ctx *adt.OpContext, bound adt.Value, count, maxCount int64) *adt.Bottom {
cnt := ctx.NewInt64(count)
n := unifyValidator(ctx, bound, cnt)
b, _ := n.BaseValue.(*adt.Bottom)
if b != nil {
b := ctx.NewErrf("%d matched, expected %v", count, bound)
// By default we should mark the error as incomplete, but check if we
// know for sure it will fail.
switch bound := bound.(type) {
case *adt.Num:
if i, err := bound.X.Int64(); err == nil && i > count && i <= maxCount {
b.Code = adt.IncompleteError
}
case *adt.BoundValue:
v := &adt.Vertex{}
v.AddConjunct(ctx.MakeConjunct(bound))
v.AddConjunct(ctx.MakeConjunct(&adt.BoundValue{
Op: adt.GreaterEqualOp,
Value: cnt,
}))
v.AddConjunct(ctx.MakeConjunct(&adt.BoundValue{
Op: adt.LessEqualOp,
Value: ctx.NewInt64(maxCount),
}))
v.Finalize(ctx)
if _, ok := v.BaseValue.(*adt.Bottom); !ok {
b.Code = adt.IncompleteError
}
default:
b.Code = adt.IncompleteError
}
return b
}
return nil
}
func bottom(c *adt.OpContext, v adt.Value) *adt.Bottom {
switch x := v.(type) {
case *adt.Vertex:
return x.Err(c)
case *adt.Bottom:
return x
}
return nil
}
|