Skip to content

Commit

Permalink
internal/core/export: fix quoting of definitions
Browse files Browse the repository at this point in the history
IsValidIdent passes for definitions and hidden fields,
so this should be handled separately.

Also added some tests in compile, although this is not necessary.

Fixes #746

Change-Id: I38024a9391e985bc2f87c401c5682ed5f64d6421
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8705
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 13, 2021
1 parent af5ea97 commit a5daa16
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cue/testdata/compile/fields.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- in.cue --
#dev: int
"#dev": int
_dev: int
_#dev: int
-- out/compile --
--- in.cue
{
#dev: int
"#dev": int
_dev: int
_#dev: int
}
-- out/eval --
(struct){
#dev: (int){ int }
"#dev": (int){ int }
_dev: (int){ int }
_#dev: (int){ int }
}
4 changes: 3 additions & 1 deletion internal/core/export/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package export

import (
"strconv"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/literal"
Expand All @@ -35,7 +36,8 @@ func (e *exporter) stringLabel(f adt.Feature) ast.Label {

case adt.StringLabel:
s := e.ctx.IndexToString(int64(x))
if f == 0 || !ast.IsValidIdent(s) {
if f == 0 || !ast.IsValidIdent(s) ||
strings.HasPrefix(s, "#") || strings.HasPrefix(s, "_") {
return ast.NewLit(token.STRING, literal.Label.Quote(s))
}
fallthrough
Expand Down
89 changes: 89 additions & 0 deletions internal/core/export/testdata/labels.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
-- in.cue --
package foo

#def: int
"#def": int
_def: int
_#def: int

a: {
#def: int
"#def": int
_def: int
_#def: int
}

-- out/definition --
package foo

#def: int
"#def": int
_def: int
_#def: int
a: {
#def: int
"#def": int
_def: int
_#def: int
}
-- out/doc --
[]
[#def]
["#def"]
[_def]
[_#def]
[a]
[a #def]
[a "#def"]
[a _def]
[a _#def]
-- out/value --
== Simplified
{
"#def": int
a: {
"#def": int
}
}
== Raw
{
#def: int
"#def": int
_def: int
_#def: int
a: {
#def: int
"#def": int
_def: int
_#def: int
}
}
== Final
{
"#def": int
a: {
"#def": int
}
}
== All
{
#def: int
"#def": int
_def: int
_#def: int
a: {
#def: int
"#def": int
_def: int
_#def: int
}
}
== Eval
{
#def: int
"#def": int
a: {
#def: int
"#def": int
}
}

0 comments on commit a5daa16

Please sign in to comment.