json_map.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package xvdoc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "github.com/xuri/excelize/v2"
  8. )
  9. type JSONMap map[string]interface{}
  10. func (s JSONMap) Bool(key string, defaultVal bool) bool {
  11. if res, check := s[key].(bool); check {
  12. return res
  13. }
  14. return defaultVal
  15. }
  16. func (s JSONMap) Int(key string, defaultVal int64) int64 {
  17. if iface, check := s[key]; check {
  18. rVal := reflect.ValueOf(iface)
  19. switch rVal.Kind() {
  20. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  21. return rVal.Int()
  22. case reflect.Float32, reflect.Float64:
  23. return int64(rVal.Float())
  24. }
  25. }
  26. return defaultVal
  27. }
  28. func (s JSONMap) Value(key string, defaultVal interface{}) interface{} {
  29. if iface, check := s[key]; check {
  30. dVal := reflect.ValueOf(defaultVal)
  31. if !dVal.IsValid() {
  32. return iface
  33. } else {
  34. lVal := reflect.ValueOf(iface)
  35. if !lVal.IsValid() {
  36. return defaultVal
  37. } else if lVal.Kind() == dVal.Kind() {
  38. return iface
  39. } else if lVal.Type().ConvertibleTo(dVal.Type()) {
  40. return lVal.Convert(dVal.Type()).Interface()
  41. } else {
  42. if lVal.Kind() == reflect.String {
  43. switch dVal.Kind() {
  44. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  45. if val, err := strconv.Atoi(lVal.String()); err != nil {
  46. return defaultVal
  47. } else {
  48. lVal = reflect.ValueOf(val)
  49. return lVal.Convert(dVal.Type())
  50. }
  51. case reflect.Float32, reflect.Float64:
  52. if val, err := strconv.ParseFloat(lVal.String(), 64); err != nil {
  53. return defaultVal
  54. } else {
  55. lVal = reflect.ValueOf(val)
  56. return lVal.Convert(dVal.Type())
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. return defaultVal
  64. }
  65. func (s JSONMap) StringVal(key, defaultVal string) string {
  66. if iface, check := s[key]; check {
  67. return fmt.Sprint(iface)
  68. }
  69. return defaultVal
  70. }
  71. func (s JSONMap) Interface(key string) interface{} {
  72. return s[key]
  73. }
  74. func (s JSONMap) StringArray(key string) (res []string) {
  75. if arr, check := s[key].([]interface{}); check {
  76. res = make([]string, len(arr))
  77. for i, v := range arr {
  78. res[i] = fmt.Sprint(v)
  79. }
  80. }
  81. return
  82. }
  83. func (s JSONMap) Array(key string) (res []interface{}) {
  84. if arr, check := s[key].([]interface{}); check {
  85. res = arr
  86. }
  87. return
  88. }
  89. func (s JSONMap) JSONMap(key string) (res JSONMap) {
  90. if m, check := s[key].(map[string]interface{}); check {
  91. res = JSONMap(m)
  92. }
  93. return
  94. }
  95. func (s JSONMap) KeyExists(key string) (check bool) {
  96. _, check = s[key]
  97. return check
  98. }
  99. func (s JSONMap) Copy() JSONMap {
  100. res := make(JSONMap)
  101. for key, val := range s {
  102. res[key] = val
  103. }
  104. return res
  105. }
  106. func (s JSONMap) JSON() (res []byte) {
  107. res, _ = json.Marshal(&s)
  108. return
  109. }
  110. func (s JSONMap) Map() map[string]interface{} {
  111. return map[string]interface{}(s)
  112. }
  113. func copyMap(m map[string]interface{}) (res map[string]interface{}) {
  114. res = make(map[string]interface{})
  115. for key, val := range m {
  116. switch val.(type) {
  117. case []interface{}:
  118. res[key] = copySlice(val.([]interface{}))
  119. case map[string]interface{}:
  120. res[key] = copyMap(val.(map[string]interface{}))
  121. default:
  122. res[key] = val
  123. }
  124. }
  125. return
  126. }
  127. func copySlice(arr []interface{}) (res []interface{}) {
  128. res = make([]interface{}, len(arr))
  129. for i, v := range arr {
  130. switch v.(type) {
  131. case []interface{}:
  132. res[i] = copySlice(v.([]interface{}))
  133. case map[string]interface{}:
  134. res[i] = copyMap(v.(map[string]interface{}))
  135. default:
  136. res[i] = v
  137. }
  138. }
  139. return
  140. }
  141. func mergeSlices(aSource, aMerge []interface{}) []interface{} {
  142. return append(aSource, aMerge...)
  143. }
  144. func mergeMaps(mSource, mMerge map[string]interface{}) (res map[string]interface{}) {
  145. res = copyMap(mSource)
  146. // merge result with result map
  147. for key, val := range mMerge {
  148. if sVal, check := res[key]; check {
  149. switch val.(type) {
  150. case map[string]interface{}:
  151. {
  152. if m, check := sVal.(map[string]interface{}); check {
  153. res[key] = mergeMaps(m, val.(map[string]interface{}))
  154. } else if val == nil {
  155. res[key] = val
  156. }
  157. }
  158. case []interface{}:
  159. {
  160. if arr, check := sVal.([]interface{}); check {
  161. res[key] = mergeSlices(arr, val.([]interface{}))
  162. } else if val == nil {
  163. res[key] = val
  164. }
  165. }
  166. default:
  167. res[key] = val
  168. }
  169. } else {
  170. switch val.(type) {
  171. case map[string]interface{}:
  172. res[key] = copyMap(val.(map[string]interface{}))
  173. case []interface{}:
  174. res[key] = copySlice(val.([]interface{}))
  175. default:
  176. res[key] = val
  177. }
  178. }
  179. }
  180. return
  181. }
  182. func (s JSONMap) ToGraphicOptions() (res *excelize.GraphicOptions) {
  183. res = &excelize.GraphicOptions{
  184. LockAspectRatio: s.Bool("lock_aspect_ratio", false),
  185. AutoFit: s.Bool("autofit", false),
  186. OffsetX: int(s.Int("offset_x", 0)),
  187. OffsetY: int(s.Int("offset_y", 0)),
  188. Hyperlink: s.StringVal("link", ""),
  189. HyperlinkType: s.StringVal("link_type", ""),
  190. Positioning: s.StringVal("positioning", ""),
  191. }
  192. return
  193. }
  194. func (s JSONMap) ToStyle() (res *excelize.Style) {
  195. res = &excelize.Style{}
  196. // borders
  197. if arr := s.Array("border"); len(arr) > 0 {
  198. for _, val := range arr {
  199. if m, check := val.(map[string]any); check {
  200. jm := JSONMap(m)
  201. res.Border = append(res.Border, excelize.Border{
  202. Type: jm.StringVal("type", "top"),
  203. Color: jm.StringVal("color", "#000000"),
  204. Style: int(jm.Int("style", 0)),
  205. })
  206. }
  207. }
  208. }
  209. if s.KeyExists("fill") {
  210. mf := s.JSONMap("fill")
  211. res.Fill = excelize.Fill{
  212. Type: mf.StringVal("type", ""),
  213. Pattern: int(mf.Int("pattern", 0)),
  214. }
  215. for _, color := range mf.Array("color") {
  216. res.Fill.Color = append(res.Fill.Color, fmt.Sprint(color))
  217. }
  218. // Shading ???
  219. }
  220. if s.KeyExists("font") {
  221. mf := s.JSONMap("font")
  222. res.Font = &excelize.Font{
  223. Bold: mf.Bool("bold", false),
  224. Italic: mf.Bool("italic", false),
  225. Underline: mf.StringVal("underline", ""),
  226. Family: mf.StringVal("family", ""),
  227. Size: float64(mf.Int("size", 0)),
  228. Strike: mf.Bool("strike", false),
  229. Color: mf.StringVal("color", "#000000"),
  230. //VertAlign: ???,
  231. }
  232. }
  233. if s.KeyExists("alignment") {
  234. ma := s.JSONMap("alignment")
  235. res.Alignment = &excelize.Alignment{
  236. Horizontal: ma.StringVal("horizontal", ""),
  237. Vertical: ma.StringVal("vertical", ""),
  238. WrapText: ma.Bool("wrap", false),
  239. }
  240. }
  241. return
  242. }
  243. func mapJSON(m map[string]interface{}) (res []byte) {
  244. res, _ = json.Marshal(&m)
  245. return
  246. }