Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package starlark_fmt |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 20 | "strconv" |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | indent = 4 |
| 26 | ) |
| 27 | |
| 28 | // Indention returns an indent string of the specified level. |
| 29 | func 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. |
| 37 | func PrintBool(item bool) string { |
Cole Faust | eb644cf | 2023-04-11 13:48:17 -0700 | [diff] [blame] | 38 | if item { |
| 39 | return "True" |
| 40 | } else { |
| 41 | return "False" |
| 42 | } |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels. |
| 46 | func PrintStringList(items []string, indentLevel int) string { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 47 | return PrintList(items, indentLevel, func(s string) string { |
| 48 | if strings.Contains(s, "\"") { |
| 49 | return `'''%s'''` |
| 50 | } |
| 51 | return `"%s"` |
| 52 | }) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // PrintList returns a Starlark-compatible string of list formmated as requested. |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 56 | func PrintList(items []string, indentLevel int, formatString func(string) string) string { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 57 | if len(items) == 0 { |
| 58 | return "[]" |
| 59 | } else if len(items) == 1 { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 60 | return fmt.Sprintf("["+formatString(items[0])+"]", items[0]) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 61 | } |
| 62 | list := make([]string, 0, len(items)+2) |
| 63 | list = append(list, "[") |
| 64 | innerIndent := Indention(indentLevel + 1) |
| 65 | for _, item := range items { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 66 | list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item)) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 67 | } |
| 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. |
| 74 | func 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. |
| 84 | func 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 Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 92 | // PrintStringIntDict returns a Starlark-compatible string formatted as dictionary with |
| 93 | // string keys and int values. |
| 94 | func 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 | |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 102 | // PrintStringStringDict returns a Starlark-compatible string formatted as dictionary with |
| 103 | // string keys and string values. |
| 104 | func PrintStringStringDict(dict map[string]string, indentLevel int) string { |
| 105 | valDict := make(map[string]string, len(dict)) |
| 106 | for k, v := range dict { |
| 107 | valDict[k] = fmt.Sprintf(`"%s"`, v) |
| 108 | } |
| 109 | return PrintDict(valDict, indentLevel) |
| 110 | } |
| 111 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 112 | // PrintDict returns a starlark-compatible string containing a dictionary with string keys and |
| 113 | // values printed with no additional formatting. |
| 114 | func PrintDict(dict map[string]string, indentLevel int) string { |
| 115 | if len(dict) == 0 { |
| 116 | return "{}" |
| 117 | } |
| 118 | items := make([]string, 0, len(dict)) |
| 119 | for k, v := range dict { |
| 120 | items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v)) |
| 121 | } |
| 122 | sort.Strings(items) |
| 123 | return fmt.Sprintf(`{ |
| 124 | %s |
| 125 | %s}`, strings.Join(items, "\n"), Indention(indentLevel)) |
| 126 | } |