format.go 3.9 KB

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