this repo has no description
0
fork

Configure Feed

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

internal/pkg: make Schema an alias for cue.Value

We just need to teach pkg/gen.go to use GODEBUG=gotypesalias=1
so that it can differentiate one from the other.

Simplify the uses of pkg.Schema, as it is now an alias so it can
be used just like a cue.Value without any need for type conversions.

While here, simplify pkg/gen.go a bit by not trying to remove one level
of pointers, and instead consistently expect string matches with them.
We already had some cases like `[]*someType` we did this way.

This should also mean that any Go users who directly call pkg/...
APIs should not be broken by cue.Value parameters now being pkg.Schema,
given that the latter is simply an alias now.

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

+26 -31
+1 -2
encoding/yaml/yaml.go
··· 23 23 "cuelang.org/go/cue" 24 24 "cuelang.org/go/cue/ast" 25 25 cueyaml "cuelang.org/go/internal/encoding/yaml" 26 - "cuelang.org/go/internal/pkg" 27 26 "cuelang.org/go/internal/source" 28 27 pkgyaml "cuelang.org/go/pkg/encoding/yaml" 29 28 ) ··· 110 109 // Validate validates the YAML and confirms it matches the constraints 111 110 // specified by v. For YAML streams, all values must match v. 112 111 func Validate(b []byte, v cue.Value) error { 113 - _, err := pkgyaml.Validate(b, pkg.Schema(v)) 112 + _, err := pkgyaml.Validate(b, v) 114 113 return err 115 114 }
+1 -1
internal/pkg/context.go
··· 51 51 52 52 // Schema returns the ith argument as is, without converting it to a cue.Value. 53 53 func (c *CallCtxt) Schema(i int) Schema { 54 - return Schema(value.Make(c.ctx, c.args[i])) 54 + return value.Make(c.ctx, c.args[i]) 55 55 } 56 56 57 57 // Value returns a finalized cue.Value for the ith argument.
+1 -7
internal/pkg/types.go
··· 21 21 22 22 // A Schema represents an arbitrary cue.Value that can hold non-concrete values. 23 23 // By default function arguments are checked to be concrete. 24 - // 25 - // TODO(mvdan,mpvl): consider using type Schema = cue.Value. 26 - type Schema cue.Value 27 - 28 - func (s Schema) Value() cue.Value { 29 - return cue.Value(s) 30 - } 24 + type Schema = cue.Value 31 25 32 26 // List represents a CUE list, which can be open or closed. 33 27 type List struct {
+1 -1
pkg/encoding/json/manual.go
··· 132 132 // Validate validates JSON and confirms it matches the constraints 133 133 // specified by v. 134 134 func Validate(b []byte, v pkg.Schema) (bool, error) { 135 - err := cuejson.Validate(b, v.Value()) 135 + err := cuejson.Validate(b, v) 136 136 if err != nil { 137 137 return false, err 138 138 }
+2 -4
pkg/encoding/yaml/manual.go
··· 86 86 87 87 // Validate validates YAML and confirms it is an instance of schema. 88 88 // If the YAML source is a stream, every object must match v. 89 - func Validate(b []byte, schema pkg.Schema) (bool, error) { 89 + func Validate(b []byte, v pkg.Schema) (bool, error) { 90 90 d := cueyaml.NewDecoder("yaml.Validate", b) 91 - v := schema.Value() 92 91 r := v.Context() 93 92 for { 94 93 expr, err := d.Decode() ··· 133 132 // specified by v using unification. This means that b must be consistent with, 134 133 // but does not have to be an instance of v. If the YAML source is a stream, 135 134 // every object must match v. 136 - func ValidatePartial(b []byte, schema pkg.Schema) (bool, error) { 135 + func ValidatePartial(b []byte, v pkg.Schema) (bool, error) { 137 136 d := cueyaml.NewDecoder("yaml.ValidatePartial", b) 138 - v := schema.Value() 139 137 r := v.Context() 140 138 for { 141 139 expr, err := d.Decode()
+4 -4
pkg/encoding/yaml/pkg.go
··· 71 71 Result: adt.BoolKind, 72 72 NonConcrete: true, 73 73 Func: func(c *pkg.CallCtxt) { 74 - b, schema := c.Bytes(0), c.Schema(1) 74 + b, v := c.Bytes(0), c.Schema(1) 75 75 if c.Do() { 76 - c.Ret, c.Err = Validate(b, schema) 76 + c.Ret, c.Err = Validate(b, v) 77 77 } 78 78 }, 79 79 }, { ··· 85 85 Result: adt.BoolKind, 86 86 NonConcrete: true, 87 87 Func: func(c *pkg.CallCtxt) { 88 - b, schema := c.Bytes(0), c.Schema(1) 88 + b, v := c.Bytes(0), c.Schema(1) 89 89 if c.Do() { 90 - c.Ret, c.Err = ValidatePartial(b, schema) 90 + c.Ret, c.Err = ValidatePartial(b, v) 91 91 } 92 92 }, 93 93 }},
+16 -12
pkg/gen.go
··· 14 14 15 15 //go:build ignore 16 16 17 + // Since our go.mod still has 'go 1.22', but we want to use go/types.Alias 18 + // to differentiate cue.Value from pkg.Schema, we enable it explicitly. 19 + // TODO(mvdan): this can be removed once we bump go.mod to 'go 1.23'; 20 + // at which point packages.NeedSyntax below can be removed as well 21 + // as we no longer need to force go/packages to typecheck with our GODEBUG setting. 22 + //go:debug gotypesalias=1 23 + 17 24 // gen.go generates the pkg.go files inside the packages under the pkg directory. 18 25 // 19 26 // It takes the list of packages from the packages.txt. ··· 106 113 packagesList = append(packagesList, path.Join(pkgParent, pkg)) 107 114 } 108 115 109 - cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes} 116 + cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes | packages.NeedSyntax} 110 117 pkgs, err := packages.Load(cfg, packagesList...) 111 118 if err != nil { 112 119 fmt.Fprintf(os.Stderr, "load: %v\n", err) ··· 365 372 // TODO(mvdan): goKind and goToCUE still use a lot of strings; simplify. 366 373 367 374 func (g *generator) goKind(typ types.Type) string { 368 - if ptr, ok := typ.(*types.Pointer); ok { 369 - typ = ptr.Elem() 370 - } 371 375 switch str := types.TypeString(typ, nil); str { 372 - case "math/big.Int": 376 + case "*math/big.Int": 373 377 return "bigInt" 374 - case "math/big.Float": 378 + case "*math/big.Float": 375 379 return "bigFloat" 376 - case "math/big.Rat": 380 + case "*math/big.Rat": 377 381 return "bigRat" 378 382 case "cuelang.org/go/internal/core/adt.Bottom": 379 383 return "error" 380 - case "github.com/cockroachdb/apd/v3.Decimal": 384 + case "*cuelang.org/go/internal.Decimal": 381 385 return "decimal" 382 386 case "cuelang.org/go/internal/pkg.List": 383 387 return "cueList" 384 388 case "cuelang.org/go/internal/pkg.Struct": 385 389 return "struct" 390 + case "[]*cuelang.org/go/internal.Decimal": 391 + return "decimalList" 392 + case "cuelang.org/go/cue.Value": 393 + return "value" 386 394 case "cuelang.org/go/internal/pkg.Schema": 387 395 g.nonConcrete = true 388 396 return "schema" 389 - case "[]*github.com/cockroachdb/apd/v3.Decimal": 390 - return "decimalList" 391 - case "cuelang.org/go/cue.Value": 392 - return "value" 393 397 case "cuelang.org/go/cue.List": 394 398 return "list" 395 399 case "[]string":