summaryrefslogtreecommitdiff
path: root/starlark_fmt/format.go
diff options
context:
space:
mode:
author Sam Delmerico <delmerico@google.com> 2022-03-25 16:33:26 +0000
committer Sam Delmerico <delmerico@google.com> 2022-04-08 14:15:43 +0000
commit932c01cf9efab7e8dc00b7faad470b5499ad04c0 (patch)
treed0a904907eda138f986c4d26ce13f06cc9c437f7 /starlark_fmt/format.go
parent7f88956c16b1bf10bf7a9187e9943c3fd33adc1a (diff)
export Java variables to Bazel
Test: build/bazel/bp2build.sh Change-Id: Ia06f9265c9f96e6add6edbd0cee925fe37b430d3
Diffstat (limited to 'starlark_fmt/format.go')
-rw-r--r--starlark_fmt/format.go13
1 files changed, 9 insertions, 4 deletions
diff --git a/starlark_fmt/format.go b/starlark_fmt/format.go
index 23eee59b3..3e51fa14c 100644
--- a/starlark_fmt/format.go
+++ b/starlark_fmt/format.go
@@ -39,21 +39,26 @@ func PrintBool(item bool) string {
// PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels.
func PrintStringList(items []string, indentLevel int) string {
- return PrintList(items, indentLevel, `"%s"`)
+ return PrintList(items, indentLevel, func(s string) string {
+ if strings.Contains(s, "\"") {
+ return `'''%s'''`
+ }
+ return `"%s"`
+ })
}
// PrintList returns a Starlark-compatible string of list formmated as requested.
-func PrintList(items []string, indentLevel int, formatString string) string {
+func PrintList(items []string, indentLevel int, formatString func(string) string) string {
if len(items) == 0 {
return "[]"
} else if len(items) == 1 {
- return fmt.Sprintf("["+formatString+"]", items[0])
+ return fmt.Sprintf("["+formatString(items[0])+"]", items[0])
}
list := make([]string, 0, len(items)+2)
list = append(list, "[")
innerIndent := Indention(indentLevel + 1)
for _, item := range items {
- list = append(list, fmt.Sprintf(`%s`+formatString+`,`, innerIndent, item))
+ list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item))
}
list = append(list, Indention(indentLevel)+"]")
return strings.Join(list, "\n")