gconv_struct.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gconv
  7. import (
  8. "github.com/gogf/gf/errors/gcode"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/internal/empty"
  11. "github.com/gogf/gf/internal/json"
  12. "github.com/gogf/gf/internal/structs"
  13. "reflect"
  14. "strings"
  15. "github.com/gogf/gf/internal/utils"
  16. )
  17. // Struct maps the params key-value pairs to the corresponding struct object's attributes.
  18. // The third parameter `mapping` is unnecessary, indicating the mapping rules between the
  19. // custom key name and the attribute name(case sensitive).
  20. //
  21. // Note:
  22. // 1. The `params` can be any type of map/struct, usually a map.
  23. // 2. The `pointer` should be type of *struct/**struct, which is a pointer to struct object
  24. // or struct pointer.
  25. // 3. Only the public attributes of struct object can be mapped.
  26. // 4. If `params` is a map, the key of the map `params` can be lowercase.
  27. // It will automatically convert the first letter of the key to uppercase
  28. // in mapping procedure to do the matching.
  29. // It ignores the map key, if it does not match.
  30. func Struct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  31. return Scan(params, pointer, mapping...)
  32. }
  33. // StructTag acts as Struct but also with support for priority tag feature, which retrieves the
  34. // specified tags for `params` key-value items to struct attribute names mapping.
  35. // The parameter `priorityTag` supports multiple tags that can be joined with char ','.
  36. func StructTag(params interface{}, pointer interface{}, priorityTag string) (err error) {
  37. return doStruct(params, pointer, nil, priorityTag)
  38. }
  39. // StructDeep do Struct function recursively.
  40. // Deprecated, use Struct instead.
  41. func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
  42. var keyToAttributeNameMapping map[string]string
  43. if len(mapping) > 0 {
  44. keyToAttributeNameMapping = mapping[0]
  45. }
  46. return doStruct(params, pointer, keyToAttributeNameMapping, "")
  47. }
  48. // doStruct is the core internal converting function for any data to struct.
  49. func doStruct(params interface{}, pointer interface{}, mapping map[string]string, priorityTag string) (err error) {
  50. if params == nil {
  51. // If `params` is nil, no conversion.
  52. return nil
  53. }
  54. if pointer == nil {
  55. return gerror.NewCode(gcode.CodeInvalidParameter, "object pointer cannot be nil")
  56. }
  57. defer func() {
  58. // Catch the panic, especially the reflect operation panics.
  59. if exception := recover(); exception != nil {
  60. if e, ok := exception.(errorStack); ok {
  61. err = e
  62. } else {
  63. err = gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%v", exception)
  64. }
  65. }
  66. }()
  67. // If given `params` is JSON, it then uses json.Unmarshal doing the converting.
  68. switch r := params.(type) {
  69. case []byte:
  70. if json.Valid(r) {
  71. if rv, ok := pointer.(reflect.Value); ok {
  72. if rv.Kind() == reflect.Ptr {
  73. return json.UnmarshalUseNumber(r, rv.Interface())
  74. } else if rv.CanAddr() {
  75. return json.UnmarshalUseNumber(r, rv.Addr().Interface())
  76. }
  77. } else {
  78. return json.UnmarshalUseNumber(r, pointer)
  79. }
  80. }
  81. case string:
  82. if paramsBytes := []byte(r); json.Valid(paramsBytes) {
  83. if rv, ok := pointer.(reflect.Value); ok {
  84. if rv.Kind() == reflect.Ptr {
  85. return json.UnmarshalUseNumber(paramsBytes, rv.Interface())
  86. } else if rv.CanAddr() {
  87. return json.UnmarshalUseNumber(paramsBytes, rv.Addr().Interface())
  88. }
  89. } else {
  90. return json.UnmarshalUseNumber(paramsBytes, pointer)
  91. }
  92. }
  93. }
  94. var (
  95. paramsReflectValue reflect.Value
  96. paramsInterface interface{} // DO NOT use `params` directly as it might be type of `reflect.Value`
  97. pointerReflectValue reflect.Value
  98. pointerReflectKind reflect.Kind
  99. pointerElemReflectValue reflect.Value // The pointed element.
  100. )
  101. if v, ok := params.(reflect.Value); ok {
  102. paramsReflectValue = v
  103. } else {
  104. paramsReflectValue = reflect.ValueOf(params)
  105. }
  106. paramsInterface = paramsReflectValue.Interface()
  107. if v, ok := pointer.(reflect.Value); ok {
  108. pointerReflectValue = v
  109. pointerElemReflectValue = v
  110. } else {
  111. pointerReflectValue = reflect.ValueOf(pointer)
  112. pointerReflectKind = pointerReflectValue.Kind()
  113. if pointerReflectKind != reflect.Ptr {
  114. return gerror.NewCodef(gcode.CodeInvalidParameter, "object pointer should be type of '*struct', but got '%v'", pointerReflectKind)
  115. }
  116. // Using IsNil on reflect.Ptr variable is OK.
  117. if !pointerReflectValue.IsValid() || pointerReflectValue.IsNil() {
  118. return gerror.NewCode(gcode.CodeInvalidParameter, "object pointer cannot be nil")
  119. }
  120. pointerElemReflectValue = pointerReflectValue.Elem()
  121. }
  122. // If `params` and `pointer` are the same type, the do directly assignment.
  123. // For performance enhancement purpose.
  124. if pointerElemReflectValue.IsValid() && pointerElemReflectValue.Type() == paramsReflectValue.Type() {
  125. pointerElemReflectValue.Set(paramsReflectValue)
  126. return nil
  127. }
  128. // Normal unmarshalling interfaces checks.
  129. if err, ok := bindVarToReflectValueWithInterfaceCheck(pointerReflectValue, paramsInterface); ok {
  130. return err
  131. }
  132. // It automatically creates struct object if necessary.
  133. // For example, if `pointer` is **User, then `elem` is *User, which is a pointer to User.
  134. if pointerElemReflectValue.Kind() == reflect.Ptr {
  135. if !pointerElemReflectValue.IsValid() || pointerElemReflectValue.IsNil() {
  136. e := reflect.New(pointerElemReflectValue.Type().Elem()).Elem()
  137. pointerElemReflectValue.Set(e.Addr())
  138. }
  139. //if v, ok := pointerElemReflectValue.Interface().(apiUnmarshalValue); ok {
  140. // return v.UnmarshalValue(params)
  141. //}
  142. // Note that it's `pointerElemReflectValue` here not `pointerReflectValue`.
  143. if err, ok := bindVarToReflectValueWithInterfaceCheck(pointerElemReflectValue, paramsInterface); ok {
  144. return err
  145. }
  146. // Retrieve its element, may be struct at last.
  147. pointerElemReflectValue = pointerElemReflectValue.Elem()
  148. }
  149. // paramsMap is the map[string]interface{} type variable for params.
  150. // DO NOT use MapDeep here.
  151. paramsMap := Map(paramsInterface)
  152. if paramsMap == nil {
  153. return gerror.NewCodef(gcode.CodeInvalidParameter, "convert params to map failed: %v", params)
  154. }
  155. // It only performs one converting to the same attribute.
  156. // doneMap is used to check repeated converting, its key is the real attribute name
  157. // of the struct.
  158. doneMap := make(map[string]struct{})
  159. // The key of the attrMap is the attribute name of the struct,
  160. // and the value is its replaced name for later comparison to improve performance.
  161. var (
  162. tempName string
  163. elemFieldType reflect.StructField
  164. elemFieldValue reflect.Value
  165. elemType = pointerElemReflectValue.Type()
  166. attrMap = make(map[string]string)
  167. )
  168. for i := 0; i < pointerElemReflectValue.NumField(); i++ {
  169. elemFieldType = elemType.Field(i)
  170. // Only do converting to public attributes.
  171. if !utils.IsLetterUpper(elemFieldType.Name[0]) {
  172. continue
  173. }
  174. // Maybe it's struct/*struct embedded.
  175. if elemFieldType.Anonymous {
  176. elemFieldValue = pointerElemReflectValue.Field(i)
  177. // Ignore the interface attribute if it's nil.
  178. if elemFieldValue.Kind() == reflect.Interface {
  179. elemFieldValue = elemFieldValue.Elem()
  180. if !elemFieldValue.IsValid() {
  181. continue
  182. }
  183. }
  184. if err = doStruct(paramsMap, elemFieldValue, mapping, priorityTag); err != nil {
  185. return err
  186. }
  187. } else {
  188. tempName = elemFieldType.Name
  189. attrMap[tempName] = utils.RemoveSymbols(tempName)
  190. }
  191. }
  192. if len(attrMap) == 0 {
  193. return nil
  194. }
  195. // The key of the tagMap is the attribute name of the struct,
  196. // and the value is its replaced tag name for later comparison to improve performance.
  197. var (
  198. tagMap = make(map[string]string)
  199. priorityTagArray []string
  200. )
  201. if priorityTag != "" {
  202. priorityTagArray = append(utils.SplitAndTrim(priorityTag, ","), StructTagPriority...)
  203. } else {
  204. priorityTagArray = StructTagPriority
  205. }
  206. tagToNameMap, err := structs.TagMapName(pointerElemReflectValue, priorityTagArray)
  207. if err != nil {
  208. return err
  209. }
  210. for tagName, attributeName := range tagToNameMap {
  211. // If there's something else in the tag string,
  212. // it uses the first part which is split using char ','.
  213. // Eg:
  214. // orm:"id, priority"
  215. // orm:"name, with:uid=id"
  216. tagMap[attributeName] = utils.RemoveSymbols(strings.Split(tagName, ",")[0])
  217. }
  218. var (
  219. attrName string
  220. checkName string
  221. )
  222. for mapK, mapV := range paramsMap {
  223. attrName = ""
  224. // It firstly checks the passed mapping rules.
  225. if len(mapping) > 0 {
  226. if passedAttrKey, ok := mapping[mapK]; ok {
  227. attrName = passedAttrKey
  228. }
  229. }
  230. // It secondly checks the predefined tags and matching rules.
  231. if attrName == "" {
  232. checkName = utils.RemoveSymbols(mapK)
  233. // Loop to find the matched attribute name with or without
  234. // string cases and chars like '-'/'_'/'.'/' '.
  235. // Matching the parameters to struct tag names.
  236. // The `tagV` is the attribute name of the struct.
  237. for attrKey, cmpKey := range tagMap {
  238. if strings.EqualFold(checkName, cmpKey) {
  239. attrName = attrKey
  240. break
  241. }
  242. }
  243. // Matching the parameters to struct attributes.
  244. if attrName == "" {
  245. for attrKey, cmpKey := range attrMap {
  246. // Eg:
  247. // UserName eq user_name
  248. // User-Name eq username
  249. // username eq userName
  250. // etc.
  251. if strings.EqualFold(checkName, cmpKey) {
  252. attrName = attrKey
  253. break
  254. }
  255. }
  256. }
  257. }
  258. // No matching, it gives up this attribute converting.
  259. if attrName == "" {
  260. continue
  261. }
  262. // If the attribute name is already checked converting, then skip it.
  263. if _, ok := doneMap[attrName]; ok {
  264. continue
  265. }
  266. // Mark it done.
  267. doneMap[attrName] = struct{}{}
  268. if err := bindVarToStructAttr(pointerElemReflectValue, attrName, mapV, mapping, priorityTag); err != nil {
  269. return err
  270. }
  271. }
  272. return nil
  273. }
  274. // bindVarToStructAttr sets value to struct object attribute by name.
  275. func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, mapping map[string]string, priorityTag string) (err error) {
  276. structFieldValue := elem.FieldByName(name)
  277. if !structFieldValue.IsValid() {
  278. return nil
  279. }
  280. // CanSet checks whether attribute is public accessible.
  281. if !structFieldValue.CanSet() {
  282. return nil
  283. }
  284. defer func() {
  285. if exception := recover(); exception != nil {
  286. if err = bindVarToReflectValue(structFieldValue, value, mapping, priorityTag); err != nil {
  287. err = gerror.WrapCodef(gcode.CodeInternalError, err, `error binding value to attribute "%s"`, name)
  288. }
  289. }
  290. }()
  291. // Directly converting.
  292. if empty.IsNil(value) {
  293. structFieldValue.Set(reflect.Zero(structFieldValue.Type()))
  294. } else {
  295. structFieldValue.Set(reflect.ValueOf(doConvert(
  296. doConvertInput{
  297. FromValue: value,
  298. ToTypeName: structFieldValue.Type().String(),
  299. ReferValue: structFieldValue,
  300. },
  301. )))
  302. }
  303. return nil
  304. }
  305. // bindVarToReflectValueWithInterfaceCheck does binding using common interfaces checks.
  306. func bindVarToReflectValueWithInterfaceCheck(reflectValue reflect.Value, value interface{}) (err error, ok bool) {
  307. var pointer interface{}
  308. if reflectValue.Kind() != reflect.Ptr && reflectValue.CanAddr() {
  309. reflectValueAddr := reflectValue.Addr()
  310. if reflectValueAddr.IsNil() || !reflectValueAddr.IsValid() {
  311. return nil, false
  312. }
  313. // Not a pointer, but can token address, that makes it can be unmarshalled.
  314. pointer = reflectValue.Addr().Interface()
  315. } else {
  316. if reflectValue.IsNil() || !reflectValue.IsValid() {
  317. return nil, false
  318. }
  319. pointer = reflectValue.Interface()
  320. }
  321. // UnmarshalValue.
  322. if v, ok := pointer.(apiUnmarshalValue); ok {
  323. return v.UnmarshalValue(value), ok
  324. }
  325. // UnmarshalText.
  326. if v, ok := pointer.(apiUnmarshalText); ok {
  327. var valueBytes []byte
  328. if b, ok := value.([]byte); ok {
  329. valueBytes = b
  330. } else if s, ok := value.(string); ok {
  331. valueBytes = []byte(s)
  332. }
  333. if len(valueBytes) > 0 {
  334. return v.UnmarshalText(valueBytes), ok
  335. }
  336. }
  337. // UnmarshalJSON.
  338. if v, ok := pointer.(apiUnmarshalJSON); ok {
  339. var valueBytes []byte
  340. if b, ok := value.([]byte); ok {
  341. valueBytes = b
  342. } else if s, ok := value.(string); ok {
  343. valueBytes = []byte(s)
  344. }
  345. if len(valueBytes) > 0 {
  346. // If it is not a valid JSON string, it then adds char `"` on its both sides to make it is.
  347. if !json.Valid(valueBytes) {
  348. newValueBytes := make([]byte, len(valueBytes)+2)
  349. newValueBytes[0] = '"'
  350. newValueBytes[len(newValueBytes)-1] = '"'
  351. copy(newValueBytes[1:], valueBytes)
  352. valueBytes = newValueBytes
  353. }
  354. return v.UnmarshalJSON(valueBytes), ok
  355. }
  356. }
  357. if v, ok := pointer.(apiSet); ok {
  358. v.Set(value)
  359. return nil, ok
  360. }
  361. return nil, false
  362. }
  363. // bindVarToReflectValue sets `value` to reflect value object `structFieldValue`.
  364. func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, mapping map[string]string, priorityTag string) (err error) {
  365. if err, ok := bindVarToReflectValueWithInterfaceCheck(structFieldValue, value); ok {
  366. return err
  367. }
  368. kind := structFieldValue.Kind()
  369. // Converting using interface, for some kinds.
  370. switch kind {
  371. case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Interface:
  372. if !structFieldValue.IsNil() {
  373. if v, ok := structFieldValue.Interface().(apiSet); ok {
  374. v.Set(value)
  375. return nil
  376. }
  377. }
  378. }
  379. // Converting by kind.
  380. switch kind {
  381. case reflect.Map:
  382. return doMapToMap(value, structFieldValue, mapping)
  383. case reflect.Struct:
  384. // Recursively converting for struct attribute.
  385. if err := doStruct(value, structFieldValue, nil, ""); err != nil {
  386. // Note there's reflect conversion mechanism here.
  387. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  388. }
  389. // Note that the slice element might be type of struct,
  390. // so it uses Struct function doing the converting internally.
  391. case reflect.Slice, reflect.Array:
  392. a := reflect.Value{}
  393. v := reflect.ValueOf(value)
  394. if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
  395. a = reflect.MakeSlice(structFieldValue.Type(), v.Len(), v.Len())
  396. if v.Len() > 0 {
  397. t := a.Index(0).Type()
  398. for i := 0; i < v.Len(); i++ {
  399. if t.Kind() == reflect.Ptr {
  400. e := reflect.New(t.Elem()).Elem()
  401. if err := doStruct(v.Index(i).Interface(), e, nil, ""); err != nil {
  402. // Note there's reflect conversion mechanism here.
  403. e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
  404. }
  405. a.Index(i).Set(e.Addr())
  406. } else {
  407. e := reflect.New(t).Elem()
  408. if err := doStruct(v.Index(i).Interface(), e, nil, ""); err != nil {
  409. // Note there's reflect conversion mechanism here.
  410. e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
  411. }
  412. a.Index(i).Set(e)
  413. }
  414. }
  415. }
  416. } else {
  417. a = reflect.MakeSlice(structFieldValue.Type(), 1, 1)
  418. t := a.Index(0).Type()
  419. if t.Kind() == reflect.Ptr {
  420. e := reflect.New(t.Elem()).Elem()
  421. if err := doStruct(value, e, nil, ""); err != nil {
  422. // Note there's reflect conversion mechanism here.
  423. e.Set(reflect.ValueOf(value).Convert(t))
  424. }
  425. a.Index(0).Set(e.Addr())
  426. } else {
  427. e := reflect.New(t).Elem()
  428. if err := doStruct(value, e, nil, ""); err != nil {
  429. // Note there's reflect conversion mechanism here.
  430. e.Set(reflect.ValueOf(value).Convert(t))
  431. }
  432. a.Index(0).Set(e)
  433. }
  434. }
  435. structFieldValue.Set(a)
  436. case reflect.Ptr:
  437. item := reflect.New(structFieldValue.Type().Elem())
  438. if err, ok := bindVarToReflectValueWithInterfaceCheck(item, value); ok {
  439. structFieldValue.Set(item)
  440. return err
  441. }
  442. elem := item.Elem()
  443. if err = bindVarToReflectValue(elem, value, mapping, priorityTag); err == nil {
  444. structFieldValue.Set(elem.Addr())
  445. }
  446. // It mainly and specially handles the interface of nil value.
  447. case reflect.Interface:
  448. if value == nil {
  449. // Specially.
  450. structFieldValue.Set(reflect.ValueOf((*interface{})(nil)))
  451. } else {
  452. // Note there's reflect conversion mechanism here.
  453. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  454. }
  455. default:
  456. defer func() {
  457. if exception := recover(); exception != nil {
  458. err = gerror.NewCodef(
  459. gcode.CodeInternalError,
  460. `cannot convert value "%+v" to type "%s":%+v`,
  461. value,
  462. structFieldValue.Type().String(),
  463. exception,
  464. )
  465. }
  466. }()
  467. // It here uses reflect converting `value` to type of the attribute and assigns
  468. // the result value to the attribute. It might fail and panic if the usual Go
  469. // conversion rules do not allow conversion.
  470. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  471. }
  472. return nil
  473. }