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

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