format.go 3.1 KB

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