Skip to content

Commit

Permalink
cmd/cue: get go: use constant.Value.ExactString to extract constant
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
myitcv authored and mpvl committed Aug 18, 2020
1 parent c76a530 commit 1a9b88d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,10 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
}

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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/cue/cmd/get_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"cuelang.org/go/internal/copy"
"github.com/google/go-cmp/cmp"
)

func TestGetGo(t *testing.T) {
Expand Down Expand Up @@ -81,7 +82,7 @@ func TestGetGo(t *testing.T) {
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
Expand Down
45 changes: 45 additions & 0 deletions cmd/cue/cmd/testdata/script/get_go_basic.txt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1a9b88d

Please sign in to comment.