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" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | const ( |
| 24 | indent = 4 |
| 25 | ) |
| 26 | |
| 27 | // Indention returns an indent string of the specified level. |
| 28 | func Indention(level int) string { |
| 29 | if level < 0 { |
| 30 | panic(fmt.Errorf("indent level cannot be less than 0, but got %d", level)) |
| 31 | } |
| 32 | return strings.Repeat(" ", level*indent) |
| 33 | } |
| 34 | |
| 35 | // PrintBool returns a Starlark compatible bool string. |
| 36 | func PrintBool(item bool) string { |
| 37 | return strings.Title(fmt.Sprintf("%t", item)) |
| 38 | } |
| 39 | |
| 40 | // PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels. |
| 41 | func PrintStringList(items []string, indentLevel int) string { |
| 42 | return PrintList(items, indentLevel, `"%s"`) |
| 43 | } |
| 44 | |
| 45 | // PrintList returns a Starlark-compatible string of list formmated as requested. |
| 46 | func PrintList(items []string, indentLevel int, formatString string) string { |
| 47 | if len(items) == 0 { |
| 48 | return "[]" |
| 49 | } else if len(items) == 1 { |
| 50 | return fmt.Sprintf("["+formatString+"]", items[0]) |
| 51 | } |
| 52 | list := make([]string, 0, len(items)+2) |
| 53 | list = append(list, "[") |
| 54 | innerIndent := Indention(indentLevel + 1) |
| 55 | for _, item := range items { |
| 56 | list = append(list, fmt.Sprintf(`%s`+formatString+`,`, innerIndent, item)) |
| 57 | } |
| 58 | list = append(list, Indention(indentLevel)+"]") |
| 59 | return strings.Join(list, "\n") |
| 60 | } |
| 61 | |
| 62 | // PrintStringListDict returns a Starlark-compatible string formatted as dictionary with |
| 63 | // string keys and list of string values. |
| 64 | func PrintStringListDict(dict map[string][]string, indentLevel int) string { |
| 65 | formattedValueDict := make(map[string]string, len(dict)) |
| 66 | for k, v := range dict { |
| 67 | formattedValueDict[k] = PrintStringList(v, indentLevel+1) |
| 68 | } |
| 69 | return PrintDict(formattedValueDict, indentLevel) |
| 70 | } |
| 71 | |
| 72 | // PrintBoolDict returns a starlark-compatible string containing a dictionary with string keys and |
| 73 | // values printed with no additional formatting. |
| 74 | func PrintBoolDict(dict map[string]bool, indentLevel int) string { |
| 75 | formattedValueDict := make(map[string]string, len(dict)) |
| 76 | for k, v := range dict { |
| 77 | formattedValueDict[k] = PrintBool(v) |
| 78 | } |
| 79 | return PrintDict(formattedValueDict, indentLevel) |
| 80 | } |
| 81 | |
| 82 | // PrintDict returns a starlark-compatible string containing a dictionary with string keys and |
| 83 | // values printed with no additional formatting. |
| 84 | func PrintDict(dict map[string]string, indentLevel int) string { |
| 85 | if len(dict) == 0 { |
| 86 | return "{}" |
| 87 | } |
| 88 | items := make([]string, 0, len(dict)) |
| 89 | for k, v := range dict { |
| 90 | items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v)) |
| 91 | } |
| 92 | sort.Strings(items) |
| 93 | return fmt.Sprintf(`{ |
| 94 | %s |
| 95 | %s}`, strings.Join(items, "\n"), Indention(indentLevel)) |
| 96 | } |