Skip to content

Commit f567a0c

Browse files
committed
cmd/cue: support more escape codes from Go string constants
We were using go/constant's ExactString to obtain a CUE expression, which worked for the most part given how Go basic literals such as strings are quite similar to CUE. However, one crucial difference is that Go allows \x hexadecimal escape sequences in strings, which ExactString prefers to use at times, whereas CUE only allows them only in byte sequences: Hexadecimal and octal escapes are only allowed within byte sequences (single quotes). There is room for further improvement, as we should not be using go/constant's ExactString for other kinds of basic literals either. That's left for a separate change to make bisecting easier if we introduce any unintentional regressions. Fixes #2915. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iea040b34a321e7b95004f35431d42ab63604edd7 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1211847 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent ce37bb5 commit f567a0c

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Diff for: cmd/cue/cmd/get_go.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"fmt"
2020
"go/ast"
21+
"go/constant"
2122
"go/token"
2223
"go/types"
2324
"os"
@@ -787,11 +788,23 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
787788
}
788789

789790
typ := e.pkg.TypesInfo.TypeOf(name)
790-
c := e.pkg.TypesInfo.Defs[v.Names[i]].(*types.Const)
791-
sv := c.Val().ExactString()
792-
cv, err := parser.ParseExpr("", sv)
793-
if err != nil {
794-
panic(fmt.Errorf("failed to parse %v: %v", sv, err))
791+
v := e.pkg.TypesInfo.Defs[v.Names[i]].(*types.Const).Val()
792+
var cv cueast.Expr
793+
switch v.Kind() {
794+
case constant.String:
795+
cv = &cueast.BasicLit{
796+
Kind: cuetoken.STRING,
797+
Value: literal.String.Quote(constant.StringVal(v)),
798+
}
799+
800+
default:
801+
// TODO(mvdan): replace this with switch cases for Bool/Int/Float
802+
sv := v.ExactString()
803+
var err error
804+
cv, err = parser.ParseExpr("", sv)
805+
if err != nil {
806+
panic(fmt.Errorf("failed to parse %v: %v", sv, err))
807+
}
795808
}
796809

797810
// Use the original Go value if compatible with CUE (octal is okay)

Diff for: cmd/cue/cmd/testdata/script/get_go_json_compat.txtar

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ cmp decls/p_go_gen.cue decls/p_go_gen.cue.golden
88
exec go run ./marshal
99
cp stdout go_marshal.json
1010
cmp go_marshal.json go_marshal.json.golden
11-
! exec cue vet go_marshal.json schema.cue
11+
exec cue vet go_marshal.json schema.cue
1212

1313
# Export from CUE and Unmarshal via Go's encoding/json.
14-
! exec cue export ./decls
15-
# cmp stdout cue_export.json.golden
16-
# stdin stdout
17-
# exec go run ./unmarshal
14+
exec cue export ./decls
15+
cmp stdout cue_export.json.golden
16+
stdin stdout
17+
exec go run ./unmarshal
1818

1919
-- schema.cue --
2020
package schema
@@ -80,9 +80,9 @@ package decls
8080

8181
#ValueSecond: int & 3000000000
8282

83-
#ErrorPrefix: "\x1b[31mError:\x1b[0m" // "\033[31mError:\033[0m"
83+
#ErrorPrefix: "\u001b[31mError:\u001b[0m" // "\033[31mError:\033[0m"
8484

85-
#ResetColor: "\x1b[0m" // "\u001b[0m"
85+
#ResetColor: "\u001b[0m"
8686
-- marshal/main.go --
8787
package main
8888

0 commit comments

Comments
 (0)