Skip to content

Commit

Permalink
internal/pkg: make Schema an alias for cue.Value
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Aug 21, 2024
1 parent d52859e commit c8f5a21
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 31 deletions.
3 changes: 1 addition & 2 deletions encoding/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
cueyaml "cuelang.org/go/internal/encoding/yaml"
"cuelang.org/go/internal/pkg"
"cuelang.org/go/internal/source"
pkgyaml "cuelang.org/go/pkg/encoding/yaml"
)
Expand Down Expand Up @@ -110,6 +109,6 @@ func EncodeStream(iter cue.Iterator) ([]byte, error) {
// Validate validates the YAML and confirms it matches the constraints
// specified by v. For YAML streams, all values must match v.
func Validate(b []byte, v cue.Value) error {
_, err := pkgyaml.Validate(b, pkg.Schema(v))
_, err := pkgyaml.Validate(b, v)
return err
}
2 changes: 1 addition & 1 deletion internal/pkg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *CallCtxt) Do() bool {

// Schema returns the ith argument as is, without converting it to a cue.Value.
func (c *CallCtxt) Schema(i int) Schema {
return Schema(value.Make(c.ctx, c.args[i]))
return value.Make(c.ctx, c.args[i])
}

// Value returns a finalized cue.Value for the ith argument.
Expand Down
8 changes: 1 addition & 7 deletions internal/pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ import (

// A Schema represents an arbitrary cue.Value that can hold non-concrete values.
// By default function arguments are checked to be concrete.
//
// TODO(mvdan,mpvl): consider using type Schema = cue.Value.
type Schema cue.Value

func (s Schema) Value() cue.Value {
return cue.Value(s)
}
type Schema = cue.Value

// List represents a CUE list, which can be open or closed.
type List struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/encoding/json/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Unmarshal(b []byte) (ast.Expr, error) {
// Validate validates JSON and confirms it matches the constraints
// specified by v.
func Validate(b []byte, v pkg.Schema) (bool, error) {
err := cuejson.Validate(b, v.Value())
err := cuejson.Validate(b, v)
if err != nil {
return false, err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/encoding/yaml/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ func UnmarshalStream(data []byte) (ast.Expr, error) {

// Validate validates YAML and confirms it is an instance of schema.
// If the YAML source is a stream, every object must match v.
func Validate(b []byte, schema pkg.Schema) (bool, error) {
func Validate(b []byte, v pkg.Schema) (bool, error) {
d := cueyaml.NewDecoder("yaml.Validate", b)
v := schema.Value()
r := v.Context()
for {
expr, err := d.Decode()
Expand Down Expand Up @@ -133,9 +132,8 @@ func Validate(b []byte, schema pkg.Schema) (bool, error) {
// specified by v using unification. This means that b must be consistent with,
// but does not have to be an instance of v. If the YAML source is a stream,
// every object must match v.
func ValidatePartial(b []byte, schema pkg.Schema) (bool, error) {
func ValidatePartial(b []byte, v pkg.Schema) (bool, error) {
d := cueyaml.NewDecoder("yaml.ValidatePartial", b)
v := schema.Value()
r := v.Context()
for {
expr, err := d.Decode()
Expand Down
8 changes: 4 additions & 4 deletions pkg/encoding/yaml/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions pkg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

//go:build ignore

// Since our go.mod still has 'go 1.22', but we want to use go/types.Alias
// to differentiate cue.Value from pkg.Schema, we enable it explicitly.
// TODO(mvdan): this can be removed once we bump go.mod to 'go 1.23';
// at which point packages.NeedSyntax below can be removed as well
// as we no longer need to force go/packages to typecheck with our GODEBUG setting.
//go:debug gotypesalias=1

// gen.go generates the pkg.go files inside the packages under the pkg directory.
//
// It takes the list of packages from the packages.txt.
Expand Down Expand Up @@ -106,7 +113,7 @@ func main() {
packagesList = append(packagesList, path.Join(pkgParent, pkg))
}

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

func (g *generator) goKind(typ types.Type) string {
if ptr, ok := typ.(*types.Pointer); ok {
typ = ptr.Elem()
}
switch str := types.TypeString(typ, nil); str {
case "math/big.Int":
case "*math/big.Int":
return "bigInt"
case "math/big.Float":
case "*math/big.Float":
return "bigFloat"
case "math/big.Rat":
case "*math/big.Rat":
return "bigRat"
case "cuelang.org/go/internal/core/adt.Bottom":
return "error"
case "github.com/cockroachdb/apd/v3.Decimal":
case "*cuelang.org/go/internal.Decimal":
return "decimal"
case "cuelang.org/go/internal/pkg.List":
return "cueList"
case "cuelang.org/go/internal/pkg.Struct":
return "struct"
case "cuelang.org/go/internal/pkg.Schema":
g.nonConcrete = true
return "schema"
case "[]*github.com/cockroachdb/apd/v3.Decimal":
case "[]*cuelang.org/go/internal.Decimal":
return "decimalList"
case "cuelang.org/go/cue.Value":
return "value"
case "cuelang.org/go/internal/pkg.Schema":
g.nonConcrete = true
return "schema"
case "cuelang.org/go/cue.List":
return "list"
case "[]string":
Expand Down

0 comments on commit c8f5a21

Please sign in to comment.