blob: a97f71b87ab6fe12feb496f93464bb49864051f5 [file] [log] [blame]
Liz Kammer72beb342022-02-03 08:42:10 -05001// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package starlark_fmt
16
17import (
18 "fmt"
19 "sort"
Alix Espino4fd7e742023-02-24 14:46:43 +000020 "strconv"
Liz Kammer72beb342022-02-03 08:42:10 -050021 "strings"
22)
23
24const (
25 indent = 4
26)
27
28// Indention returns an indent string of the specified level.
29func Indention(level int) string {
30 if level < 0 {
31 panic(fmt.Errorf("indent level cannot be less than 0, but got %d", level))
32 }
33 return strings.Repeat(" ", level*indent)
34}
35
36// PrintBool returns a Starlark compatible bool string.
37func PrintBool(item bool) string {
Cole Fausteb644cf2023-04-11 13:48:17 -070038 if item {
39 return "True"
40 } else {
41 return "False"
42 }
Liz Kammer72beb342022-02-03 08:42:10 -050043}
44
45// PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels.
46func PrintStringList(items []string, indentLevel int) string {
Sam Delmerico932c01c2022-03-25 16:33:26 +000047 return PrintList(items, indentLevel, func(s string) string {
48 if strings.Contains(s, "\"") {
49 return `'''%s'''`
50 }
51 return `"%s"`
52 })
Liz Kammer72beb342022-02-03 08:42:10 -050053}
54
55// PrintList returns a Starlark-compatible string of list formmated as requested.
Sam Delmerico932c01c2022-03-25 16:33:26 +000056func PrintList(items []string, indentLevel int, formatString func(string) string) string {
Liz Kammer72beb342022-02-03 08:42:10 -050057 if len(items) == 0 {
58 return "[]"
59 } else if len(items) == 1 {
Sam Delmerico932c01c2022-03-25 16:33:26 +000060 return fmt.Sprintf("["+formatString(items[0])+"]", items[0])
Liz Kammer72beb342022-02-03 08:42:10 -050061 }
62 list := make([]string, 0, len(items)+2)
63 list = append(list, "[")
64 innerIndent := Indention(indentLevel + 1)
65 for _, item := range items {
Sam Delmerico932c01c2022-03-25 16:33:26 +000066 list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item))
Liz Kammer72beb342022-02-03 08:42:10 -050067 }
68 list = append(list, Indention(indentLevel)+"]")
69 return strings.Join(list, "\n")
70}
71
72// PrintStringListDict returns a Starlark-compatible string formatted as dictionary with
73// string keys and list of string values.
74func PrintStringListDict(dict map[string][]string, indentLevel int) string {
75 formattedValueDict := make(map[string]string, len(dict))
76 for k, v := range dict {
77 formattedValueDict[k] = PrintStringList(v, indentLevel+1)
78 }
79 return PrintDict(formattedValueDict, indentLevel)
80}
81
82// PrintBoolDict returns a starlark-compatible string containing a dictionary with string keys and
83// values printed with no additional formatting.
84func PrintBoolDict(dict map[string]bool, indentLevel int) string {
85 formattedValueDict := make(map[string]string, len(dict))
86 for k, v := range dict {
87 formattedValueDict[k] = PrintBool(v)
88 }
89 return PrintDict(formattedValueDict, indentLevel)
90}
91
Alix Espino4fd7e742023-02-24 14:46:43 +000092// PrintStringIntDict returns a Starlark-compatible string formatted as dictionary with
93// string keys and int values.
94func PrintStringIntDict(dict map[string]int, indentLevel int) string {
95 valDict := make(map[string]string, len(dict))
96 for k, v := range dict {
97 valDict[k] = strconv.Itoa(v)
98 }
99 return PrintDict(valDict, indentLevel)
100}
101
Liz Kammer72beb342022-02-03 08:42:10 -0500102// PrintDict returns a starlark-compatible string containing a dictionary with string keys and
103// values printed with no additional formatting.
104func PrintDict(dict map[string]string, indentLevel int) string {
105 if len(dict) == 0 {
106 return "{}"
107 }
108 items := make([]string, 0, len(dict))
109 for k, v := range dict {
110 items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v))
111 }
112 sort.Strings(items)
113 return fmt.Sprintf(`{
114%s
115%s}`, strings.Join(items, "\n"), Indention(indentLevel))
116}