Skip to content

Commit c8f5a21

Browse files
committed
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í <[email protected]> Change-Id: Ib0eb8680fa7c4384cecad3ba6ab0e9e862690b91 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199811 Reviewed-by: Marcel van Lohuizen <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent d52859e commit c8f5a21

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

encoding/yaml/yaml.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"cuelang.org/go/cue"
2424
"cuelang.org/go/cue/ast"
2525
cueyaml "cuelang.org/go/internal/encoding/yaml"
26-
"cuelang.org/go/internal/pkg"
2726
"cuelang.org/go/internal/source"
2827
pkgyaml "cuelang.org/go/pkg/encoding/yaml"
2928
)
@@ -110,6 +109,6 @@ func EncodeStream(iter cue.Iterator) ([]byte, error) {
110109
// Validate validates the YAML and confirms it matches the constraints
111110
// specified by v. For YAML streams, all values must match v.
112111
func Validate(b []byte, v cue.Value) error {
113-
_, err := pkgyaml.Validate(b, pkg.Schema(v))
112+
_, err := pkgyaml.Validate(b, v)
114113
return err
115114
}

internal/pkg/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *CallCtxt) Do() bool {
5151

5252
// Schema returns the ith argument as is, without converting it to a cue.Value.
5353
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])
5555
}
5656

5757
// Value returns a finalized cue.Value for the ith argument.

internal/pkg/types.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ import (
2121

2222
// A Schema represents an arbitrary cue.Value that can hold non-concrete values.
2323
// 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
3125

3226
// List represents a CUE list, which can be open or closed.
3327
type List struct {

pkg/encoding/json/manual.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func Unmarshal(b []byte) (ast.Expr, error) {
132132
// Validate validates JSON and confirms it matches the constraints
133133
// specified by v.
134134
func Validate(b []byte, v pkg.Schema) (bool, error) {
135-
err := cuejson.Validate(b, v.Value())
135+
err := cuejson.Validate(b, v)
136136
if err != nil {
137137
return false, err
138138
}

pkg/encoding/yaml/manual.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ func UnmarshalStream(data []byte) (ast.Expr, error) {
8686

8787
// Validate validates YAML and confirms it is an instance of schema.
8888
// 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) {
9090
d := cueyaml.NewDecoder("yaml.Validate", b)
91-
v := schema.Value()
9291
r := v.Context()
9392
for {
9493
expr, err := d.Decode()
@@ -133,9 +132,8 @@ func Validate(b []byte, schema pkg.Schema) (bool, error) {
133132
// specified by v using unification. This means that b must be consistent with,
134133
// but does not have to be an instance of v. If the YAML source is a stream,
135134
// 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) {
137136
d := cueyaml.NewDecoder("yaml.ValidatePartial", b)
138-
v := schema.Value()
139137
r := v.Context()
140138
for {
141139
expr, err := d.Decode()

pkg/encoding/yaml/pkg.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gen.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
//go:build ignore
1616

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+
1724
// gen.go generates the pkg.go files inside the packages under the pkg directory.
1825
//
1926
// It takes the list of packages from the packages.txt.
@@ -106,7 +113,7 @@ func main() {
106113
packagesList = append(packagesList, path.Join(pkgParent, pkg))
107114
}
108115

109-
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes}
116+
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes | packages.NeedSyntax}
110117
pkgs, err := packages.Load(cfg, packagesList...)
111118
if err != nil {
112119
fmt.Fprintf(os.Stderr, "load: %v\n", err)
@@ -365,31 +372,28 @@ func (g *generator) genFunc(fn *types.Func) {
365372
// TODO(mvdan): goKind and goToCUE still use a lot of strings; simplify.
366373

367374
func (g *generator) goKind(typ types.Type) string {
368-
if ptr, ok := typ.(*types.Pointer); ok {
369-
typ = ptr.Elem()
370-
}
371375
switch str := types.TypeString(typ, nil); str {
372-
case "math/big.Int":
376+
case "*math/big.Int":
373377
return "bigInt"
374-
case "math/big.Float":
378+
case "*math/big.Float":
375379
return "bigFloat"
376-
case "math/big.Rat":
380+
case "*math/big.Rat":
377381
return "bigRat"
378382
case "cuelang.org/go/internal/core/adt.Bottom":
379383
return "error"
380-
case "github.com/cockroachdb/apd/v3.Decimal":
384+
case "*cuelang.org/go/internal.Decimal":
381385
return "decimal"
382386
case "cuelang.org/go/internal/pkg.List":
383387
return "cueList"
384388
case "cuelang.org/go/internal/pkg.Struct":
385389
return "struct"
386-
case "cuelang.org/go/internal/pkg.Schema":
387-
g.nonConcrete = true
388-
return "schema"
389-
case "[]*github.com/cockroachdb/apd/v3.Decimal":
390+
case "[]*cuelang.org/go/internal.Decimal":
390391
return "decimalList"
391392
case "cuelang.org/go/cue.Value":
392393
return "value"
394+
case "cuelang.org/go/internal/pkg.Schema":
395+
g.nonConcrete = true
396+
return "schema"
393397
case "cuelang.org/go/cue.List":
394398
return "list"
395399
case "[]string":

0 commit comments

Comments
 (0)