functor.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package optdec
  2. import (
  3. "encoding/json"
  4. "math"
  5. "unsafe"
  6. "github.com/bytedance/sonic/internal/rt"
  7. "github.com/bytedance/sonic/internal/resolver"
  8. )
  9. type decFunc interface {
  10. FromDom(vp unsafe.Pointer, node Node, ctx *context) error
  11. }
  12. type ptrDecoder struct {
  13. typ *rt.GoType
  14. deref decFunc
  15. }
  16. // Pointer Value is allocated in the Caller
  17. func (d *ptrDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  18. if node.IsNull() {
  19. *(*unsafe.Pointer)(vp) = nil
  20. return nil
  21. }
  22. if *(*unsafe.Pointer)(vp) == nil {
  23. *(*unsafe.Pointer)(vp) = rt.Mallocgc(d.typ.Size, d.typ, true)
  24. }
  25. return d.deref.FromDom(*(*unsafe.Pointer)(vp), node, ctx)
  26. }
  27. type embeddedFieldPtrDecoder struct {
  28. field resolver.FieldMeta
  29. fieldDec decFunc
  30. fieldName string
  31. }
  32. // Pointer Value is allocated in the Caller
  33. func (d *embeddedFieldPtrDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  34. if node.IsNull() {
  35. return nil
  36. }
  37. // seek into the pointer
  38. vp = unsafe.Pointer(uintptr(vp) - uintptr(d.field.Path[0].Size))
  39. for _, f := range d.field.Path {
  40. deref := rt.UnpackType(f.Type)
  41. vp = unsafe.Pointer(uintptr(vp) + f.Size)
  42. if f.Kind == resolver.F_deref {
  43. if *(*unsafe.Pointer)(vp) == nil {
  44. *(*unsafe.Pointer)(vp) = rt.Mallocgc(deref.Size, deref, true)
  45. }
  46. vp = *(*unsafe.Pointer)(vp)
  47. }
  48. }
  49. return d.fieldDec.FromDom(vp, node, ctx)
  50. }
  51. type i8Decoder struct{}
  52. func (d *i8Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  53. if node.IsNull() {
  54. return nil
  55. }
  56. ret, ok := node.AsI64(ctx)
  57. if !ok || ret > math.MaxInt8 || ret < math.MinInt8 {
  58. return error_mismatch(node, ctx, int8Type)
  59. }
  60. *(*int8)(vp) = int8(ret)
  61. return nil
  62. }
  63. type i16Decoder struct{}
  64. func (d *i16Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  65. if node.IsNull() {
  66. return nil
  67. }
  68. ret, ok := node.AsI64(ctx)
  69. if !ok || ret > math.MaxInt16 || ret < math.MinInt16 {
  70. return error_mismatch(node, ctx, int16Type)
  71. }
  72. *(*int16)(vp) = int16(ret)
  73. return nil
  74. }
  75. type i32Decoder struct{}
  76. func (d *i32Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  77. if node.IsNull() {
  78. return nil
  79. }
  80. ret, ok := node.AsI64(ctx)
  81. if !ok || ret > math.MaxInt32 || ret < math.MinInt32 {
  82. return error_mismatch(node, ctx, int32Type)
  83. }
  84. *(*int32)(vp) = int32(ret)
  85. return nil
  86. }
  87. type i64Decoder struct{}
  88. func (d *i64Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  89. if node.IsNull() {
  90. return nil
  91. }
  92. ret, ok := node.AsI64(ctx)
  93. if !ok {
  94. return error_mismatch(node, ctx, int64Type)
  95. }
  96. *(*int64)(vp) = int64(ret)
  97. return nil
  98. }
  99. type u8Decoder struct{}
  100. func (d *u8Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  101. if node.IsNull() {
  102. return nil
  103. }
  104. ret, ok := node.AsU64(ctx)
  105. if !ok || ret > math.MaxUint8 {
  106. err := error_mismatch(node, ctx, uint8Type)
  107. return err
  108. }
  109. *(*uint8)(vp) = uint8(ret)
  110. return nil
  111. }
  112. type u16Decoder struct{}
  113. func (d *u16Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  114. if node.IsNull() {
  115. return nil
  116. }
  117. ret, ok := node.AsU64(ctx)
  118. if !ok || ret > math.MaxUint16 {
  119. return error_mismatch(node, ctx, uint16Type)
  120. }
  121. *(*uint16)(vp) = uint16(ret)
  122. return nil
  123. }
  124. type u32Decoder struct{}
  125. func (d *u32Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  126. if node.IsNull() {
  127. return nil
  128. }
  129. ret, ok := node.AsU64(ctx)
  130. if !ok || ret > math.MaxUint32 {
  131. return error_mismatch(node, ctx, uint32Type)
  132. }
  133. *(*uint32)(vp) = uint32(ret)
  134. return nil
  135. }
  136. type u64Decoder struct{}
  137. func (d *u64Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  138. if node.IsNull() {
  139. return nil
  140. }
  141. ret, ok := node.AsU64(ctx)
  142. if !ok {
  143. return error_mismatch(node, ctx, uint64Type)
  144. }
  145. *(*uint64)(vp) = uint64(ret)
  146. return nil
  147. }
  148. type f32Decoder struct{}
  149. func (d *f32Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  150. if node.IsNull() {
  151. return nil
  152. }
  153. ret, ok := node.AsF64(ctx)
  154. if !ok || ret > math.MaxFloat32 || ret < -math.MaxFloat32 {
  155. return error_mismatch(node, ctx, float32Type)
  156. }
  157. *(*float32)(vp) = float32(ret)
  158. return nil
  159. }
  160. type f64Decoder struct{}
  161. func (d *f64Decoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  162. if node.IsNull() {
  163. return nil
  164. }
  165. ret, ok := node.AsF64(ctx)
  166. if !ok {
  167. return error_mismatch(node, ctx, float64Type)
  168. }
  169. *(*float64)(vp) = float64(ret)
  170. return nil
  171. }
  172. type boolDecoder struct {
  173. }
  174. func (d *boolDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  175. if node.IsNull() {
  176. return nil
  177. }
  178. ret, ok := node.AsBool()
  179. if !ok {
  180. return error_mismatch(node, ctx, boolType)
  181. }
  182. *(*bool)(vp) = bool(ret)
  183. return nil
  184. }
  185. type stringDecoder struct {
  186. }
  187. func (d *stringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  188. if node.IsNull() {
  189. return nil
  190. }
  191. ret, ok := node.AsStr(ctx)
  192. if !ok {
  193. return error_mismatch(node, ctx, stringType)
  194. }
  195. *(*string)(vp) = ret
  196. return nil
  197. }
  198. type numberDecoder struct {
  199. }
  200. func (d *numberDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  201. if node.IsNull() {
  202. return nil
  203. }
  204. num, ok := node.AsNumber(ctx)
  205. if !ok {
  206. return error_mismatch(node, ctx, jsonNumberType)
  207. }
  208. *(*json.Number)(vp) = num
  209. return nil
  210. }
  211. type recuriveDecoder struct {
  212. typ *rt.GoType
  213. }
  214. func (d *recuriveDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  215. dec, err := findOrCompile(d.typ)
  216. if err != nil {
  217. return err
  218. }
  219. return dec.FromDom(vp, node, ctx)
  220. }
  221. type unsupportedTypeDecoder struct {
  222. typ *rt.GoType
  223. }
  224. func (d *unsupportedTypeDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  225. if node.IsNull() {
  226. return nil
  227. }
  228. return error_unsuppoted(d.typ)
  229. }