this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/adt: merge validate package with adt

The adt package will need to use it, which will
cause a cycle. We therefore merge the package.

This is part of the effort to allow == for all CUE
values.

Issue #2583

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: Id8a32fc9ae64f7855358d582de438d17a5c13e57
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1217006
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+57 -65
+2 -3
cue/types.go
··· 37 37 "cuelang.org/go/internal/core/export" 38 38 "cuelang.org/go/internal/core/runtime" 39 39 "cuelang.org/go/internal/core/subsume" 40 - "cuelang.org/go/internal/core/validate" 41 40 internaljson "cuelang.org/go/internal/encoding/json" 42 41 "cuelang.org/go/internal/types" 43 42 ) ··· 2103 2102 o := options{} 2104 2103 o.updateOptions(opts) 2105 2104 2106 - cfg := &validate.Config{ 2105 + cfg := &adt.ValidateConfig{ 2107 2106 Concrete: o.concrete, 2108 2107 Final: o.final, 2109 2108 DisallowCycles: o.disallowCycles, 2110 2109 AllErrors: true, 2111 2110 } 2112 2111 2113 - b := validate.Validate(v.ctx(), v.v, cfg) 2112 + b := adt.Validate(v.ctx(), v.v, cfg) 2114 2113 if b != nil { 2115 2114 return v.toErr(b) 2116 2115 }
+2 -3
internal/core/adt/eval_test.go
··· 34 34 "cuelang.org/go/internal/core/debug" 35 35 "cuelang.org/go/internal/core/eval" 36 36 "cuelang.org/go/internal/core/runtime" 37 - "cuelang.org/go/internal/core/validate" 38 37 "cuelang.org/go/internal/cuedebug" 39 38 "cuelang.org/go/internal/cueexperiment" 40 39 "cuelang.org/go/internal/cuetxtar" ··· 177 176 // t.Skipf("%d leaks reported", n) 178 177 // } 179 178 180 - if b := validate.Validate(ctx, v, &validate.Config{ 179 + if b := adt.Validate(ctx, v, &adt.ValidateConfig{ 181 180 AllErrors: true, 182 181 }); b != nil { 183 182 fmt.Fprintln(t, "Errors:") ··· 251 250 } 252 251 } 253 252 254 - if b := validate.Validate(ctx, v, &validate.Config{ 253 + if b := adt.Validate(ctx, v, &adt.ValidateConfig{ 255 254 AllErrors: true, 256 255 }); b != nil { 257 256 t.Log(errors.Details(b.Err, nil))
+4 -5
internal/core/compile/validator.go
··· 18 18 19 19 import ( 20 20 "cuelang.org/go/internal/core/adt" 21 - "cuelang.org/go/internal/core/validate" 22 21 ) 23 22 24 23 // matchN is a validator that checks that the number of schemas in the given ··· 50 49 var count, possibleCount int64 51 50 for _, check := range constraints { 52 51 v := adt.Unify(c, self, check) 53 - if err := validate.Validate(c, v, finalCfg); err == nil { 52 + if err := adt.Validate(c, v, finalCfg); err == nil { 54 53 // TODO: is it always true that the lack of an error signifies 55 54 // success? 56 55 count++ ··· 107 106 ifSchema, thenSchema, elseSchema := args[1], args[2], args[3] 108 107 v := adt.Unify(c, self, ifSchema) 109 108 var chosenSchema adt.Value 110 - if err := validate.Validate(c, v, finalCfg); err == nil { 109 + if err := adt.Validate(c, v, finalCfg); err == nil { 111 110 chosenSchema = thenSchema 112 111 } else { 113 112 chosenSchema = elseSchema 114 113 } 115 114 v = adt.Unify(c, self, chosenSchema) 116 - err := validate.Validate(c, v, finalCfg) 115 + err := adt.Validate(c, v, finalCfg) 117 116 if err == nil { 118 117 return &adt.Bool{B: true} 119 118 } ··· 123 122 }, 124 123 } 125 124 126 - var finalCfg = &validate.Config{Final: true} 125 + var finalCfg = &adt.ValidateConfig{Final: true} 127 126 128 127 // finalizeSelf ensures a value is fully evaluated and then strips it of any 129 128 // of its validators or default values.
+21 -25
internal/core/validate/validate.go internal/core/adt/validate.go
··· 13 13 // limitations under the License. 14 14 15 15 // Package validate collects errors from an evaluated Vertex. 16 - package validate 17 - 18 - import ( 19 - "cuelang.org/go/internal/core/adt" 20 - ) 16 + package adt 21 17 22 - type Config struct { 18 + type ValidateConfig struct { 23 19 // Concrete, if true, requires that all values be concrete. 24 20 Concrete bool 25 21 ··· 37 33 38 34 // Validate checks that a value has certain properties. The value must have 39 35 // been evaluated. 40 - func Validate(ctx *adt.OpContext, v *adt.Vertex, cfg *Config) *adt.Bottom { 36 + func Validate(ctx *OpContext, v *Vertex, cfg *ValidateConfig) *Bottom { 41 37 if cfg == nil { 42 - cfg = &Config{} 38 + cfg = &ValidateConfig{} 43 39 } 44 - x := validator{Config: *cfg, ctx: ctx} 40 + x := validator{ValidateConfig: *cfg, ctx: ctx} 45 41 x.validate(v) 46 42 return x.err 47 43 } 48 44 49 45 type validator struct { 50 - Config 51 - ctx *adt.OpContext 52 - err *adt.Bottom 46 + ValidateConfig 47 + ctx *OpContext 48 + err *Bottom 53 49 inDefinition int 54 50 55 51 // shared vertices should be visited at least once if referenced by ··· 57 53 // TODO: we could also keep track of the number of references to a 58 54 // shared vertex. This would allow us to report more than a single error 59 55 // per shared vertex. 60 - visited map[*adt.Vertex]bool 56 + visited map[*Vertex]bool 61 57 } 62 58 63 59 func (v *validator) checkConcrete() bool { ··· 68 64 return (v.Concrete || v.Final) && v.inDefinition == 0 69 65 } 70 66 71 - func (v *validator) add(b *adt.Bottom) { 67 + func (v *validator) add(b *Bottom) { 72 68 if !v.AllErrors { 73 - v.err = adt.CombineErrors(nil, v.err, b) 69 + v.err = CombineErrors(nil, v.err, b) 74 70 return 75 71 } 76 72 if !b.ChildError { 77 - v.err = adt.CombineErrors(nil, v.err, b) 73 + v.err = CombineErrors(nil, v.err, b) 78 74 } 79 75 } 80 76 81 - func (v *validator) validate(x *adt.Vertex) { 77 + func (v *validator) validate(x *Vertex) { 82 78 defer v.ctx.PopArcAndLabel(v.ctx.PushArcAndLabel(x)) 83 79 84 80 y := x ··· 94 90 return 95 91 } 96 92 if v.visited == nil { 97 - v.visited = make(map[*adt.Vertex]bool) 93 + v.visited = make(map[*Vertex]bool) 98 94 } 99 95 if v.visited[x] { 100 96 return ··· 104 100 105 101 if b := x.Bottom(); b != nil { 106 102 switch b.Code { 107 - case adt.CycleError: 103 + case CycleError: 108 104 if v.checkFinal() || v.DisallowCycles { 109 105 v.add(b) 110 106 } 111 107 112 - case adt.IncompleteError: 108 + case IncompleteError: 113 109 if v.checkFinal() { 114 110 v.add(b) 115 111 } ··· 123 119 124 120 } else if v.checkConcrete() { 125 121 x = x.Default() 126 - if !adt.IsConcrete(x) { 122 + if !IsConcrete(x) { 127 123 x := x.Value() 128 - v.add(&adt.Bottom{ 129 - Code: adt.IncompleteError, 124 + v.add(&Bottom{ 125 + Code: IncompleteError, 130 126 Err: v.ctx.Newf("incomplete value %v", x), 131 127 }) 132 128 } 133 129 } 134 130 135 131 for _, a := range x.Arcs { 136 - if a.ArcType == adt.ArcRequired && v.Final && v.inDefinition == 0 { 132 + if a.ArcType == ArcRequired && v.Final && v.inDefinition == 0 { 137 133 v.ctx.PushArcAndLabel(a) 138 - v.add(adt.NewRequiredNotPresentError(v.ctx, a)) 134 + v.add(NewRequiredNotPresentError(v.ctx, a)) 139 135 v.ctx.PopArcAndLabel(a) 140 136 continue 141 137 }
+26 -27
internal/core/validate/validate_test.go internal/core/adt/validate_test.go
··· 12 12 // See the License for the specific language governing permissions and 13 13 // limitations under the License. 14 14 15 - package validate_test 15 + package adt_test 16 16 17 17 import ( 18 18 "fmt" ··· 24 24 "cuelang.org/go/internal/core/adt" 25 25 "cuelang.org/go/internal/core/compile" 26 26 "cuelang.org/go/internal/core/eval" 27 - "cuelang.org/go/internal/core/validate" 28 27 "cuelang.org/go/internal/cuetdtest" 29 28 "github.com/google/go-cmp/cmp" 30 29 ) ··· 35 34 in string 36 35 out string 37 36 lookup string 38 - cfg *validate.Config 37 + cfg *adt.ValidateConfig 39 38 40 39 todo_v3 bool 41 40 } 42 41 testCases := []testCase{{ 43 42 name: "no error, but not concrete, even with definition label", 44 - cfg: &validate.Config{Concrete: true}, 43 + cfg: &adt.ValidateConfig{Concrete: true}, 45 44 in: ` 46 45 #foo: { use: string } 47 46 `, ··· 49 48 out: "incomplete\n#foo.use: incomplete value string:\n test:2:16", 50 49 }, { 51 50 name: "definitions not considered for completeness", 52 - cfg: &validate.Config{Concrete: true}, 51 + cfg: &adt.ValidateConfig{Concrete: true}, 53 52 in: ` 54 53 #foo: { use: string } 55 54 `, 56 55 }, { 57 56 name: "hidden fields not considered for completeness", 58 - cfg: &validate.Config{Concrete: true}, 57 + cfg: &adt.ValidateConfig{Concrete: true}, 59 58 in: ` 60 59 _foo: { use: string } 61 60 `, ··· 85 84 out: "eval\nx: conflicting values 2 and 1:\n test:2:6\n test:2:10", 86 85 }, { 87 86 name: "all errors", 88 - cfg: &validate.Config{AllErrors: true}, 87 + cfg: &adt.ValidateConfig{AllErrors: true}, 89 88 in: ` 90 89 x: 1 & 2 91 90 y: 2 & 4 ··· 99 98 test:3:10`, 100 99 }, { 101 100 name: "incomplete", 102 - cfg: &validate.Config{Concrete: true}, 101 + cfg: &adt.ValidateConfig{Concrete: true}, 103 102 in: ` 104 103 y: 2 + x 105 104 x: string ··· 113 112 `, 114 113 }, { 115 114 name: "allowed incomplete when disallowing cycles", 116 - cfg: &validate.Config{DisallowCycles: true}, 115 + cfg: &adt.ValidateConfig{DisallowCycles: true}, 117 116 in: ` 118 117 y: string 119 118 x: y ··· 123 122 todo_v3: true, 124 123 125 124 name: "disallow cycle", 126 - cfg: &validate.Config{DisallowCycles: true}, 125 + cfg: &adt.ValidateConfig{DisallowCycles: true}, 127 126 in: ` 128 127 y: x + 1 129 128 x: y - 1 ··· 134 133 todo_v3: true, 135 134 136 135 name: "disallow cycle", 137 - cfg: &validate.Config{DisallowCycles: true}, 136 + cfg: &adt.ValidateConfig{DisallowCycles: true}, 138 137 in: ` 139 138 a: b - 100 140 139 b: a + 100 ··· 142 141 out: "cycle\ncycle error:\n test:2:6", 143 142 }, { 144 143 name: "treat cycles as incomplete when not disallowing", 145 - cfg: &validate.Config{}, 144 + cfg: &adt.ValidateConfig{}, 146 145 in: ` 147 146 y: x + 1 148 147 x: y - 1 ··· 151 150 // Note: this is already handled by evaluation, as terminal errors 152 151 // are percolated up. 153 152 name: "catch most serious error", 154 - cfg: &validate.Config{Concrete: true}, 153 + cfg: &adt.ValidateConfig{Concrete: true}, 155 154 in: ` 156 155 y: string 157 156 x: 1 & 2 ··· 159 158 out: "eval\nx: conflicting values 2 and 1:\n test:3:6\n test:3:10", 160 159 }, { 161 160 name: "consider defaults for concreteness", 162 - cfg: &validate.Config{Concrete: true}, 161 + cfg: &adt.ValidateConfig{Concrete: true}, 163 162 in: ` 164 163 x: *1 | 2 165 164 `, 166 165 }, { 167 166 name: "allow non-concrete in definitions in concrete mode", 168 - cfg: &validate.Config{Concrete: true}, 167 + cfg: &adt.ValidateConfig{Concrete: true}, 169 168 in: ` 170 169 x: 2 171 170 #d: { ··· 175 174 `, 176 175 }, { 177 176 name: "pick up non-concrete value in default", 178 - cfg: &validate.Config{Concrete: true}, 177 + cfg: &adt.ValidateConfig{Concrete: true}, 179 178 in: ` 180 179 x: null | *{ 181 180 a: int ··· 184 183 out: "incomplete\nx.a: incomplete value int:\n test:3:7", 185 184 }, { 186 185 name: "pick up non-concrete value in default", 187 - cfg: &validate.Config{Concrete: true}, 186 + cfg: &adt.ValidateConfig{Concrete: true}, 188 187 in: ` 189 188 x: null | *{ 190 189 a: 1 | 2 ··· 193 192 out: "incomplete\nx.a: incomplete value 1 | 2", 194 193 }, { 195 194 name: "required field not present", 196 - cfg: &validate.Config{Final: true}, 195 + cfg: &adt.ValidateConfig{Final: true}, 197 196 in: ` 198 197 Person: { 199 198 name!: string ··· 204 203 out: "incomplete\nPerson.name: field is required but not present:\n test:3:5", 205 204 }, { 206 205 name: "allow required fields in definitions", 207 - cfg: &validate.Config{Concrete: true}, 206 + cfg: &adt.ValidateConfig{Concrete: true}, 208 207 in: ` 209 208 #Person: { 210 209 name!: string ··· 223 222 out: "", 224 223 }, { 225 224 name: "indirect resolved disjunction using matchN", 226 - cfg: &validate.Config{Final: true}, 225 + cfg: &adt.ValidateConfig{Final: true}, 227 226 in: ` 228 227 x: {} 229 228 x: matchN(0, [bool | {x!: _}]) ··· 231 230 out: "", 232 231 }, { 233 232 name: "indirect resolved disjunction", 234 - cfg: &validate.Config{Final: true}, 233 + cfg: &adt.ValidateConfig{Final: true}, 235 234 in: ` 236 235 x: {bar: 2} 237 236 x: string | {foo!: string} ··· 239 238 out: "incomplete\nx.foo: field is required but not present:\n test:3:18", 240 239 }, { 241 240 name: "disallow incomplete error with final", 242 - cfg: &validate.Config{Final: true}, 241 + cfg: &adt.ValidateConfig{Final: true}, 243 242 in: ` 244 243 x: y + 1 245 244 y: int ··· 247 246 out: "incomplete\nx: non-concrete value int in operand to +:\n test:2:7\n test:3:7", 248 247 }, { 249 248 name: "allow incomplete error with final while in definition", 250 - cfg: &validate.Config{Final: true}, 249 + cfg: &adt.ValidateConfig{Final: true}, 251 250 in: ` 252 251 #D: x: #D.y + 1 253 252 #D: y: int 254 253 `, 255 254 }, { 256 255 name: "allow incomplete error with final while in definition", 257 - cfg: &validate.Config{Final: true}, 256 + cfg: &adt.ValidateConfig{Final: true}, 258 257 in: ` 259 258 #D: x: #D.y + 1 260 259 #D: y: int 261 260 `, 262 261 }, { 263 262 name: "report non-concrete value of structure shared node in correct position", 264 - cfg: &validate.Config{ 263 + cfg: &adt.ValidateConfig{ 265 264 Concrete: true, 266 265 Final: true, 267 266 }, ··· 275 274 }, { 276 275 // Issue #3864: issue resulting from structure sharing. 277 276 name: "attribute incomplete values in definitions to concrete path", 278 - cfg: &validate.Config{ 277 + cfg: &adt.ValidateConfig{ 279 278 Concrete: true, 280 279 Final: true, 281 280 }, ··· 313 312 v = v.Lookup(adt.MakeIdentLabel(r, tc.lookup, "main")) 314 313 } 315 314 316 - b := validate.Validate(ctx, v, tc.cfg) 315 + b := adt.Validate(ctx, v, tc.cfg) 317 316 318 317 w := &strings.Builder{} 319 318 if b != nil {
+2 -2
pkg/internal/builtintest/testing.go
··· 19 19 "testing" 20 20 21 21 "cuelang.org/go/cue/format" 22 + "cuelang.org/go/internal/core/adt" 22 23 "cuelang.org/go/internal/core/eval" 23 24 "cuelang.org/go/internal/core/export" 24 - "cuelang.org/go/internal/core/validate" 25 25 "cuelang.org/go/internal/cuetdtest" 26 26 "cuelang.org/go/internal/cuetxtar" 27 27 ) ··· 46 46 ctx := e.NewContext(v) 47 47 v.Finalize(ctx) 48 48 49 - if b := validate.Validate(ctx, v, &validate.Config{ 49 + if b := adt.Validate(ctx, v, &adt.ValidateConfig{ 50 50 AllErrors: true, 51 51 }); b != nil { 52 52 fmt.Fprintln(t, "Errors:")