Skip to content

Commit 16809ea

Browse files
committed
internal/core/export: support byte interpolations
Change-Id: I4dff3dafd0f836725c06a2882ca363b316a70d3d Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7904 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 2ea30a2 commit 16809ea

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

internal/core/export/adt.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,38 @@ func (e *exporter) adt(expr adt.Expr, conjuncts []adt.Conjunct) ast.Expr {
223223
return &ast.SliceExpr{X: e.expr(x.X), Low: lo, High: hi}
224224

225225
case *adt.Interpolation:
226+
var (
227+
tripple = `"""`
228+
openQuote = `"`
229+
closeQuote = `"`
230+
f = literal.String
231+
)
232+
if x.K&adt.BytesKind != 0 {
233+
tripple = `'''`
234+
openQuote = `'`
235+
closeQuote = `'`
236+
f = literal.Bytes
237+
}
238+
toString := func(v adt.Expr) string {
239+
str := ""
240+
switch x := v.(type) {
241+
case *adt.String:
242+
str = x.Str
243+
case *adt.Bytes:
244+
str = string(x.B)
245+
}
246+
return str
247+
}
226248
t := &ast.Interpolation{}
227-
f := literal.String.WithGraphicOnly() // TODO: also support bytes
228-
openQuote := `"`
229-
closeQuote := `"`
249+
f = f.WithGraphicOnly()
230250
indent := ""
231251
// TODO: mark formatting in interpolation itself.
232252
for i := 0; i < len(x.Parts); i += 2 {
233-
str := x.Parts[i].(*adt.String).Str
234-
if strings.IndexByte(str, '\n') >= 0 {
253+
if strings.IndexByte(toString(x.Parts[i]), '\n') >= 0 {
235254
f = f.WithTabIndent(len(e.stack))
236255
indent = strings.Repeat("\t", len(e.stack))
237-
openQuote = `"""` + "\n" + indent
238-
closeQuote = `"""`
256+
openQuote = tripple + "\n" + indent
257+
closeQuote = tripple
239258
break
240259
}
241260
}
@@ -247,7 +266,7 @@ func (e *exporter) adt(expr adt.Expr, conjuncts []adt.Conjunct) ast.Expr {
247266
} else {
248267
// b := strings.Builder{}
249268
buf := []byte(prefix)
250-
str := elem.(*adt.String).Str
269+
str := toString(elem)
251270
buf = f.AppendEscaped(buf, str)
252271
if i == len(x.Parts)-1 {
253272
if len(closeQuote) > 1 {

internal/core/export/testdata/strings.txtar

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ b: "message: \(a)!"
88

99
c: d: a
1010

11+
bin1: '\(a)'
12+
13+
bin2: '''
14+
multi
15+
\(b)
16+
'''
17+
1118
-- out/definition --
1219
a: """
1320
multi
@@ -17,12 +24,19 @@ b: "message: \(a)!"
1724
c: {
1825
d: a
1926
}
27+
bin1: '\(a)'
28+
bin2: '''
29+
multi
30+
\(b)
31+
'''
2032
-- out/doc --
2133
[]
2234
[a]
2335
[b]
2436
[c]
2537
[c d]
38+
[bin1]
39+
[bin2]
2640
-- out/value --
2741
== Simplified
2842
{
@@ -40,6 +54,15 @@ c: {
4054
line
4155
"""
4256
}
57+
bin1: '''
58+
multi
59+
line
60+
'''
61+
bin2: '''
62+
multi
63+
message: multi
64+
line!
65+
'''
4366
}
4467
== Raw
4568
{
@@ -57,6 +80,15 @@ c: {
5780
line
5881
"""
5982
}
83+
bin1: '''
84+
multi
85+
line
86+
'''
87+
bin2: '''
88+
multi
89+
message: multi
90+
line!
91+
'''
6092
}
6193
== Final
6294
{
@@ -74,6 +106,15 @@ c: {
74106
line
75107
"""
76108
}
109+
bin1: '''
110+
multi
111+
line
112+
'''
113+
bin2: '''
114+
multi
115+
message: multi
116+
line!
117+
'''
77118
}
78119
== All
79120
{
@@ -91,4 +132,13 @@ c: {
91132
line
92133
"""
93134
}
135+
bin1: '''
136+
multi
137+
line
138+
'''
139+
bin2: '''
140+
multi
141+
message: multi
142+
line!
143+
'''
94144
}

0 commit comments

Comments
 (0)