cmd/cue: get go: use constant.Value.ExactString to extract constant
Currently long string constant values break during the import because of
the use of go/constant.Value.String() (which is the short version).
Switch to using go/constant.Value.ExactString() for the complete value.
Add a basic testscript test whilst we are at it.
Fixes #458
Change-Id: I1a2973d008bd517d47474becf5dbbfb3df22ecfa
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6781
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go
index a84c83f..85d371a 100644
--- a/cmd/cue/cmd/get_go.go
+++ b/cmd/cue/cmd/get_go.go
@@ -695,9 +695,10 @@
}
c := e.pkg.TypesInfo.Defs[v.Names[i]].(*types.Const)
- cv, err := parser.ParseExpr("", c.Val().String())
+ sv := c.Val().ExactString()
+ cv, err := parser.ParseExpr("", sv)
if err != nil {
- panic(err)
+ panic(fmt.Errorf("failed to parse %v: %v", sv, err))
}
// Use orignal Go value if compatible with CUE (octal is okay)
diff --git a/cmd/cue/cmd/get_go_test.go b/cmd/cue/cmd/get_go_test.go
index bd69476..a667172 100644
--- a/cmd/cue/cmd/get_go_test.go
+++ b/cmd/cue/cmd/get_go_test.go
@@ -22,6 +22,7 @@
"testing"
"cuelang.org/go/internal/copy"
+ "github.com/google/go-cmp/cmp"
)
func TestGetGo(t *testing.T) {
@@ -81,7 +82,7 @@
got := loadFile(t, filepath.Join(root, path[len(dst):]))
if want != got {
- t.Errorf("contexts for file %s differ", path[len(prefix):])
+ t.Errorf("contexts for file %s differ: \n%s", path[len(prefix):], cmp.Diff(got, want))
}
})
return nil
diff --git a/cmd/cue/cmd/testdata/script/get_go_basic.txt b/cmd/cue/cmd/testdata/script/get_go_basic.txt
new file mode 100644
index 0000000..0856ea9
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/get_go_basic.txt
@@ -0,0 +1,45 @@
+# Test that a basic get go works using golden files to verify output
+
+# Set HOME for go-build cache to be valid
+env HOME=$WORK${/}home
+env USERPROFILE=$HOME
+env LOCALAPPDATA=$WORK${/}appdata
+
+# All the things
+cue get go --local
+cmp blah_go_gen.cue all.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"