From 5b770cdbf697df0ac273e7c754c401dc4375f2d2 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Tue, 29 Dec 2020 22:41:11 -0500 Subject: [PATCH] cmd/cue: embed packages required for get go checking 'cue get go' loads interface types from cmd/cue/cmd/interfaces during initialisation. Go types that satisfy these interface types are then used as special cases for the target CUE definition types - see the complete logic in (*extractor).altType. Given the nature of 'cue get go', it is assumed that the command is run in the context of a Go module. That is to say, 'go env GOMOD' will be non-empty. (It doesn't seem worth adding GOPATH support given that modules will be GA in Go 1.16) Currently, 'cue get go' assumes that the package cuelang.org/go/cmd/cue/cmd/interfaces will be available. That is to say, either cuelang.org/go is the main module or, more likely, that cuelang.org/go is a module dependency of the main module. However, this is an unnecessary restriction and one that is potentially incorrect. After all, the user is using a specific version of cmd/cue - loading a different version of cuelang.org/go seems likely to lead to version-skew related problems. This change switches cmd/cue to instead load cuelang.org/go/cmd/cue/cmd/interfaces via a simple embedding of the GoFiles in that package. As part of the same change we surface all loading and type checking errors for either the embedded cuelang.org/go/cmd/cue/cmd/interfaces or the arguments to 'cue get go'. With thanks to @verdverm and @NicolasRouquette for the analysis and suggestions that went into this fix. Drops GOPATH support. Fixes #621 Fixes #668 Change-Id: I630e2cb167c05d63186aa356eaf70a58749b61c5 --- cmd/cue/cmd/get_go.go | 94 +++- cmd/cue/cmd/get_go_test.go | 102 ---- cmd/cue/cmd/interfaces_gen.go | 10 + cmd/cue/cmd/testdata/code/go/pkg1/alias.go | 19 - cmd/cue/cmd/testdata/code/go/pkg1/file1.go | 132 ----- cmd/cue/cmd/testdata/code/go/pkg2/add.cue | 19 - cmd/cue/cmd/testdata/code/go/pkg2/pkg2.go | 48 -- cmd/cue/cmd/testdata/code/go/pkg3/pkg3.go | 19 - .../testdata/mod/example.com_blah_v1.0.0.txt | 1 - .../testdata/code/go/pkg1/alias_go_gen.cue | 9 - .../testdata/code/go/pkg1/file1_go_gen.cue | 91 --- .../cue/cmd/testdata/code/go/pkg2/add_gen.cue | 23 - .../cmd/testdata/code/go/pkg2/pkg2_go_gen.cue | 31 -- .../cmd/testdata/code/go/pkg3/pkg3_go_gen.cue | 9 - cmd/cue/cmd/testdata/script/get_go_basic.txt | 60 -- cmd/cue/cmd/testdata/script/get_go_local.txt | 46 ++ .../cmd/testdata/script/get_go_non_local.txt | 54 ++ cmd/cue/cmd/testdata/script/get_go_types.txt | 520 ++++++++++++++++++ encoding/gocode/generator.go | 2 +- internal/cmd/embedpkg/embedpkg.go | 109 ++++ 20 files changed, 827 insertions(+), 571 deletions(-) delete mode 100644 cmd/cue/cmd/get_go_test.go create mode 100644 cmd/cue/cmd/interfaces_gen.go delete mode 100644 cmd/cue/cmd/testdata/code/go/pkg1/alias.go delete mode 100644 cmd/cue/cmd/testdata/code/go/pkg1/file1.go delete mode 100644 cmd/cue/cmd/testdata/code/go/pkg2/add.cue delete mode 100644 cmd/cue/cmd/testdata/code/go/pkg2/pkg2.go delete mode 100644 cmd/cue/cmd/testdata/code/go/pkg3/pkg3.go delete mode 100644 cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/alias_go_gen.cue delete mode 100644 cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue delete mode 100644 cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/add_gen.cue delete mode 100644 cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue delete mode 100644 cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3/pkg3_go_gen.cue delete mode 100644 cmd/cue/cmd/testdata/script/get_go_basic.txt create mode 100644 cmd/cue/cmd/testdata/script/get_go_local.txt create mode 100644 cmd/cue/cmd/testdata/script/get_go_non_local.txt create mode 100644 cmd/cue/cmd/testdata/script/get_go_types.txt create mode 100644 internal/cmd/embedpkg/embedpkg.go diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go index 2dbdab756..55b1e2300 100644 --- a/cmd/cue/cmd/get_go.go +++ b/cmd/cue/cmd/get_go.go @@ -281,13 +281,59 @@ func (e *extractor) usedPkg(pkg string) { e.usedPkgs[pkg] = true } -func initInterfaces() error { +const cueGoMod = ` +module cuelang.org/go +` + +//go:generate go run cuelang.org/go/internal/cmd/embedpkg cuelang.org/go/cmd/cue/cmd/interfaces + +func initInterfaces() (err error) { + // tempdir needed for overlay + tmpDir, err := ioutil.TempDir("", "cuelang") + if err != nil { + return err + } + + defer func() { + rerr := os.RemoveAll(tmpDir) + if err == nil { + err = rerr + } + }() + + // write the cuelang go.mod + err = ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(cueGoMod), 0666) + if err != nil { + return err + } + + for fn, contents := range interfacesFiles { + fn = filepath.Join(tmpDir, filepath.FromSlash(fn)) + dir := filepath.Dir(fn) + if err := os.MkdirAll(dir, 0777); err != nil { + return err + } + + if err = ioutil.WriteFile(fn, contents, 0666); err != nil { + return err + } + } + cfg := &packages.Config{ - Mode: packages.LoadAllSyntax, + Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps, + Dir: filepath.Join(tmpDir), } + p, err := packages.Load(cfg, "cuelang.org/go/cmd/cue/cmd/interfaces") if err != nil { - return err + return fmt.Errorf("error loading embedded cuelang.org/go/cmd/cue/cmd/interfaces package: %w", err) + } + if len(p[0].Errors) > 0 { + var buf bytes.Buffer + for _, e := range p[0].Errors { + fmt.Fprintf(&buf, "\t%v\n", e) + } + return fmt.Errorf("error loading embedded cuelang.org/go/cmd/cue/cmd/interfaces package:\n%s", buf.String()) } for e, tt := range p[0].TypesInfo.Types { @@ -321,6 +367,31 @@ var ( // - consider not including types with any dropped fields. func extract(cmd *Command, args []string) error { + // TODO: there are various permutations when it comes to the arguments + // to get go and the calling context. All require that the + // + // 1. the arguments to + // TODO the CUE load using "." (below) assumes that a CUE module and a Go module will + // exist within the same directory (more precisely that a Go module could be + // nested within a CUE module), such that the module path in any + // subdirectory below the current directory will be the same. This seems an + // entirely reasonable restriction, but also one that we should enforce. + // + // Enforcing this restriction also makes --local entirely redundant. + // + // TODO: GOPATH. Strictly speaking it would be better from a UX perspective + // to eargerly check that the two modules align. But because we still offer + // GOPATH support we can't know the Go module + // It would generally be + // Strictly speaking we could lazily check whether the two modules "align" + // but it seems better and more consistent to check early, by using the + // CUE and Go equivalents of loading ".", then comparing the path result. + // Note we need to do + // + // --local is required for GOPATH mode. + + // command specifies a Go package(s) that belong to the main module + // and where for some reason the // determine module root: binst := loadFromArgs(cmd, []string{"."}, nil)[0] @@ -337,12 +408,21 @@ func extract(cmd *Command, args []string) error { } cfg := &packages.Config{ - Mode: packages.LoadAllSyntax | packages.NeedModule, + Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedModule, } pkgs, err := packages.Load(cfg, args...) if err != nil { return err } + var errs []string + for _, P := range pkgs { + if len(P.Errors) > 0 { + errs = append(errs, fmt.Sprintf("\t%s: %v", P.PkgPath, P.Errors)) + } + } + if len(errs) > 0 { + return fmt.Errorf("could not load Go packages:\n%s", strings.Join(errs, "\n")) + } e := extractor{ cmd: cmd, @@ -408,7 +488,7 @@ func (e *extractor) extractPkg(root string, p *packages.Package) error { } } - if err := os.MkdirAll(dir, 0755); err != nil { + if err := os.MkdirAll(dir, 0777); err != nil { return err } @@ -481,7 +561,7 @@ func (e *extractor) extractPkg(root string, p *packages.Package) error { if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(dir, file), b, 0644) + err = ioutil.WriteFile(filepath.Join(dir, file), b, 0666) if err != nil { return err } @@ -539,7 +619,7 @@ func (e *extractor) importCUEFiles(p *packages.Package, dir, args string) error w.Write(b) dst := filepath.Join(dir, file) - if err := ioutil.WriteFile(dst, w.Bytes(), 0644); err != nil { + if err := ioutil.WriteFile(dst, w.Bytes(), 0666); err != nil { return err } } diff --git a/cmd/cue/cmd/get_go_test.go b/cmd/cue/cmd/get_go_test.go deleted file mode 100644 index a6671725a..000000000 --- a/cmd/cue/cmd/get_go_test.go +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2019 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "io/ioutil" - "os" - "path/filepath" - "strings" - "testing" - - "cuelang.org/go/internal/copy" - "github.com/google/go-cmp/cmp" -) - -func TestGetGo(t *testing.T) { - // Leave the current working directory outside the testdata directory - // so that Go loader finds the Go mod file and creates a proper path. - // We need to trick the command to generate the data within the testdata - // directory, though. - tmp, err := ioutil.TempDir("", "cue_get_go") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmp) - - cueTestRoot = tmp - - // We don't use runCommand here, as we are interested in generated packages. - cmd := newGoCmd(newRootCmd()) - cmd.SetArgs([]string{"./testdata/code/go/..."}) - err = cmd.Execute() - if err != nil { - t.Fatal(err) - } - - // Packages will generate differently in modules versus GOPATH. Search - // for the common ground to not have breaking text if people run these - // test in GOPATH mode. - root := "" - _ = filepath.Walk(tmp, func(path string, info os.FileInfo, err error) error { - if root != "" { - return filepath.SkipDir - } - if filepath.Base(path) == "cuelang.org" { - root = filepath.Dir(path) - return filepath.SkipDir - } - return nil - }) - - const dst = "testdata/pkg" - - if *update { - os.RemoveAll(dst) - err := copy.Dir(filepath.Join(root), dst) - if err != nil { - t.Fatal(err) - } - t.Skip("files updated") - } - - prefix := "testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/" - _ = filepath.Walk(dst, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - return nil - } - t.Run(path, func(t *testing.T) { - want := loadFile(t, path) - got := loadFile(t, filepath.Join(root, path[len(dst):])) - - if want != got { - t.Errorf("contexts for file %s differ: \n%s", path[len(prefix):], cmp.Diff(got, want)) - } - }) - return nil - }) -} - -func loadFile(t *testing.T, path string) string { - t.Helper() - b, err := ioutil.ReadFile(path) - if err != nil { - t.Fatalf("could not load file %s", path) - } - // Strip comments up till package clause. Local packages will generate - // differently using GOPATH versus modules. - s := string(b) - return s[strings.Index(s, "package"):] -} diff --git a/cmd/cue/cmd/interfaces_gen.go b/cmd/cue/cmd/interfaces_gen.go new file mode 100644 index 000000000..6adde7282 --- /dev/null +++ b/cmd/cue/cmd/interfaces_gen.go @@ -0,0 +1,10 @@ +// Code generated by internal/cmd/embedpkg. DO NOT EDIT. + +package cmd + +// interfacesFiles is the result of embedding GoFiles from the +// cuelang.org/go/cmd/cue/cmd/interfaces package. +var interfacesFiles = map[string][]byte { + "cmd/cue/cmd/interfaces/text.go": []byte{0x2f, 0x2f, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x32, 0x30, 0x31, 0x39, 0x20, 0x43, 0x55, 0x45, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x64, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x32, 0x2e, 0x30, 0x20, 0x28, 0x74, 0x68, 0x65, 0x20, 0x22, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x22, 0x29, 0x3b, 0xa, 0x2f, 0x2f, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x75, 0x73, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x65, 0x78, 0x63, 0x65, 0x70, 0x74, 0x20, 0x69, 0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x6c, 0x69, 0x61, 0x6e, 0x63, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2e, 0xa, 0x2f, 0x2f, 0x20, 0x59, 0x6f, 0x75, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x6f, 0x62, 0x74, 0x61, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x70, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x61, 0x74, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x73, 0x2f, 0x4c, 0x49, 0x43, 0x45, 0x4e, 0x53, 0x45, 0x2d, 0x32, 0x2e, 0x30, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x55, 0x6e, 0x6c, 0x65, 0x73, 0x73, 0x20, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x6c, 0x61, 0x77, 0x20, 0x6f, 0x72, 0x20, 0x61, 0x67, 0x72, 0x65, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x69, 0x6e, 0x20, 0x77, 0x72, 0x69, 0x74, 0x69, 0x6e, 0x67, 0x2c, 0x20, 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0xa, 0x2f, 0x2f, 0x20, 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x6e, 0x20, 0x22, 0x41, 0x53, 0x20, 0x49, 0x53, 0x22, 0x20, 0x42, 0x41, 0x53, 0x49, 0x53, 0x2c, 0xa, 0x2f, 0x2f, 0x20, 0x57, 0x49, 0x54, 0x48, 0x4f, 0x55, 0x54, 0x20, 0x57, 0x41, 0x52, 0x52, 0x41, 0x4e, 0x54, 0x49, 0x45, 0x53, 0x20, 0x4f, 0x52, 0x20, 0x43, 0x4f, 0x4e, 0x44, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x53, 0x20, 0x4f, 0x46, 0x20, 0x41, 0x4e, 0x59, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x2c, 0x20, 0x65, 0x69, 0x74, 0x68, 0x65, 0x72, 0x20, 0x65, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x6f, 0x72, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x69, 0x65, 0x64, 0x2e, 0xa, 0x2f, 0x2f, 0x20, 0x53, 0x65, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x63, 0x20, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x20, 0x67, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x61, 0x6e, 0x64, 0xa, 0x2f, 0x2f, 0x20, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2e, 0xa, 0xa, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x73, 0xa, 0xa, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x22, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x22, 0xa, 0xa, 0x74, 0x79, 0x70, 0x65, 0x20, 0x28, 0xa, 0x9, 0x74, 0x65, 0x78, 0x74, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x20, 0x20, 0x20, 0x3d, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x54, 0x65, 0x78, 0x74, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0xa, 0x9, 0x74, 0x65, 0x78, 0x74, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x54, 0x65, 0x78, 0x74, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0xa, 0x29, 0xa, 0xa, 0x2f, 0x2f, 0x20, 0x53, 0x75, 0x70, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x69, 0x6e, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x20, 0x28, 0x74, 0x79, 0x70, 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x75, 0x73, 0x65, 0x64, 0x29, 0x2e, 0xa, 0x76, 0x61, 0x72, 0x20, 0x28, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x29, 0xa}, + "cmd/cue/cmd/interfaces/top.go": []byte{0x2f, 0x2f, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x32, 0x30, 0x31, 0x39, 0x20, 0x43, 0x55, 0x45, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x64, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x32, 0x2e, 0x30, 0x20, 0x28, 0x74, 0x68, 0x65, 0x20, 0x22, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x22, 0x29, 0x3b, 0xa, 0x2f, 0x2f, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x75, 0x73, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x65, 0x78, 0x63, 0x65, 0x70, 0x74, 0x20, 0x69, 0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x6c, 0x69, 0x61, 0x6e, 0x63, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2e, 0xa, 0x2f, 0x2f, 0x20, 0x59, 0x6f, 0x75, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x6f, 0x62, 0x74, 0x61, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x70, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x61, 0x74, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x73, 0x2f, 0x4c, 0x49, 0x43, 0x45, 0x4e, 0x53, 0x45, 0x2d, 0x32, 0x2e, 0x30, 0xa, 0x2f, 0x2f, 0xa, 0x2f, 0x2f, 0x20, 0x55, 0x6e, 0x6c, 0x65, 0x73, 0x73, 0x20, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x6c, 0x61, 0x77, 0x20, 0x6f, 0x72, 0x20, 0x61, 0x67, 0x72, 0x65, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x69, 0x6e, 0x20, 0x77, 0x72, 0x69, 0x74, 0x69, 0x6e, 0x67, 0x2c, 0x20, 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0xa, 0x2f, 0x2f, 0x20, 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x6e, 0x20, 0x22, 0x41, 0x53, 0x20, 0x49, 0x53, 0x22, 0x20, 0x42, 0x41, 0x53, 0x49, 0x53, 0x2c, 0xa, 0x2f, 0x2f, 0x20, 0x57, 0x49, 0x54, 0x48, 0x4f, 0x55, 0x54, 0x20, 0x57, 0x41, 0x52, 0x52, 0x41, 0x4e, 0x54, 0x49, 0x45, 0x53, 0x20, 0x4f, 0x52, 0x20, 0x43, 0x4f, 0x4e, 0x44, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x53, 0x20, 0x4f, 0x46, 0x20, 0x41, 0x4e, 0x59, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x2c, 0x20, 0x65, 0x69, 0x74, 0x68, 0x65, 0x72, 0x20, 0x65, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x6f, 0x72, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x69, 0x65, 0x64, 0x2e, 0xa, 0x2f, 0x2f, 0x20, 0x53, 0x65, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x63, 0x20, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x20, 0x67, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x61, 0x6e, 0x64, 0xa, 0x2f, 0x2f, 0x20, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2e, 0xa, 0xa, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x73, 0xa, 0xa, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x28, 0xa, 0x9, 0x22, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x22, 0xa, 0x29, 0xa, 0xa, 0x74, 0x79, 0x70, 0x65, 0x20, 0x28, 0xa, 0x9, 0x6a, 0x73, 0x6f, 0x6e, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x20, 0x20, 0x20, 0x3d, 0x20, 0x6a, 0x73, 0x6f, 0x6e, 0x2e, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0xa, 0x9, 0x6a, 0x73, 0x6f, 0x6e, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6a, 0x73, 0x6f, 0x6e, 0x2e, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0xa, 0xa, 0x9, 0x79, 0x61, 0x6d, 0x6c, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x20, 0x20, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x7b, 0x20, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x59, 0x41, 0x4d, 0x4c, 0x28, 0x29, 0x20, 0x28, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x7b, 0x7d, 0x2c, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x29, 0x20, 0x7d, 0xa, 0x9, 0x79, 0x61, 0x6d, 0x6c, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x7b, 0xa, 0x9, 0x9, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x59, 0x41, 0x4d, 0x4c, 0x28, 0x66, 0x75, 0x6e, 0x63, 0x28, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x7b, 0x7d, 0x29, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x29, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0xa, 0x9, 0x7d, 0xa, 0x29, 0xa, 0xa, 0x2f, 0x2f, 0x20, 0x53, 0x75, 0x70, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x69, 0x6e, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x20, 0x28, 0x74, 0x79, 0x70, 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x75, 0x73, 0x65, 0x64, 0x29, 0x2e, 0xa, 0x76, 0x61, 0x72, 0x20, 0x28, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x6a, 0x73, 0x6f, 0x6e, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x6a, 0x73, 0x6f, 0x6e, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x65, 0x72, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x79, 0x61, 0x6d, 0x6c, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x9, 0x5f, 0x20, 0x3d, 0x20, 0x79, 0x61, 0x6d, 0x6c, 0x55, 0x6e, 0x6d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x28, 0x6e, 0x69, 0x6c, 0x29, 0xa, 0x29, 0xa}, +} diff --git a/cmd/cue/cmd/testdata/code/go/pkg1/alias.go b/cmd/cue/cmd/testdata/code/go/pkg1/alias.go deleted file mode 100644 index f64443755..000000000 --- a/cmd/cue/cmd/testdata/code/go/pkg1/alias.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2020 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pkg1 - -import p3 "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3" - -type MyBarzer = p3.Barzer diff --git a/cmd/cue/cmd/testdata/code/go/pkg1/file1.go b/cmd/cue/cmd/testdata/code/go/pkg1/file1.go deleted file mode 100644 index 1d2076482..000000000 --- a/cmd/cue/cmd/testdata/code/go/pkg1/file1.go +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2019 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/* - block comment -*/ -package pkg1 - -import ( - "encoding" - "encoding/json" - "time" - - p2 "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2" -) - -// Foozer foozes a jaman. -type Foozer struct { - Int int - String string - - Inline `json:",inline"` - NoInline - - CustomJSON CustomJSON - CustomYAML *CustomYAML - AnyJSON json.Marshaler - AnyText encoding.TextMarshaler - - Bar int `json:"bar,omitempty" cue:">10"` - - exclude int - - // Time is mapped to CUE's internal type. - Time time.Time - - Barzer p2.Barzer - - Alias1 *MyBarzer - - Map map[string]*CustomJSON - Slice1 []int - Slice2 []interface{} - Slice3 *[]json.Unmarshaler - Array1 [5]int - Array2 [5]interface{} - Array3 *[5]json.Marshaler - - Intf Interface `protobuf:"varint,2,name=intf"` - Intf2 interface{} - Intf3 struct{ Interface } - Intf4 interface{ Foo() } - - // Even though this struct as a type implements MarshalJSON, it is known - // that it is really only implemented by the embedded field. - Embed struct{ CustomJSON } - - Unsupported map[int]string -} - -type Identifier string - -const ( - internalIdentifier Identifier = "internal" -) - -const _ = true - -// appease linter -var _ = internalIdentifier - -// Level gives an indication of the extent of stuff. -type Level int - -const ( - /* - Block comment. - Indented. - - Empty line before. - */ - Unknown Level = iota - Low - // Medium is neither High nor Low - Medium - High -) - -type CustomJSON struct { -} - -func (c *CustomJSON) MarshalJSON() ([]byte, error) { - return nil, nil -} - -type CustomYAML struct { -} - -func (c CustomYAML) MarshalYAML() ([]byte, error) { - return nil, nil -} - -type localType int - -const ( - localConst localType = 1 - - _ = localConst // silence linter -) - -type Inline struct { - Kind string -} - -type NoInline struct { - Kind string -} - -type Interface interface { - Boomer() bool -} diff --git a/cmd/cue/cmd/testdata/code/go/pkg2/add.cue b/cmd/cue/cmd/testdata/code/go/pkg2/add.cue deleted file mode 100644 index 1c6002333..000000000 --- a/cmd/cue/cmd/testdata/code/go/pkg2/add.cue +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2019 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pkgtwo - -Barzer: { - S: =~"cat$" -} diff --git a/cmd/cue/cmd/testdata/code/go/pkg2/pkg2.go b/cmd/cue/cmd/testdata/code/go/pkg2/pkg2.go deleted file mode 100644 index 7b93f3283..000000000 --- a/cmd/cue/cmd/testdata/code/go/pkg2/pkg2.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2019 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package pkgtwo does other stuff. -package pkgtwo - -import ( - "math/big" - t "time" -) - -// A Barzer barzes. -type Barzer struct { - A int `protobuf:"varint,2," json:"a"` - - T t.Time - B *big.Int - C big.Int - F big.Float `xml:",attr"` - G *big.Float - H bool `json:"-"` - S string - - XY bool `json:"x-y"` - - Err error - - *Inline `json:",inline"` -} - -const Perm = 0755 - -const Few = 3 - -const Couple int = 2 - -type Inline struct{ A int } diff --git a/cmd/cue/cmd/testdata/code/go/pkg3/pkg3.go b/cmd/cue/cmd/testdata/code/go/pkg3/pkg3.go deleted file mode 100644 index db619c73e..000000000 --- a/cmd/cue/cmd/testdata/code/go/pkg3/pkg3.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2020 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pkg3 - -import pkgtwo "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2" - -type Barzer = pkgtwo.Barzer diff --git a/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt b/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt index 0fc3b23a5..5d110c512 100644 --- a/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt +++ b/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt @@ -6,7 +6,6 @@ module example.com/blah -- go.mod -- module example.com/blah - -- blah.go -- package blah diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/alias_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/alias_go_gen.cue deleted file mode 100644 index 4fa869f8e..000000000 --- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/alias_go_gen.cue +++ /dev/null @@ -1,9 +0,0 @@ -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1 - -package pkg1 - -import "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2" - -#MyBarzer: pkgtwo.#Barzer diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue deleted file mode 100644 index d9ef845fb..000000000 --- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue +++ /dev/null @@ -1,91 +0,0 @@ -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1 - -// block comment -package pkg1 - -import ( - "time" - p2 "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2:pkgtwo" -) - -// Foozer foozes a jaman. -#Foozer: { - Int: int - String: string - - #Inline - NoInline: #NoInline - CustomJSON: #CustomJSON - CustomYAML?: null | #CustomYAML @go(,*CustomYAML) - AnyJSON: _ @go(,json.Marshaler) - AnyText: string @go(,encoding.TextMarshaler) - bar?: int & >10 @go(Bar) - - // Time is mapped to CUE's internal type. - Time: time.Time - Barzer: p2.#Barzer - Alias1?: null | p2.#Barzer @go(,*p2.Barzer) - Map: {[string]: null | #CustomJSON} @go(,map[string]*CustomJSON) - Slice1: [...int] @go(,[]int) - Slice2: [...] @go(,[]interface{}) - Slice3?: null | [...] @go(,*[]json.Unmarshaler) - Array1: 5 * [int] @go(,[5]int) - Array2: 5 * [_] @go(,[5]interface{}) - Array3?: null | 5*[_] @go(,*[5]json.Marshaler) - Intf: #Interface @protobuf(2,varint,name=intf) - Intf2: _ @go(,interface{}) - Intf3: { - Interface: #Interface - } @go(,struct{Interface}) - Intf4: _ @go(,"interface{Foo()}") - - // Even though this struct as a type implements MarshalJSON, it is known - // that it is really only implemented by the embedded field. - Embed: { - CustomJSON: #CustomJSON - } @go(,struct{CustomJSON}) -} - -#Identifier: string // #enumIdentifier - -#enumIdentifier: - _#internalIdentifier - -_#internalIdentifier: #Identifier & "internal" - -// Level gives an indication of the extent of stuff. -#Level: int // #enumLevel - -#enumLevel: - #Unknown | - #Low | - #Medium | - #High - -// Block comment. -// Indented. -// -// Empty line before. -#Unknown: #Level & 0 -#Low: #Level & 1 - -// Medium is neither High nor Low -#Medium: #Level & 2 -#High: #Level & 3 - -#CustomJSON: _ - -#CustomYAML: { -} - -_#localType: int - -_#localConst: _#localType & 1 - -#Inline: Kind: string - -#NoInline: Kind: string - -#Interface: _ diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/add_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/add_gen.cue deleted file mode 100644 index cd02e8817..000000000 --- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/add_gen.cue +++ /dev/null @@ -1,23 +0,0 @@ -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2 - -// Copyright 2019 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package pkgtwo - -Barzer: { - S: =~"cat$" -} diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue deleted file mode 100644 index ca478413b..000000000 --- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2/pkg2_go_gen.cue +++ /dev/null @@ -1,31 +0,0 @@ -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2 - -// Package pkgtwo does other stuff. -package pkgtwo - -import t "time" - -// A Barzer barzes. -#Barzer: { - a: int @go(A) @protobuf(2,varint,) - T: t.Time - B?: null | int @go(,*big.Int) - C: int @go(,big.Int) - F: string @go(,big.Float) @xml(,attr) - G?: null | string @go(,*big.Float) - S: string - "x-y": bool @go(XY) - Err: _ @go(,error) - - #Inline -} - -#Perm: 0o755 - -#Few: 3 - -#Couple: int & 2 - -#Inline: A: int diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3/pkg3_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3/pkg3_go_gen.cue deleted file mode 100644 index 4ee74a39d..000000000 --- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3/pkg3_go_gen.cue +++ /dev/null @@ -1,9 +0,0 @@ -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg3 - -package pkg3 - -import "cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg2:pkgtwo" - -#Barzer: pkgtwo.#Barzer diff --git a/cmd/cue/cmd/testdata/script/get_go_basic.txt b/cmd/cue/cmd/testdata/script/get_go_basic.txt deleted file mode 100644 index e0fc4f3f6..000000000 --- a/cmd/cue/cmd/testdata/script/get_go_basic.txt +++ /dev/null @@ -1,60 +0,0 @@ -# Test that a basic get go works using golden files to verify output - -# All the things -cue get go --local -cmp blah_go_gen.cue all.cue.golden - -# Use an alternative package name -cue get go --local -p other -cmp blah_go_gen.cue other.cue.golden - --- go.mod -- -module mod.com/blah --- blah.go -- -package main - -type S struct { - Name string - T -} - -type T struct { - Age int -} - -const ( - LongStringConst = "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()" - IntConst = "test" -) --- all.cue.golden -- -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go mod.com/blah - -package main - -#S: { - Name: string - T: #T -} - -#T: Age: int - -#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()" -#IntConst: "test" --- other.cue.golden -- -// Code generated by cue get go. DO NOT EDIT. - -//cue:generate cue get go mod.com/blah - -package other - -#S: { - Name: string - T: #T -} - -#T: Age: int - -#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()" -#IntConst: "test" diff --git a/cmd/cue/cmd/testdata/script/get_go_local.txt b/cmd/cue/cmd/testdata/script/get_go_local.txt new file mode 100644 index 000000000..b7c8be0bf --- /dev/null +++ b/cmd/cue/cmd/testdata/script/get_go_local.txt @@ -0,0 +1,46 @@ +# Test that a basic get go works using --local + +# All the things +cue get go --local +cmp blah_go_gen.cue all.cue.golden + +# Verify dependencies did not change +cmp go.mod go.mod.golden + +# Use an alternative package name +cue get go --local -p other +cmp blah_go_gen.cue other.cue.golden + +# Verify dependencies did not change +cmp go.mod go.mod.golden + +-- go.mod -- +module mod.com/blah + +go 1.14 +-- go.mod.golden -- +module mod.com/blah + +go 1.14 +-- blah.go -- +package main + +type T struct { + Age int +} +-- all.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go mod.com/blah + +package main + +#T: Age: int +-- other.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go mod.com/blah + +package other + +#T: Age: int diff --git a/cmd/cue/cmd/testdata/script/get_go_non_local.txt b/cmd/cue/cmd/testdata/script/get_go_non_local.txt new file mode 100644 index 000000000..ee50d96c0 --- /dev/null +++ b/cmd/cue/cmd/testdata/script/get_go_non_local.txt @@ -0,0 +1,54 @@ +# Test that cue get go works when used without the --local flag + +# cue get go currently appear to act like a wrapper around go get. +# But this is going to change: "cue get go" will be renamed "cue import go". +# When that change happens, cue import go will assume that all +# required dependencies are present, and fail otherwise. +go get example.com/blah@v1.0.0 +go mod tidy + +# cue get go +cue get go example.com/blah +cmp cue.mod/gen/example.com/blah/blah_go_gen.cue cue.mod/gen/example.com/blah/blah_go_gen.cue.golden +cmp cue.mod/gen/example.com/blah/blah_gen.cue cue.mod/gen/example.com/blah/blah_gen.cue.golden + +# Verify dependencies are as expected +cmp go.mod go.mod.golden + +-- cue.mod/module.cue -- +module: "mod.com/blah" + +-- go.mod -- +module mod.com/blah + +go 1.14 +-- go.mod.golden -- +module mod.com/blah + +go 1.14 + +require example.com/blah v1.0.0 +-- cuedeps.go -- +// +build cuedeps + +package cuedeps + +import ( + _ "example.com/blah" +) +-- cue.mod/gen/example.com/blah/blah_go_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/blah + +package blah + +#Name: "Orange" +-- cue.mod/gen/example.com/blah/blah_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/blah + +package blah + +Type: "Fruit" diff --git a/cmd/cue/cmd/testdata/script/get_go_types.txt b/cmd/cue/cmd/testdata/script/get_go_types.txt new file mode 100644 index 000000000..9a99a2488 --- /dev/null +++ b/cmd/cue/cmd/testdata/script/get_go_types.txt @@ -0,0 +1,520 @@ +# Test that a get go works for various "interesting" types, +# including those special cases that result in _ and string +# that are specifically mentioned in help get go. +# +# This test does not exercise with/without --local. Instead +# that is left for other tests, so that this test can focus +# on the various rules of get go. + +# cue get go +cue get go --local ./... +cmp ./pkg3/pkg3_go_gen.cue ./pkg3/pkg3_go_gen.cue.golden +cmp ./pkg1/file1_go_gen.cue ./pkg1/file1_go_gen.cue.golden +cmp ./pkg1/alias_go_gen.cue ./pkg1/alias_go_gen.cue.golden +cmp ./pkg2/pkg2_go_gen.cue ./pkg2/pkg2_go_gen.cue.golden + +# Verify dependencies did not change +cmp go.mod go.mod.golden + +-- go.mod -- +module example.com + +go 1.14 +-- go.mod.golden -- +module example.com + +go 1.14 +-- cue.mod -- +module: "example.com" +-- pkg1/alias.go -- +package pkg1 + +import p3 "example.com/pkg3" + +type MyBarzer = p3.Barzer +-- pkg1/file1.go -- +/* + block comment +*/ +package pkg1 + +import ( + "encoding" + "encoding/json" + "time" + + p2 "example.com/pkg2" +) + +// Foozer foozes a jaman. +type Foozer struct { + Int int + String string + + Inline `json:",inline"` + NoInline + + CustomJSON CustomJSON + CustomYAML *CustomYAML + AnyJSON json.Marshaler + AnyText encoding.TextMarshaler + + Bar int `json:"bar,omitempty" cue:">10"` + + exclude int + + // Time is mapped to CUE's internal type. + Time time.Time + + Barzer p2.Barzer + + Alias1 *MyBarzer + + Map map[string]*CustomJSON + Slice1 []int + Slice2 []interface{} + Slice3 *[]json.Unmarshaler + Array1 [5]int + Array2 [5]interface{} + Array3 *[5]json.Marshaler + + Intf Interface `protobuf:"varint,2,name=intf"` + Intf2 interface{} + Intf3 struct{ Interface } + Intf4 interface{ Foo() } + + // Even though this struct as a type implements MarshalJSON, it is known + // that it is really only implemented by the embedded field. + Embed struct{ CustomJSON } + + Unsupported map[int]string +} + +type Identifier string + +const ( + internalIdentifier Identifier = "internal" +) + +const _ = true + +// appease linter +var _ = internalIdentifier + +// Level gives an indication of the extent of stuff. +type Level int + +const ( + /* + Block comment. + Indented. + + Empty line before. + */ + Unknown Level = iota + Low + // Medium is neither High nor Low + Medium + High +) + +type CustomJSON struct { +} + +func (c *CustomJSON) MarshalJSON() ([]byte, error) { + return nil, nil +} + +type CustomYAML struct { +} + +func (c CustomYAML) MarshalYAML() ([]byte, error) { + return nil, nil +} + +type localType int + +const ( + localConst localType = 1 + + _ = localConst // silence linter +) + +type Inline struct { + Kind string +} + +type NoInline struct { + Kind string +} + +type Interface interface { + Boomer() bool +} +-- pkg2/add.cue -- +package pkgtwo + +Barzer: { + S: =~"cat$" +} +-- pkg2/pkg2.go -- +// Package pkgtwo does other stuff. +package pkgtwo + +import ( + "math/big" + t "time" +) + +// A Barzer barzes. +type Barzer struct { + A int `protobuf:"varint,2," json:"a"` + + T t.Time + B *big.Int + C big.Int + F big.Float `xml:",attr"` + G *big.Float + H bool `json:"-"` + S string + + XY bool `json:"x-y"` + + Err error + + *Inline `json:",inline"` +} + +const Perm = 0755 + +const Few = 3 + +const Couple int = 2 + +// LongStringConst ensures that we are using go/constant +// correctly for the exact value of a constant as opposed +// to a shortened version (relevant for strings) +const LongStringConst = "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()" + +type Inline struct{ A int } +-- pkg3/pkg3.go -- +package pkg3 + +import pkgtwo "example.com/pkg2" + +type Barzer = pkgtwo.Barzer +-- pkg1/alias_go_gen.cue -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg1 + +package pkg1 + +import "example.com/pkg2" + +#MyBarzer: pkgtwo.#Barzer +-- pkg1/file1_go_gen.cue -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg1 + +// block comment +package pkg1 + +import ( + "time" + p2 "example.com/pkg2:pkgtwo" +) + +// Foozer foozes a jaman. +#Foozer: { + Int: int + String: string + + #Inline + NoInline: #NoInline + CustomJSON: #CustomJSON + CustomYAML?: null | #CustomYAML @go(,*CustomYAML) + AnyJSON: _ @go(,json.Marshaler) + AnyText: string @go(,encoding.TextMarshaler) + bar?: int & >10 @go(Bar) + + // Time is mapped to CUE's internal type. + Time: time.Time + Barzer: p2.#Barzer + Alias1?: null | p2.#Barzer @go(,*p2.Barzer) + Map: {[string]: null | #CustomJSON} @go(,map[string]*CustomJSON) + Slice1: [...int] @go(,[]int) + Slice2: [...] @go(,[]interface{}) + Slice3?: null | [...] @go(,*[]json.Unmarshaler) + Array1: 5 * [int] @go(,[5]int) + Array2: 5 * [_] @go(,[5]interface{}) + Array3?: null | 5*[_] @go(,*[5]json.Marshaler) + Intf: #Interface @protobuf(2,varint,name=intf) + Intf2: _ @go(,interface{}) + Intf3: { + Interface: #Interface + } @go(,struct{Interface}) + Intf4: _ @go(,"interface{Foo()}") + + // Even though this struct as a type implements MarshalJSON, it is known + // that it is really only implemented by the embedded field. + Embed: { + CustomJSON: #CustomJSON + } @go(,struct{CustomJSON}) +} + +#Identifier: string // #enumIdentifier + +#enumIdentifier: + _#internalIdentifier + +_#internalIdentifier: #Identifier & "internal" + +// Level gives an indication of the extent of stuff. +#Level: int // #enumLevel + +#enumLevel: + #Unknown | + #Low | + #Medium | + #High + +// Block comment. +// Indented. +// +// Empty line before. +#Unknown: #Level & 0 +#Low: #Level & 1 + +// Medium is neither High nor Low +#Medium: #Level & 2 +#High: #Level & 3 + +#CustomJSON: _ + +#CustomYAML: { +} + +_#localType: int + +_#localConst: _#localType & 1 + +#Inline: Kind: string + +#NoInline: Kind: string + +#Interface: _ +-- pkg2/pkg2_go_gen.cue -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg2 + +// Package pkgtwo does other stuff. +package pkgtwo + +import t "time" + +// A Barzer barzes. +#Barzer: { + a: int @go(A) @protobuf(2,varint,) + T: t.Time + B?: null | int @go(,*big.Int) + C: int @go(,big.Int) + F: string @go(,big.Float) @xml(,attr) + G?: null | string @go(,*big.Float) + S: string + "x-y": bool @go(XY) + Err: _ @go(,error) + + #Inline +} + +#Perm: 0o755 + +#Few: 3 + +#Couple: int & 2 + +#Inline: A: int +-- pkg3/pkg3_go_gen.cue -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg3 + +package pkg3 + +import "example.com/pkg2:pkgtwo" + +#Barzer: pkgtwo.#Barzer + +-- pkg1/alias_go_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg1 + +package pkg1 + +import "example.com/pkg2" + +#MyBarzer: pkgtwo.#Barzer +-- pkg1/file1_go_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg1 + +// block comment +package pkg1 + +import ( + "time" + p2 "example.com/pkg2:pkgtwo" +) + +// Foozer foozes a jaman. +#Foozer: { + Int: int + String: string + + #Inline + NoInline: #NoInline + CustomJSON: #CustomJSON + CustomYAML?: null | #CustomYAML @go(,*CustomYAML) + AnyJSON: _ @go(,json.Marshaler) + AnyText: string @go(,encoding.TextMarshaler) + bar?: int & >10 @go(Bar) + + // Time is mapped to CUE's internal type. + Time: time.Time + Barzer: p2.#Barzer + Alias1?: null | p2.#Barzer @go(,*p2.Barzer) + Map: {[string]: null | #CustomJSON} @go(,map[string]*CustomJSON) + Slice1: [...int] @go(,[]int) + Slice2: [...] @go(,[]interface{}) + Slice3?: null | [...] @go(,*[]json.Unmarshaler) + Array1: 5 * [int] @go(,[5]int) + Array2: 5 * [_] @go(,[5]interface{}) + Array3?: null | 5*[_] @go(,*[5]json.Marshaler) + Intf: #Interface @protobuf(2,varint,name=intf) + Intf2: _ @go(,interface{}) + Intf3: { + Interface: #Interface + } @go(,struct{Interface}) + Intf4: _ @go(,"interface{Foo()}") + + // Even though this struct as a type implements MarshalJSON, it is known + // that it is really only implemented by the embedded field. + Embed: { + CustomJSON: #CustomJSON + } @go(,struct{CustomJSON}) +} + +#Identifier: string // #enumIdentifier + +#enumIdentifier: + _#internalIdentifier + +_#internalIdentifier: #Identifier & "internal" + +// Level gives an indication of the extent of stuff. +#Level: int // #enumLevel + +#enumLevel: + #Unknown | + #Low | + #Medium | + #High + +// Block comment. +// Indented. +// +// Empty line before. +#Unknown: #Level & 0 +#Low: #Level & 1 + +// Medium is neither High nor Low +#Medium: #Level & 2 +#High: #Level & 3 + +#CustomJSON: _ + +#CustomYAML: { +} + +_#localType: int + +_#localConst: _#localType & 1 + +#Inline: Kind: string + +#NoInline: Kind: string + +#Interface: _ +-- pkg2/add_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg2 + +// Copyright 2019 CUE Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pkgtwo + +Barzer: { + S: =~"cat$" +} +-- pkg2/pkg2_go_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg2 + +// Package pkgtwo does other stuff. +package pkgtwo + +import t "time" + +// A Barzer barzes. +#Barzer: { + a: int @go(A) @protobuf(2,varint,) + T: t.Time + B?: null | int @go(,*big.Int) + C: int @go(,big.Int) + F: string @go(,big.Float) @xml(,attr) + G?: null | string @go(,*big.Float) + S: string + "x-y": bool @go(XY) + Err: _ @go(,error) + + #Inline +} + +#Perm: 0o755 + +#Few: 3 + +#Couple: int & 2 + +#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()" + +#Inline: A: int +-- pkg3/pkg3_go_gen.cue.golden -- +// Code generated by cue get go. DO NOT EDIT. + +//cue:generate cue get go example.com/pkg3 + +package pkg3 + +import "example.com/pkg2:pkgtwo" + +#Barzer: pkgtwo.#Barzer diff --git a/encoding/gocode/generator.go b/encoding/gocode/generator.go index b5f4476c6..0270e930e 100644 --- a/encoding/gocode/generator.go +++ b/encoding/gocode/generator.go @@ -115,7 +115,7 @@ func Generate(pkgPath string, inst *cue.Instance, c *Config) (b []byte, err erro if pkgPath != "" { loadCfg := &packages.Config{ - Mode: packages.LoadAllSyntax, + Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps, } pkgs, err := packages.Load(loadCfg, pkgPath) if err != nil { diff --git a/internal/cmd/embedpkg/embedpkg.go b/internal/cmd/embedpkg/embedpkg.go new file mode 100644 index 000000000..2867aa1c6 --- /dev/null +++ b/internal/cmd/embedpkg/embedpkg.go @@ -0,0 +1,109 @@ +// Copyright 2020 CUE Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// embedpkg accepts a [packages] argument (see 'go help packages') and creates +// a map[string][]byte for each package argument, a map that represents the +// GoFiles for that package. The principal use case here is the embedding +// of the cuelang.org/go/cmd/cue/cmd/interfaces used by cue get go. +package main + +import ( + "bytes" + "flag" + "io/ioutil" + "log" + "path/filepath" + "text/template" + + "golang.org/x/tools/go/packages" +) + +const genTmpl = `// Code generated by internal/cmd/embedpkg. DO NOT EDIT. + +package cmd + +// {{ .Basename }}Files is the result of embedding GoFiles from the +// {{ .PkgPath }} package. +var {{ .Basename }}Files = map[string][]byte { +{{- range $fn, $content := .GoFiles }} + "{{ $fn }}": {{ printf "%#v" $content }}, +{{- end }} +} +` + +func main() { + log.SetFlags(0) + flag.Parse() + + cfg := &packages.Config{ + Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedModule, + } + pkgs, err := packages.Load(cfg, flag.Args()...) + if err != nil { + log.Fatal(err) + } + + for _, p := range pkgs { + if packages.PrintErrors(pkgs) > 0 { + // The errors will already have been printed + log.Fatalln("error loading packages") + } + + // Read GoFiles + files := map[string][]byte{} + for _, fn := range p.GoFiles { + // Because of https://github.com/golang/go/issues/38445 we don't have p.Dir + content, err := ioutil.ReadFile(fn) + if err != nil { + log.Fatal(err) + } + relFile, err := filepath.Rel(p.Module.Dir, fn) + if err != nil { + log.Fatal(err) + } + files[relFile] = content + } + + // parse template + tmpl, err := template.New(p.Name).Parse(genTmpl) + if err != nil { + log.Fatal(err) + } + + // setup template data + data := struct { + Basename string + PkgPath string + GoFiles map[string][]byte + }{ + Basename: p.Name, + PkgPath: p.PkgPath, + GoFiles: files, + } + + // generate content + var b bytes.Buffer + err = tmpl.Execute(&b, data) + if err != nil { + log.Fatal(err) + } + + // write file + err = ioutil.WriteFile(filepath.Join(p.Name+"_gen.go"), b.Bytes(), 0666) + if err != nil { + log.Fatal(err) + } + + } +}