mxj.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // mxj - A collection of map[string]interface{} and associated XML and JSON utilities.
  2. // Copyright 2012-2014 Charles Banning. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file
  5. package mxj
  6. import (
  7. "fmt"
  8. "sort"
  9. "strconv"
  10. )
  11. const (
  12. Cast = true // for clarity - e.g., mxj.NewMapXml(doc, mxj.Cast)
  13. SafeEncoding = true // ditto - e.g., mv.Json(mxj.SafeEncoding)
  14. )
  15. type Map map[string]interface{}
  16. // Allocate a Map.
  17. func New() Map {
  18. m := make(map[string]interface{}, 0)
  19. return m
  20. }
  21. // Cast a Map to map[string]interface{}
  22. func (mv Map) Old() map[string]interface{} {
  23. return mv
  24. }
  25. // Return a copy of mv as a newly allocated Map. If the Map only contains string,
  26. // numeric, map[string]interface{}, and []interface{} values, then it can be thought
  27. // of as a "deep copy." Copying a structure (or structure reference) value is subject
  28. // to the noted restrictions.
  29. // NOTE: If 'mv' includes structure values with, possibly, JSON encoding tags
  30. // then only public fields of the structure are in the new Map - and with
  31. // keys that conform to any encoding tag instructions. The structure itself will
  32. // be represented as a map[string]interface{} value.
  33. func (mv Map) Copy() (Map, error) {
  34. // this is the poor-man's deep copy
  35. // not efficient, but it works
  36. j, jerr := mv.Json()
  37. // must handle, we don't know how mv got built
  38. if jerr != nil {
  39. return nil, jerr
  40. }
  41. return NewMapJson(j)
  42. }
  43. // --------------- StringIndent ... from x2j.WriteMap -------------
  44. // Pretty print a Map.
  45. func (mv Map) StringIndent(offset ...int) string {
  46. return writeMap(map[string]interface{}(mv), true, offset...)
  47. }
  48. // Pretty print a Map without the value type information - just key:value entries.
  49. func (mv Map) StringIndentNoTypeInfo(offset ...int) string {
  50. return writeMapNoTypes(map[string]interface{}(mv), true, offset...)
  51. }
  52. // writeMap - dumps the map[string]interface{} for examination.
  53. // 'offset' is initial indentation count; typically: Write(m).
  54. func writeMap(m interface{}, root bool, offset ...int) string {
  55. var indent int
  56. if len(offset) == 1 {
  57. indent = offset[0]
  58. }
  59. var s string
  60. switch m.(type) {
  61. case nil:
  62. return "[nil] nil"
  63. case string:
  64. return "[string] " + m.(string)
  65. case int, int32, int64:
  66. return "[int] " + strconv.Itoa(m.(int))
  67. case float64, float32:
  68. return "[float64] " + strconv.FormatFloat(m.(float64), 'e', 2, 64)
  69. case bool:
  70. return "[bool] " + strconv.FormatBool(m.(bool))
  71. case []interface{}:
  72. s += "[[]interface{}]"
  73. for i, v := range m.([]interface{}) {
  74. s += "\n"
  75. for i := 0; i < indent; i++ {
  76. s += " "
  77. }
  78. s += "[item: " + strconv.FormatInt(int64(i), 10) + "]"
  79. switch v.(type) {
  80. case string, float64, bool:
  81. s += "\n"
  82. default:
  83. // noop
  84. }
  85. for i := 0; i < indent; i++ {
  86. s += " "
  87. }
  88. s += writeMap(v, false, indent+1)
  89. }
  90. case map[string]interface{}:
  91. list := make([][2]string, len(m.(map[string]interface{})))
  92. var n int
  93. for k, v := range m.(map[string]interface{}) {
  94. list[n][0] = k
  95. list[n][1] = writeMap(v, false, indent+1)
  96. n++
  97. }
  98. sort.Sort(mapList(list))
  99. for _, v := range list {
  100. if !root {
  101. s += "\n"
  102. }
  103. for i := 0; i < indent; i++ {
  104. s += " "
  105. }
  106. s += v[0] + " :" + v[1]
  107. }
  108. default:
  109. // shouldn't ever be here ...
  110. s += fmt.Sprintf("[unknown] %#v", m)
  111. }
  112. return s
  113. }
  114. // writeMapNoTypes - dumps the map[string]interface{} for examination.
  115. // 'offset' is initial indentation count; typically: Write(m).
  116. func writeMapNoTypes(m interface{}, root bool, offset ...int) string {
  117. var indent int
  118. if len(offset) == 1 {
  119. indent = offset[0]
  120. }
  121. var s string
  122. switch m.(type) {
  123. case nil:
  124. return "nil"
  125. case string:
  126. return m.(string)
  127. case float64:
  128. return strconv.FormatFloat(m.(float64), 'e', 2, 64)
  129. case bool:
  130. return strconv.FormatBool(m.(bool))
  131. case []interface{}:
  132. s += ""
  133. for i, v := range m.([]interface{}) {
  134. s += "\n"
  135. for i := 0; i < indent; i++ {
  136. s += " "
  137. }
  138. s += "[" + strconv.FormatInt(int64(i), 10) + "]"
  139. switch v.(type) {
  140. case string, float64, bool:
  141. s += "\n"
  142. default:
  143. // noop
  144. }
  145. for i := 0; i < indent; i++ {
  146. s += " "
  147. }
  148. s += writeMapNoTypes(v, false, indent+1)
  149. }
  150. case map[string]interface{}:
  151. list := make([][2]string, len(m.(map[string]interface{})))
  152. var n int
  153. for k, v := range m.(map[string]interface{}) {
  154. list[n][0] = k
  155. list[n][1] = writeMapNoTypes(v, false, indent+1)
  156. n++
  157. }
  158. sort.Sort(mapList(list))
  159. for _, v := range list {
  160. if !root {
  161. s += "\n"
  162. }
  163. for i := 0; i < indent; i++ {
  164. s += " "
  165. }
  166. s += v[0] + " :" + v[1]
  167. }
  168. default:
  169. // shouldn't ever be here ...
  170. s += fmt.Sprintf("[?] %#v", m)
  171. }
  172. return s
  173. }
  174. // ======================== utility ===============
  175. type mapList [][2]string
  176. func (ml mapList) Len() int {
  177. return len(ml)
  178. }
  179. func (ml mapList) Swap(i, j int) {
  180. ml[i], ml[j] = ml[j], ml[i]
  181. }
  182. func (ml mapList) Less(i, j int) bool {
  183. if ml[i][0] > ml[j][0] {
  184. return false
  185. }
  186. return true
  187. }