any_object.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package jsoniter
  2. import (
  3. "reflect"
  4. "unsafe"
  5. )
  6. type objectLazyAny struct {
  7. baseAny
  8. cfg *frozenConfig
  9. buf []byte
  10. err error
  11. }
  12. func (any *objectLazyAny) ValueType() ValueType {
  13. return ObjectValue
  14. }
  15. func (any *objectLazyAny) MustBeValid() Any {
  16. return any
  17. }
  18. func (any *objectLazyAny) LastError() error {
  19. return any.err
  20. }
  21. func (any *objectLazyAny) ToBool() bool {
  22. return true
  23. }
  24. func (any *objectLazyAny) ToInt() int {
  25. return 0
  26. }
  27. func (any *objectLazyAny) ToInt32() int32 {
  28. return 0
  29. }
  30. func (any *objectLazyAny) ToInt64() int64 {
  31. return 0
  32. }
  33. func (any *objectLazyAny) ToUint() uint {
  34. return 0
  35. }
  36. func (any *objectLazyAny) ToUint32() uint32 {
  37. return 0
  38. }
  39. func (any *objectLazyAny) ToUint64() uint64 {
  40. return 0
  41. }
  42. func (any *objectLazyAny) ToFloat32() float32 {
  43. return 0
  44. }
  45. func (any *objectLazyAny) ToFloat64() float64 {
  46. return 0
  47. }
  48. func (any *objectLazyAny) ToString() string {
  49. return *(*string)(unsafe.Pointer(&any.buf))
  50. }
  51. func (any *objectLazyAny) ToVal(obj interface{}) {
  52. iter := any.cfg.BorrowIterator(any.buf)
  53. defer any.cfg.ReturnIterator(iter)
  54. iter.ReadVal(obj)
  55. }
  56. func (any *objectLazyAny) Get(path ...interface{}) Any {
  57. if len(path) == 0 {
  58. return any
  59. }
  60. switch firstPath := path[0].(type) {
  61. case string:
  62. iter := any.cfg.BorrowIterator(any.buf)
  63. defer any.cfg.ReturnIterator(iter)
  64. valueBytes := locateObjectField(iter, firstPath)
  65. if valueBytes == nil {
  66. return newInvalidAny(path)
  67. }
  68. iter.ResetBytes(valueBytes)
  69. return locatePath(iter, path[1:])
  70. case int32:
  71. if '*' == firstPath {
  72. mappedAll := map[string]Any{}
  73. iter := any.cfg.BorrowIterator(any.buf)
  74. defer any.cfg.ReturnIterator(iter)
  75. iter.ReadMapCB(func(iter *Iterator, field string) bool {
  76. mapped := locatePath(iter, path[1:])
  77. if mapped.ValueType() != InvalidValue {
  78. mappedAll[field] = mapped
  79. }
  80. return true
  81. })
  82. return wrapMap(mappedAll)
  83. }
  84. return newInvalidAny(path)
  85. default:
  86. return newInvalidAny(path)
  87. }
  88. }
  89. func (any *objectLazyAny) Keys() []string {
  90. keys := []string{}
  91. iter := any.cfg.BorrowIterator(any.buf)
  92. defer any.cfg.ReturnIterator(iter)
  93. iter.ReadMapCB(func(iter *Iterator, field string) bool {
  94. iter.Skip()
  95. keys = append(keys, field)
  96. return true
  97. })
  98. return keys
  99. }
  100. func (any *objectLazyAny) Size() int {
  101. size := 0
  102. iter := any.cfg.BorrowIterator(any.buf)
  103. defer any.cfg.ReturnIterator(iter)
  104. iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  105. iter.Skip()
  106. size++
  107. return true
  108. })
  109. return size
  110. }
  111. func (any *objectLazyAny) WriteTo(stream *Stream) {
  112. stream.Write(any.buf)
  113. }
  114. func (any *objectLazyAny) GetInterface() interface{} {
  115. iter := any.cfg.BorrowIterator(any.buf)
  116. defer any.cfg.ReturnIterator(iter)
  117. return iter.Read()
  118. }
  119. type objectAny struct {
  120. baseAny
  121. err error
  122. val reflect.Value
  123. }
  124. func wrapStruct(val interface{}) *objectAny {
  125. return &objectAny{baseAny{}, nil, reflect.ValueOf(val)}
  126. }
  127. func (any *objectAny) ValueType() ValueType {
  128. return ObjectValue
  129. }
  130. func (any *objectAny) MustBeValid() Any {
  131. return any
  132. }
  133. func (any *objectAny) Parse() *Iterator {
  134. return nil
  135. }
  136. func (any *objectAny) LastError() error {
  137. return any.err
  138. }
  139. func (any *objectAny) ToBool() bool {
  140. return any.val.NumField() != 0
  141. }
  142. func (any *objectAny) ToInt() int {
  143. return 0
  144. }
  145. func (any *objectAny) ToInt32() int32 {
  146. return 0
  147. }
  148. func (any *objectAny) ToInt64() int64 {
  149. return 0
  150. }
  151. func (any *objectAny) ToUint() uint {
  152. return 0
  153. }
  154. func (any *objectAny) ToUint32() uint32 {
  155. return 0
  156. }
  157. func (any *objectAny) ToUint64() uint64 {
  158. return 0
  159. }
  160. func (any *objectAny) ToFloat32() float32 {
  161. return 0
  162. }
  163. func (any *objectAny) ToFloat64() float64 {
  164. return 0
  165. }
  166. func (any *objectAny) ToString() string {
  167. str, err := MarshalToString(any.val.Interface())
  168. any.err = err
  169. return str
  170. }
  171. func (any *objectAny) Get(path ...interface{}) Any {
  172. if len(path) == 0 {
  173. return any
  174. }
  175. switch firstPath := path[0].(type) {
  176. case string:
  177. field := any.val.FieldByName(firstPath)
  178. if !field.IsValid() {
  179. return newInvalidAny(path)
  180. }
  181. return Wrap(field.Interface())
  182. case int32:
  183. if '*' == firstPath {
  184. mappedAll := map[string]Any{}
  185. for i := 0; i < any.val.NumField(); i++ {
  186. field := any.val.Field(i)
  187. if field.CanInterface() {
  188. mapped := Wrap(field.Interface()).Get(path[1:]...)
  189. if mapped.ValueType() != InvalidValue {
  190. mappedAll[any.val.Type().Field(i).Name] = mapped
  191. }
  192. }
  193. }
  194. return wrapMap(mappedAll)
  195. }
  196. return newInvalidAny(path)
  197. default:
  198. return newInvalidAny(path)
  199. }
  200. }
  201. func (any *objectAny) Keys() []string {
  202. keys := make([]string, 0, any.val.NumField())
  203. for i := 0; i < any.val.NumField(); i++ {
  204. keys = append(keys, any.val.Type().Field(i).Name)
  205. }
  206. return keys
  207. }
  208. func (any *objectAny) Size() int {
  209. return any.val.NumField()
  210. }
  211. func (any *objectAny) WriteTo(stream *Stream) {
  212. stream.WriteVal(any.val)
  213. }
  214. func (any *objectAny) GetInterface() interface{} {
  215. return any.val.Interface()
  216. }
  217. type mapAny struct {
  218. baseAny
  219. err error
  220. val reflect.Value
  221. }
  222. func wrapMap(val interface{}) *mapAny {
  223. return &mapAny{baseAny{}, nil, reflect.ValueOf(val)}
  224. }
  225. func (any *mapAny) ValueType() ValueType {
  226. return ObjectValue
  227. }
  228. func (any *mapAny) MustBeValid() Any {
  229. return any
  230. }
  231. func (any *mapAny) Parse() *Iterator {
  232. return nil
  233. }
  234. func (any *mapAny) LastError() error {
  235. return any.err
  236. }
  237. func (any *mapAny) ToBool() bool {
  238. return true
  239. }
  240. func (any *mapAny) ToInt() int {
  241. return 0
  242. }
  243. func (any *mapAny) ToInt32() int32 {
  244. return 0
  245. }
  246. func (any *mapAny) ToInt64() int64 {
  247. return 0
  248. }
  249. func (any *mapAny) ToUint() uint {
  250. return 0
  251. }
  252. func (any *mapAny) ToUint32() uint32 {
  253. return 0
  254. }
  255. func (any *mapAny) ToUint64() uint64 {
  256. return 0
  257. }
  258. func (any *mapAny) ToFloat32() float32 {
  259. return 0
  260. }
  261. func (any *mapAny) ToFloat64() float64 {
  262. return 0
  263. }
  264. func (any *mapAny) ToString() string {
  265. str, err := MarshalToString(any.val.Interface())
  266. any.err = err
  267. return str
  268. }
  269. func (any *mapAny) Get(path ...interface{}) Any {
  270. if len(path) == 0 {
  271. return any
  272. }
  273. switch firstPath := path[0].(type) {
  274. case int32:
  275. if '*' == firstPath {
  276. mappedAll := map[string]Any{}
  277. for _, key := range any.val.MapKeys() {
  278. keyAsStr := key.String()
  279. element := Wrap(any.val.MapIndex(key).Interface())
  280. mapped := element.Get(path[1:]...)
  281. if mapped.ValueType() != InvalidValue {
  282. mappedAll[keyAsStr] = mapped
  283. }
  284. }
  285. return wrapMap(mappedAll)
  286. }
  287. return newInvalidAny(path)
  288. default:
  289. value := any.val.MapIndex(reflect.ValueOf(firstPath))
  290. if !value.IsValid() {
  291. return newInvalidAny(path)
  292. }
  293. return Wrap(value.Interface())
  294. }
  295. }
  296. func (any *mapAny) Keys() []string {
  297. keys := make([]string, 0, any.val.Len())
  298. for _, key := range any.val.MapKeys() {
  299. keys = append(keys, key.String())
  300. }
  301. return keys
  302. }
  303. func (any *mapAny) Size() int {
  304. return any.val.Len()
  305. }
  306. func (any *mapAny) WriteTo(stream *Stream) {
  307. stream.WriteVal(any.val)
  308. }
  309. func (any *mapAny) GetInterface() interface{} {
  310. return any.val.Interface()
  311. }