this repo has no description
0
fork

Configure Feed

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

internal/cuetxtar: restrict @test(allows) to struct/list

Restrict @test(allows) to struct and list values: calling val.Allows on
a scalar produces meaningless results (always true). An error is now
reported when the directive is used on a non-struct, non-list value.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I20f11886cf29f86fad9fc5c12c518a1ad5dd9116
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1235354
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>

+28 -2
+3 -2
cue/testdata/readme.md
··· 241 241 ### `allows` — field allowance 242 242 243 243 Checks whether `val.Allows(sel)` returns the expected result for the given 244 - selector expression. 244 + selector expression. Valid on struct and list values. 245 245 246 246 ```cue 247 - openStruct: {a: 1} @test(allows, b) // open: any field allowed 247 + openStruct: {a: 1} @test(allows, b) // open: any field allowed 248 248 knownField: close({a: 1}) @test(allows, a) // closed: known field allowed 249 249 unknownField: close({a: 1}) @test(allows=false, b) // closed: unknown field denied 250 250 anyPattern: {[string]: 1} @test(allows, string) // any-string pattern allowed 251 + openList: [...] @test(allows, int) // open list: int index allowed 251 252 ``` 252 253 253 254 | Selector form | Meaning |
+5
internal/cuetxtar/inline.go
··· 1601 1601 t.Errorf("path %s: @test(allows): missing selector argument", path) 1602 1602 return 1603 1603 } 1604 + k := val.IncompleteKind() 1605 + if k != cue.StructKind && k != cue.ListKind { 1606 + t.Errorf("path %s: @test(allows): directive only valid on struct or list values, got %v", path, k) 1607 + return 1608 + } 1604 1609 var sel cue.Selector 1605 1610 switch rawSel { 1606 1611 case "string":
+20
internal/cuetxtar/inline_test.go
··· 594 594 } 595 595 }) 596 596 597 + t.Run("allows errors on scalar value", func(t *testing.T) { 598 + val := ctx.CompileString("x: 42") 599 + rec := &failCapture{TB: t} 600 + pa := parsedTestAttr{directive: "allows", raw: internal.ParseAttr(&ast.Attribute{Text: "@test(allows, foo)"})} 601 + r.runAllowsAssertion(rec, path, val.LookupPath(path), pa) 602 + if !rec.failed { 603 + t.Errorf("expected failure: @test(allows) should error on scalar value") 604 + } 605 + }) 606 + 607 + t.Run("allows passes for AnyIndex on open list", func(t *testing.T) { 608 + val := ctx.CompileString("x: [...]") 609 + rec := &failCapture{TB: t} 610 + pa := parsedTestAttr{directive: "allows", raw: internal.ParseAttr(&ast.Attribute{Text: "@test(allows, int)"})} 611 + r.runAllowsAssertion(rec, path, val.LookupPath(path), pa) 612 + if rec.failed { 613 + t.Errorf("unexpected failure: open list should allow int indices\n%s", rec.msgs.String()) 614 + } 615 + }) 616 + 597 617 t.Run("allows passes for open struct", func(t *testing.T) { 598 618 val := ctx.CompileString("x: {a: 1}") 599 619 rec := &failCapture{TB: t}