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 { |
| 38 | return strings.Title(fmt.Sprintf("%t", item)) |
| 39 | } |
| 40 | |
| 41 | // PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels. |
| 42 | func PrintStringList(items []string, indentLevel int) string { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 43 | return PrintList(items, indentLevel, func(s string) string { |
| 44 | if strings.Contains(s, "\"") { |
| 45 | return `'''%s'''` |
| 46 | } |
| 47 | return `"%s"` |
| 48 | }) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // PrintList returns a Starlark-compatible string of list formmated as requested. |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 52 | func PrintList(items []string, indentLevel int, formatString func(string) string) string { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 53 | if len(items) == 0 { |
| 54 | return "[]" |
| 55 | } else if len(items) == 1 { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 56 | return fmt.Sprintf("["+formatString(items[0])+"]", items[0]) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 57 | } |
| 58 | list := make([]string, 0, len(items)+2) |
| 59 | list = append(list, "[") |
| 60 | innerIndent := Indention(indentLevel + 1) |
| 61 | for _, item := range items { |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 62 | list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item)) |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 63 | } |
| 64 | list = append(list, Indention(indentLevel)+"]") |
| 65 | return strings.Join(list, "\n") |
| 66 | } |
| 67 | |
| 68 | // PrintStringListDict returns a Starlark-compatible string formatted as dictionary with |
| 69 | // string keys and list of string values. |
| 70 | func PrintStringListDict(dict map[string][]string, indentLevel int) string { |
| 71 | formattedValueDict := make(map[string]string, len(dict)) |
| 72 | for k, v := range dict { |
| 73 | formattedValueDict[k] = PrintStringList(v, indentLevel+1) |
| 74 | } |
| 75 | return PrintDict(formattedValueDict, indentLevel) |
| 76 | } |
| 77 | |
| 78 | // PrintBoolDict returns a starlark-compatible string containing a dictionary with string keys and |
| 79 | // values printed with no additional formatting. |
| 80 | func PrintBoolDict(dict map[string]bool, indentLevel int) string { |
| 81 | formattedValueDict := make(map[string]string, len(dict)) |
| 82 | for k, v := range dict { |
| 83 | formattedValueDict[k] = PrintBool(v) |
| 84 | } |
| 85 | return PrintDict(formattedValueDict, indentLevel) |
| 86 | } |
| 87 | |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame^] | 88 | // PrintStringIntDict returns a Starlark-compatible string formatted as dictionary with |
| 89 | // string keys and int values. |
| 90 | func PrintStringIntDict(dict map[string]int, indentLevel int) string { |
| 91 | valDict := make(map[string]string, len(dict)) |
| 92 | for k, v := range dict { |
| 93 | valDict[k] = strconv.Itoa(v) |
| 94 | } |
| 95 | return PrintDict(valDict, indentLevel) |
| 96 | } |
| 97 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 98 | // PrintDict returns a starlark-compatible string containing a dictionary with string keys and |
| 99 | // values printed with no additional formatting. |
| 100 | func PrintDict(dict map[string]string, indentLevel int) string { |
| 101 | if len(dict) == 0 { |
| 102 | return "{}" |
| 103 | } |
| 104 | items := make([]string, 0, len(dict)) |
| 105 | for k, v := range dict { |
| 106 | items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v)) |
| 107 | } |
| 108 | sort.Strings(items) |
| 109 | return fmt.Sprintf(`{ |
| 110 | %s |
| 111 | %s}`, strings.Join(items, "\n"), Indention(indentLevel)) |
| 112 | } |