map.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package json
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. )
  8. // Map type
  9. type Map map[string]interface{}
  10. // New init Map object
  11. func NewMap() Map {
  12. return make(Map)
  13. }
  14. // FromInterface convert map[string]interface{} or Map interface to Map
  15. func MapFromInterface(iface interface{}) (res Map) {
  16. switch val := iface.(type) {
  17. case map[string]interface{}:
  18. res = FromMap(val)
  19. case Map:
  20. res = val
  21. default:
  22. res = NewMap()
  23. }
  24. return
  25. }
  26. // FromMap convert map[string]interface{} to Map object
  27. func FromMap(m map[string]interface{}) Map {
  28. return Map(m)
  29. }
  30. // Bool returns bool value by key
  31. func (s Map) Bool(key string, defaultVal bool) bool {
  32. if res, check := s[key].(bool); check {
  33. return res
  34. }
  35. return defaultVal
  36. }
  37. // KeyExists check value exists by key
  38. func (s Map) KeyExists(key string) bool {
  39. _, check := s[key]
  40. return check
  41. }
  42. // KeysExists check values exists by keys list.
  43. // Returns the first blank key found.
  44. // If all keys are defined, an empty string will be returned
  45. func (s Map) KeysExists(keys []string) string {
  46. for _, key := range keys {
  47. if _, check := s[key]; !check {
  48. return key
  49. }
  50. }
  51. return ""
  52. }
  53. func val(l, r interface{}) (res reflect.Value) {
  54. lVal, rVal := reflect.ValueOf(l), reflect.ValueOf(r)
  55. if lVal.Kind() == reflect.Ptr && rVal.Kind() != reflect.Ptr {
  56. return val(lVal.Elem().Interface(), r)
  57. }
  58. defer func() {
  59. if r := recover(); r != nil {
  60. res = rVal
  61. }
  62. }()
  63. res = lVal.Convert(rVal.Type())
  64. return
  65. }
  66. // Int returns int64 value by key.
  67. // If key isn't defined will be returned defaultVal arg value
  68. func (s Map) Int(key string, defaultVal int64) int64 {
  69. if iface, check := s[key]; check {
  70. return val(iface, defaultVal).Int()
  71. }
  72. return defaultVal
  73. }
  74. func (s Map) Int32(key string, defaultVal int) int {
  75. if iface, check := s[key]; check {
  76. return val(iface, defaultVal).Interface().(int)
  77. }
  78. return defaultVal
  79. }
  80. // Value returns interface object with attempt to convert to defaultVal type.
  81. // If key isn't defined will be returned defaultVal arg value
  82. func (s Map) Value(key string, defaultVal interface{}) interface{} {
  83. // check value exists by key
  84. if iface, check := s[key]; check {
  85. // check defaultVal is valid
  86. dVal := reflect.ValueOf(defaultVal)
  87. if !dVal.IsValid() {
  88. // invalid, return arrived interface
  89. return iface
  90. }
  91. // defaultVal is valid, attempt to convert found value to defaultVal type
  92. lVal := reflect.ValueOf(iface)
  93. switch {
  94. case !lVal.IsValid():
  95. return defaultVal // invalid found value, return defaultVal
  96. case lVal.Kind() == dVal.Kind():
  97. return iface // types of found value and defaultVal is match. return found value
  98. case lVal.Type().ConvertibleTo(dVal.Type()):
  99. return lVal.Convert(dVal.Type()).Interface() // found value type can be converted to defaultVal type. return converted found value
  100. default:
  101. {
  102. // found value type can't be converted to defaultVal type. If found value is string, attempt to convert to number value
  103. if lVal.Kind() == reflect.String {
  104. switch dVal.Kind() {
  105. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  106. val, err := strconv.Atoi(lVal.String())
  107. if err != nil {
  108. return defaultVal
  109. }
  110. lVal = reflect.ValueOf(val)
  111. return lVal.Convert(dVal.Type())
  112. case reflect.Float32, reflect.Float64:
  113. val, err := strconv.ParseFloat(lVal.String(), 64)
  114. if err != nil {
  115. return defaultVal
  116. }
  117. lVal = reflect.ValueOf(val)
  118. return lVal.Convert(dVal.Type())
  119. }
  120. }
  121. }
  122. }
  123. }
  124. return defaultVal
  125. }
  126. // ValueJSON returns json source object by key
  127. // If key isn't defined will be returned defaultVal arg value
  128. func (s Map) ValueJSON(key string, defaultVal []byte) (res []byte) {
  129. res = defaultVal
  130. if s.KeyExists(key) {
  131. if r, err := json.Marshal(s[key]); err == nil {
  132. res = r
  133. }
  134. }
  135. return
  136. }
  137. // String returns string value by key
  138. // If key isn't defined will be returned defaultVal arg value
  139. func (s Map) String(key, defaultVal string) string {
  140. if iface, check := s[key]; check {
  141. return val(iface, defaultVal).String()
  142. //return fmt.Sprint(iface)
  143. }
  144. return defaultVal
  145. }
  146. // Slce returns slice of interface{} by key
  147. // If key isn't defined or have a different type will be returned defaultVal arg value
  148. func (s Map) Slice(key string, defaultVal []interface{}) (res []interface{}) {
  149. if arr, check := s[key].([]interface{}); check {
  150. res = arr
  151. } else {
  152. res = defaultVal
  153. }
  154. return
  155. }
  156. // StringSlice returns string slice by key
  157. // If key isn't defined will be returned defaultVal arg value
  158. func (s Map) StringSlice(key string, defaultVal []string) (res []string) {
  159. if arr, check := s[key].([]interface{}); check {
  160. res = make([]string, len(arr))
  161. for i, v := range arr {
  162. res[i] = fmt.Sprint(v)
  163. }
  164. } else {
  165. res = defaultVal
  166. }
  167. return
  168. }
  169. // Map returns Map object by key
  170. // If key isn't defined or have other type will be returned defaultVal arg value
  171. func (s Map) Map(key string, defaultVal Map) (res Map) {
  172. val := s[key]
  173. switch iface := val.(type) {
  174. case Map:
  175. res = iface
  176. case map[string]interface{}:
  177. res = Map(iface)
  178. default:
  179. res = defaultVal
  180. }
  181. return
  182. }
  183. // JSON Return JSON source of the self object
  184. func (s Map) JSON() (res []byte) {
  185. res, _ = json.Marshal(s)
  186. return
  187. }
  188. // ToMap returns map[string]interface{} of the self object
  189. func (s Map) ToMap() map[string]interface{} { return map[string]interface{}(s) }
  190. // Copy returns copied map (todo dipcopy)
  191. func (s Map) Copy() (res Map) {
  192. res = make(Map)
  193. for key, val := range s {
  194. res[key] = val
  195. }
  196. return
  197. }
  198. func (s Map) IsEmpty() bool {
  199. return len(s) == 0
  200. }
  201. func (s Map) Update(m Map) {
  202. for key, val := range m {
  203. s[key] = val
  204. }
  205. }
  206. func (s Map) StorePtr(key string, val interface{}) interface{} {
  207. s[key] = &val
  208. return &val
  209. }
  210. func (s Map) Variable(key string, ptr interface{}) bool {
  211. val, check := s[key]
  212. if !check {
  213. // value is not exists
  214. return false
  215. }
  216. vVal := reflect.ValueOf(ptr)
  217. if vVal.Kind() != reflect.Ptr {
  218. // ptr is not a pointer
  219. return false
  220. }
  221. vElem := vVal.Elem()
  222. rVal := reflect.ValueOf(val)
  223. if !rVal.CanConvert(vElem.Type()) {
  224. // type is not converted
  225. return false
  226. }
  227. vElem.Set(rVal.Convert(vElem.Type()))
  228. return true
  229. }