Skip to content

Commit

Permalink
internal/core/export: support byte interpolations
Browse files Browse the repository at this point in the history
Change-Id: I4dff3dafd0f836725c06a2882ca363b316a70d3d
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7904
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 7, 2020
1 parent 2ea30a2 commit 16809ea
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
35 changes: 27 additions & 8 deletions internal/core/export/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,38 @@ func (e *exporter) adt(expr adt.Expr, conjuncts []adt.Conjunct) ast.Expr {
return &ast.SliceExpr{X: e.expr(x.X), Low: lo, High: hi}

case *adt.Interpolation:
var (
tripple = `"""`
openQuote = `"`
closeQuote = `"`
f = literal.String
)
if x.K&adt.BytesKind != 0 {
tripple = `'''`
openQuote = `'`
closeQuote = `'`
f = literal.Bytes
}
toString := func(v adt.Expr) string {
str := ""
switch x := v.(type) {
case *adt.String:
str = x.Str
case *adt.Bytes:
str = string(x.B)
}
return str
}
t := &ast.Interpolation{}
f := literal.String.WithGraphicOnly() // TODO: also support bytes
openQuote := `"`
closeQuote := `"`
f = f.WithGraphicOnly()
indent := ""
// TODO: mark formatting in interpolation itself.
for i := 0; i < len(x.Parts); i += 2 {
str := x.Parts[i].(*adt.String).Str
if strings.IndexByte(str, '\n') >= 0 {
if strings.IndexByte(toString(x.Parts[i]), '\n') >= 0 {
f = f.WithTabIndent(len(e.stack))
indent = strings.Repeat("\t", len(e.stack))
openQuote = `"""` + "\n" + indent
closeQuote = `"""`
openQuote = tripple + "\n" + indent
closeQuote = tripple
break
}
}
Expand All @@ -247,7 +266,7 @@ func (e *exporter) adt(expr adt.Expr, conjuncts []adt.Conjunct) ast.Expr {
} else {
// b := strings.Builder{}
buf := []byte(prefix)
str := elem.(*adt.String).Str
str := toString(elem)
buf = f.AppendEscaped(buf, str)
if i == len(x.Parts)-1 {
if len(closeQuote) > 1 {
Expand Down
50 changes: 50 additions & 0 deletions internal/core/export/testdata/strings.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ b: "message: \(a)!"

c: d: a

bin1: '\(a)'

bin2: '''
multi
\(b)
'''

-- out/definition --
a: """
multi
Expand All @@ -17,12 +24,19 @@ b: "message: \(a)!"
c: {
d: a
}
bin1: '\(a)'
bin2: '''
multi
\(b)
'''
-- out/doc --
[]
[a]
[b]
[c]
[c d]
[bin1]
[bin2]
-- out/value --
== Simplified
{
Expand All @@ -40,6 +54,15 @@ c: {
line
"""
}
bin1: '''
multi
line
'''
bin2: '''
multi
message: multi
line!
'''
}
== Raw
{
Expand All @@ -57,6 +80,15 @@ c: {
line
"""
}
bin1: '''
multi
line
'''
bin2: '''
multi
message: multi
line!
'''
}
== Final
{
Expand All @@ -74,6 +106,15 @@ c: {
line
"""
}
bin1: '''
multi
line
'''
bin2: '''
multi
message: multi
line!
'''
}
== All
{
Expand All @@ -91,4 +132,13 @@ c: {
line
"""
}
bin1: '''
multi
line
'''
bin2: '''
multi
message: multi
line!
'''
}

0 comments on commit 16809ea

Please sign in to comment.