Skip to content

Commit 1a9b88d

Browse files
myitcvmpvl
authored andcommitted
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 <[email protected]>
1 parent c76a530 commit 1a9b88d

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

cmd/cue/cmd/get_go.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,10 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
695695
}
696696

697697
c := e.pkg.TypesInfo.Defs[v.Names[i]].(*types.Const)
698-
cv, err := parser.ParseExpr("", c.Val().String())
698+
sv := c.Val().ExactString()
699+
cv, err := parser.ParseExpr("", sv)
699700
if err != nil {
700-
panic(err)
701+
panic(fmt.Errorf("failed to parse %v: %v", sv, err))
701702
}
702703

703704
// Use orignal Go value if compatible with CUE (octal is okay)

cmd/cue/cmd/get_go_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323

2424
"cuelang.org/go/internal/copy"
25+
"github.com/google/go-cmp/cmp"
2526
)
2627

2728
func TestGetGo(t *testing.T) {
@@ -81,7 +82,7 @@ func TestGetGo(t *testing.T) {
8182
got := loadFile(t, filepath.Join(root, path[len(dst):]))
8283

8384
if want != got {
84-
t.Errorf("contexts for file %s differ", path[len(prefix):])
85+
t.Errorf("contexts for file %s differ: \n%s", path[len(prefix):], cmp.Diff(got, want))
8586
}
8687
})
8788
return nil
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Test that a basic get go works using golden files to verify output
2+
3+
# Set HOME for go-build cache to be valid
4+
env HOME=$WORK${/}home
5+
env USERPROFILE=$HOME
6+
env LOCALAPPDATA=$WORK${/}appdata
7+
8+
# All the things
9+
cue get go --local
10+
cmp blah_go_gen.cue all.cue.golden
11+
12+
-- go.mod --
13+
module mod.com/blah
14+
-- blah.go --
15+
package main
16+
17+
type S struct {
18+
Name string
19+
T
20+
}
21+
22+
type T struct {
23+
Age int
24+
}
25+
26+
const (
27+
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()"
28+
IntConst = "test"
29+
)
30+
-- all.cue.golden --
31+
// Code generated by cue get go. DO NOT EDIT.
32+
33+
//cue:generate cue get go mod.com/blah
34+
35+
package main
36+
37+
#S: {
38+
Name: string
39+
T: #T
40+
}
41+
42+
#T: Age: int
43+
44+
#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()"
45+
#IntConst: "test"

0 commit comments

Comments
 (0)