structure.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2013 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 pretty
  15. import (
  16. "bytes"
  17. "strconv"
  18. "strings"
  19. )
  20. type node interface {
  21. WriteTo(w *bytes.Buffer, indent string, cfg *Config)
  22. }
  23. func compactString(n node) string {
  24. switch k := n.(type) {
  25. case stringVal:
  26. return string(k)
  27. case rawVal:
  28. return string(k)
  29. }
  30. buf := new(bytes.Buffer)
  31. n.WriteTo(buf, "", &Config{Compact: true})
  32. return buf.String()
  33. }
  34. type stringVal string
  35. func (str stringVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
  36. w.WriteString(strconv.Quote(string(str)))
  37. }
  38. type rawVal string
  39. func (r rawVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
  40. w.WriteString(string(r))
  41. }
  42. type keyval struct {
  43. key string
  44. val node
  45. }
  46. type keyvals []keyval
  47. func (l keyvals) Len() int { return len(l) }
  48. func (l keyvals) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
  49. func (l keyvals) Less(i, j int) bool { return l[i].key < l[j].key }
  50. func (l keyvals) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
  51. w.WriteByte('{')
  52. switch {
  53. case cfg.Compact:
  54. // All on one line:
  55. for i, kv := range l {
  56. if i > 0 {
  57. w.WriteByte(',')
  58. }
  59. w.WriteString(kv.key)
  60. w.WriteByte(':')
  61. kv.val.WriteTo(w, indent, cfg)
  62. }
  63. case cfg.Diffable:
  64. w.WriteByte('\n')
  65. inner := indent + " "
  66. // Each value gets its own line:
  67. for _, kv := range l {
  68. w.WriteString(inner)
  69. w.WriteString(kv.key)
  70. w.WriteString(": ")
  71. kv.val.WriteTo(w, inner, cfg)
  72. w.WriteString(",\n")
  73. }
  74. w.WriteString(indent)
  75. default:
  76. keyWidth := 0
  77. for _, kv := range l {
  78. if kw := len(kv.key); kw > keyWidth {
  79. keyWidth = kw
  80. }
  81. }
  82. alignKey := indent + " "
  83. alignValue := strings.Repeat(" ", keyWidth)
  84. inner := alignKey + alignValue + " "
  85. // First and last line shared with bracket:
  86. for i, kv := range l {
  87. if i > 0 {
  88. w.WriteString(",\n")
  89. w.WriteString(alignKey)
  90. }
  91. w.WriteString(kv.key)
  92. w.WriteString(": ")
  93. w.WriteString(alignValue[len(kv.key):])
  94. kv.val.WriteTo(w, inner, cfg)
  95. }
  96. }
  97. w.WriteByte('}')
  98. }
  99. type list []node
  100. func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
  101. if max := cfg.ShortList; max > 0 {
  102. short := compactString(l)
  103. if len(short) <= max {
  104. w.WriteString(short)
  105. return
  106. }
  107. }
  108. w.WriteByte('[')
  109. switch {
  110. case cfg.Compact:
  111. // All on one line:
  112. for i, v := range l {
  113. if i > 0 {
  114. w.WriteByte(',')
  115. }
  116. v.WriteTo(w, indent, cfg)
  117. }
  118. case cfg.Diffable:
  119. w.WriteByte('\n')
  120. inner := indent + " "
  121. // Each value gets its own line:
  122. for _, v := range l {
  123. w.WriteString(inner)
  124. v.WriteTo(w, inner, cfg)
  125. w.WriteString(",\n")
  126. }
  127. w.WriteString(indent)
  128. default:
  129. inner := indent + " "
  130. // First and last line shared with bracket:
  131. for i, v := range l {
  132. if i > 0 {
  133. w.WriteString(",\n")
  134. w.WriteString(inner)
  135. }
  136. v.WriteTo(w, inner, cfg)
  137. }
  138. }
  139. w.WriteByte(']')
  140. }