You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

union_handler.go 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package serder
  2. import (
  3. "fmt"
  4. "reflect"
  5. "unsafe"
  6. jsoniter "github.com/json-iterator/go"
  7. "github.com/modern-go/reflect2"
  8. "gitlink.org.cn/cloudream/common/pkgs/types"
  9. sertypes "gitlink.org.cn/cloudream/common/utils/serder/types"
  10. ref2 "gitlink.org.cn/cloudream/common/utils/reflect2"
  11. )
  12. type anyTypeUnionExternallyTagged struct {
  13. Union *types.AnyTypeUnion
  14. TypeNameToType map[string]reflect.Type
  15. }
  16. type TypeUnionExternallyTagged[T any] struct {
  17. anyTypeUnionExternallyTagged
  18. TUnion *types.TypeUnion[T]
  19. }
  20. // 遇到TypeUnion的基类(UnionType)的字段时,将其实际值的类型信息也编码到JSON中,反序列化时也会根据解析出类型信息,还原出真实的类型。
  21. // Externally Tagged的格式是:{ "类型名": {...对象内容...} }
  22. //
  23. // 可以通过内嵌Metadata结构体,并在它身上增加"union"Tag来指定类型名称,如果没有指定,则默认使用系统类型名(包括包路径)。
  24. func UseTypeUnionExternallyTagged[T any](union *types.TypeUnion[T]) *TypeUnionExternallyTagged[T] {
  25. eu := &TypeUnionExternallyTagged[T]{
  26. anyTypeUnionExternallyTagged: anyTypeUnionExternallyTagged{
  27. Union: union.ToAny(),
  28. TypeNameToType: make(map[string]reflect.Type),
  29. },
  30. TUnion: union,
  31. }
  32. for _, eleType := range union.ElementTypes {
  33. eu.Add(eleType)
  34. }
  35. unionHandler.externallyTagged[union.UnionType] = &eu.anyTypeUnionExternallyTagged
  36. return eu
  37. }
  38. func (u *TypeUnionExternallyTagged[T]) Add(typ reflect.Type) error {
  39. err := u.TUnion.Add(typ)
  40. if err != nil {
  41. return nil
  42. }
  43. u.TypeNameToType[makeDerefFullTypeName(typ)] = typ
  44. return nil
  45. }
  46. func (u *TypeUnionExternallyTagged[T]) AddT(nilValue T) error {
  47. u.Add(reflect.TypeOf(nilValue))
  48. return nil
  49. }
  50. type anyTypeUnionInternallyTagged struct {
  51. Union *types.AnyTypeUnion
  52. TagField string
  53. TagToType map[string]reflect.Type
  54. }
  55. type TypeUnionInternallyTagged[T any] struct {
  56. anyTypeUnionInternallyTagged
  57. TUnion *types.TypeUnion[T]
  58. }
  59. // 遇到TypeUnion的基类(UnionType)的字段时,将其实际值的类型信息也编码到JSON中,反序列化时也会解析出类型信息,还原出真实的类型。
  60. // Internally Tagged的格式是:{ "类型字段": "类型名", ...对象内容...},JSON中的类型字段名需要指定。
  61. // 注:对象定义需要包含类型字段,而且在序列化之前需要手动赋值,目前不支持自动设置。
  62. //
  63. // 可以通过内嵌Metadata结构体,并在它身上增加"union"Tag来指定类型名称,如果没有指定,则默认使用系统类型名(包括包路径)。
  64. func UseTypeUnionInternallyTagged[T any](union *types.TypeUnion[T], tagField string) *TypeUnionInternallyTagged[T] {
  65. iu := &TypeUnionInternallyTagged[T]{
  66. anyTypeUnionInternallyTagged: anyTypeUnionInternallyTagged{
  67. Union: union.ToAny(),
  68. TagField: tagField,
  69. TagToType: make(map[string]reflect.Type),
  70. },
  71. TUnion: union,
  72. }
  73. for _, eleType := range union.ElementTypes {
  74. iu.Add(eleType)
  75. }
  76. unionHandler.internallyTagged[union.UnionType] = &iu.anyTypeUnionInternallyTagged
  77. return iu
  78. }
  79. func (u *TypeUnionInternallyTagged[T]) Add(typ reflect.Type) error {
  80. err := u.Union.Add(typ)
  81. if err != nil {
  82. return nil
  83. }
  84. // 解引用直到得到结构体类型
  85. structType := typ
  86. for structType.Kind() == reflect.Pointer {
  87. structType = structType.Elem()
  88. }
  89. // 要求内嵌Metadata结构体,那么结构体中的字段名就会是Metadata,
  90. field, ok := structType.FieldByName(ref2.TypeNameOf[Metadata]())
  91. if !ok {
  92. u.TagToType[makeDerefFullTypeName(structType)] = typ
  93. return nil
  94. }
  95. // 为防同名,检查类型是不是也是Metadata
  96. if field.Type != ref2.TypeOf[Metadata]() {
  97. u.TagToType[makeDerefFullTypeName(structType)] = typ
  98. return nil
  99. }
  100. tag := field.Tag.Get("union")
  101. if tag == "" {
  102. u.TagToType[makeDerefFullTypeName(structType)] = typ
  103. return nil
  104. }
  105. u.TagToType[tag] = typ
  106. return nil
  107. }
  108. func (u *TypeUnionInternallyTagged[T]) AddT(nilValue T) error {
  109. u.Add(reflect.TypeOf(nilValue))
  110. return nil
  111. }
  112. type UnionHandler struct {
  113. internallyTagged map[reflect.Type]*anyTypeUnionInternallyTagged
  114. externallyTagged map[reflect.Type]*anyTypeUnionExternallyTagged
  115. }
  116. func (h *UnionHandler) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
  117. }
  118. func (h *UnionHandler) CreateMapKeyDecoder(typ reflect2.Type) jsoniter.ValDecoder {
  119. return nil
  120. }
  121. func (h *UnionHandler) CreateMapKeyEncoder(typ reflect2.Type) jsoniter.ValEncoder {
  122. return nil
  123. }
  124. func (h *UnionHandler) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
  125. typ1 := typ.Type1()
  126. if it, ok := h.internallyTagged[typ1]; ok {
  127. return &InternallyTaggedDecoder{
  128. union: it,
  129. }
  130. }
  131. if et, ok := h.externallyTagged[typ1]; ok {
  132. return &ExternallyTaggedDecoder{
  133. union: et,
  134. }
  135. }
  136. return nil
  137. }
  138. func (h *UnionHandler) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
  139. typ1 := typ.Type1()
  140. if it, ok := h.internallyTagged[typ1]; ok {
  141. return &InternallyTaggedEncoder{
  142. union: it,
  143. }
  144. }
  145. if et, ok := h.externallyTagged[typ1]; ok {
  146. return &ExternallyTaggedEncoder{
  147. union: et,
  148. }
  149. }
  150. return nil
  151. }
  152. func (h *UnionHandler) DecorateDecoder(typ reflect2.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
  153. return decoder
  154. }
  155. func (h *UnionHandler) DecorateEncoder(typ reflect2.Type, encoder jsoniter.ValEncoder) jsoniter.ValEncoder {
  156. return encoder
  157. }
  158. // 以下Encoder/Decoder都是在传入类型/目标类型是TypeUnion的基类(UnionType)时使用
  159. type InternallyTaggedEncoder struct {
  160. union *anyTypeUnionInternallyTagged
  161. }
  162. func (e *InternallyTaggedEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  163. return false
  164. }
  165. func (e *InternallyTaggedEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
  166. var val any
  167. if e.union.Union.UnionType.NumMethod() == 0 {
  168. // 无方法的interface底层都是eface结构体,所以可以直接转*any
  169. val = *(*any)(ptr)
  170. } else {
  171. // 有方法的interface底层都是iface结构体,可以将其转成eface,转换后不损失类型信息
  172. val = reflect2.IFaceToEFace(ptr)
  173. }
  174. if val != nil {
  175. if on, ok := val.(sertypes.OnUnionSerializing); ok {
  176. on.OnUnionSerializing()
  177. }
  178. }
  179. // 可以考虑检查一下Type字段有没有赋值,没有赋值则将其赋值为union Tag指定的值
  180. stream.WriteVal(val)
  181. }
  182. type InternallyTaggedDecoder struct {
  183. union *anyTypeUnionInternallyTagged
  184. }
  185. func (e *InternallyTaggedDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  186. nextTokenKind := iter.WhatIsNext()
  187. if nextTokenKind == jsoniter.NilValue {
  188. iter.Skip()
  189. return
  190. }
  191. raw := iter.ReadAny()
  192. if raw.LastError() != nil {
  193. iter.ReportError("decode TaggedUnionType", "getting object raw:"+raw.LastError().Error())
  194. return
  195. }
  196. tagField := raw.Get(e.union.TagField)
  197. if tagField.LastError() != nil {
  198. iter.ReportError("decode TaggedUnionType", "getting type tag field:"+tagField.LastError().Error())
  199. return
  200. }
  201. typeTag := tagField.ToString()
  202. if typeTag == "" {
  203. iter.ReportError("decode TaggedUnionType", "type tag is empty")
  204. return
  205. }
  206. typ, ok := e.union.TagToType[typeTag]
  207. if !ok {
  208. iter.ReportError("decode TaggedUnionType", fmt.Sprintf("unknow type tag %s in union %s", typeTag, e.union.Union.UnionType.Name()))
  209. return
  210. }
  211. // 如果目标类型已经是个指针类型*T,那么在New的时候就需要使用T,
  212. // 否则New出来的是会是**T,这将导致后续的反序列化出问题
  213. if typ.Kind() == reflect.Pointer {
  214. val := reflect.New(typ.Elem())
  215. raw.ToVal(val.Interface()) // TODO 使用的库丢失了ToVal期间的错误信息,考虑换个库
  216. retVal := reflect.NewAt(e.union.Union.UnionType, ptr)
  217. retVal.Elem().Set(val)
  218. } else {
  219. val := reflect.New(typ)
  220. raw.ToVal(val.Interface())
  221. retVal := reflect.NewAt(e.union.Union.UnionType, ptr)
  222. retVal.Elem().Set(val.Elem())
  223. }
  224. }
  225. type ExternallyTaggedEncoder struct {
  226. union *anyTypeUnionExternallyTagged
  227. }
  228. func (e *ExternallyTaggedEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  229. return false
  230. }
  231. func (e *ExternallyTaggedEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
  232. var val any
  233. if e.union.Union.UnionType.NumMethod() == 0 {
  234. // 无方法的interface底层都是eface结构体,所以可以直接转*any
  235. val = *(*any)(ptr)
  236. } else {
  237. // 有方法的interface底层都是iface结构体,可以将其转成eface,转换后不损失类型信息
  238. val = reflect2.IFaceToEFace(ptr)
  239. }
  240. if val == nil {
  241. stream.WriteNil()
  242. return
  243. }
  244. if on, ok := val.(sertypes.OnUnionSerializing); ok {
  245. on.OnUnionSerializing()
  246. }
  247. stream.WriteObjectStart()
  248. valType := ref2.TypeOfValue(val)
  249. if !e.union.Union.Include(valType) {
  250. stream.Error = fmt.Errorf("type %v is not in union %v", valType, e.union.Union.UnionType)
  251. return
  252. }
  253. stream.WriteObjectField(makeDerefFullTypeName(valType))
  254. stream.WriteVal(val)
  255. stream.WriteObjectEnd()
  256. }
  257. type ExternallyTaggedDecoder struct {
  258. union *anyTypeUnionExternallyTagged
  259. }
  260. func (e *ExternallyTaggedDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  261. nextTkType := iter.WhatIsNext()
  262. if nextTkType == jsoniter.NilValue {
  263. iter.Skip()
  264. return
  265. }
  266. if nextTkType != jsoniter.ObjectValue {
  267. iter.ReportError("decode UnionType", fmt.Sprintf("unknow next token type %v", nextTkType))
  268. return
  269. }
  270. typeStr := iter.ReadObject()
  271. if typeStr == "" {
  272. iter.ReportError("decode UnionType", "type string is empty")
  273. }
  274. typ, ok := e.union.TypeNameToType[typeStr]
  275. if !ok {
  276. iter.ReportError("decode UnionType", fmt.Sprintf("unknow type string %s in union %v", typeStr, e.union.Union.UnionType))
  277. return
  278. }
  279. // 如果目标类型已经是个指针类型*T,那么在New的时候就需要使用T,
  280. // 否则New出来的是会是**T,这将导致后续的反序列化出问题
  281. if typ.Kind() == reflect.Pointer {
  282. val := reflect.New(typ.Elem())
  283. iter.ReadVal(val.Interface())
  284. retVal := reflect.NewAt(e.union.Union.UnionType, ptr)
  285. retVal.Elem().Set(val)
  286. } else {
  287. val := reflect.New(typ)
  288. iter.ReadVal(val.Interface())
  289. retVal := reflect.NewAt(e.union.Union.UnionType, ptr)
  290. retVal.Elem().Set(val.Elem())
  291. }
  292. if iter.ReadObject() != "" {
  293. iter.ReportError("decode UnionType", "there should be only one fields in the json object")
  294. }
  295. }
  296. func makeDerefFullTypeName(typ reflect.Type) string {
  297. realType := typ
  298. for realType.Kind() == reflect.Pointer {
  299. realType = realType.Elem()
  300. }
  301. return fmt.Sprintf("%s.%s", realType.PkgPath(), realType.Name())
  302. }