gconv_struct.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). 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. "fmt"
  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 doStruct(params, pointer, mapping...)
  32. }
  33. // StructDeep do Struct function recursively.
  34. // Deprecated, use Struct instead.
  35. func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
  36. return doStruct(params, pointer, mapping...)
  37. }
  38. // doStruct is the core internal converting function for any data to struct.
  39. func doStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
  40. if params == nil {
  41. // If <params> is nil, no conversion.
  42. return nil
  43. }
  44. if pointer == nil {
  45. return gerror.New("object pointer cannot be nil")
  46. }
  47. defer func() {
  48. // Catch the panic, especially the reflect operation panics.
  49. if exception := recover(); exception != nil {
  50. if e, ok := exception.(errorStack); ok {
  51. err = e
  52. } else {
  53. err = gerror.NewSkipf(1, "%v", exception)
  54. }
  55. }
  56. }()
  57. // If given <params> is JSON, it then uses json.Unmarshal doing the converting.
  58. switch r := params.(type) {
  59. case []byte:
  60. if json.Valid(r) {
  61. if rv, ok := pointer.(reflect.Value); ok {
  62. if rv.Kind() == reflect.Ptr {
  63. return json.Unmarshal(r, rv.Interface())
  64. }
  65. } else {
  66. return json.Unmarshal(r, pointer)
  67. }
  68. }
  69. case string:
  70. if paramsBytes := []byte(r); json.Valid(paramsBytes) {
  71. if rv, ok := pointer.(reflect.Value); ok {
  72. if rv.Kind() == reflect.Ptr {
  73. return json.Unmarshal(paramsBytes, rv.Interface())
  74. }
  75. } else {
  76. return json.Unmarshal(paramsBytes, pointer)
  77. }
  78. }
  79. }
  80. var (
  81. paramsReflectValue reflect.Value
  82. pointerReflectValue reflect.Value
  83. pointerReflectKind reflect.Kind
  84. pointerElemReflectValue reflect.Value // The pointed element.
  85. )
  86. if v, ok := params.(reflect.Value); ok {
  87. paramsReflectValue = v
  88. } else {
  89. paramsReflectValue = reflect.ValueOf(params)
  90. }
  91. if v, ok := pointer.(reflect.Value); ok {
  92. pointerReflectValue = v
  93. pointerElemReflectValue = v
  94. } else {
  95. pointerReflectValue = reflect.ValueOf(pointer)
  96. pointerReflectKind = pointerReflectValue.Kind()
  97. if pointerReflectKind != reflect.Ptr {
  98. return gerror.Newf("object pointer should be type of '*struct', but got '%v'", pointerReflectKind)
  99. }
  100. // Using IsNil on reflect.Ptr variable is OK.
  101. if !pointerReflectValue.IsValid() || pointerReflectValue.IsNil() {
  102. return gerror.New("object pointer cannot be nil")
  103. }
  104. pointerElemReflectValue = pointerReflectValue.Elem()
  105. }
  106. // If `params` and `pointer` are the same type, the do directly assignment.
  107. // For performance enhancement purpose.
  108. if pointerElemReflectValue.IsValid() && pointerElemReflectValue.Type() == paramsReflectValue.Type() {
  109. pointerElemReflectValue.Set(paramsReflectValue)
  110. return nil
  111. }
  112. // UnmarshalValue.
  113. // Assign value with interface UnmarshalValue.
  114. // Note that only pointer can implement interface UnmarshalValue.
  115. if v, ok := pointerReflectValue.Interface().(apiUnmarshalValue); ok {
  116. return v.UnmarshalValue(params)
  117. }
  118. // It automatically creates struct object if necessary.
  119. // For example, if <pointer> is **User, then <elem> is *User, which is a pointer to User.
  120. if pointerElemReflectValue.Kind() == reflect.Ptr {
  121. if !pointerElemReflectValue.IsValid() || pointerElemReflectValue.IsNil() {
  122. e := reflect.New(pointerElemReflectValue.Type().Elem()).Elem()
  123. pointerElemReflectValue.Set(e.Addr())
  124. }
  125. if v, ok := pointerElemReflectValue.Interface().(apiUnmarshalValue); ok {
  126. return v.UnmarshalValue(params)
  127. }
  128. // Retrieve its element, may be struct at last.
  129. pointerElemReflectValue = pointerElemReflectValue.Elem()
  130. }
  131. // paramsMap is the map[string]interface{} type variable for params.
  132. // DO NOT use MapDeep here.
  133. paramsMap := Map(params)
  134. if paramsMap == nil {
  135. return gerror.Newf("convert params to map failed: %v", params)
  136. }
  137. // It only performs one converting to the same attribute.
  138. // doneMap is used to check repeated converting, its key is the real attribute name
  139. // of the struct.
  140. doneMap := make(map[string]struct{})
  141. // The key of the attrMap is the attribute name of the struct,
  142. // and the value is its replaced name for later comparison to improve performance.
  143. var (
  144. tempName string
  145. elemFieldType reflect.StructField
  146. elemFieldValue reflect.Value
  147. elemType = pointerElemReflectValue.Type()
  148. attrMap = make(map[string]string)
  149. )
  150. for i := 0; i < pointerElemReflectValue.NumField(); i++ {
  151. elemFieldType = elemType.Field(i)
  152. // Only do converting to public attributes.
  153. if !utils.IsLetterUpper(elemFieldType.Name[0]) {
  154. continue
  155. }
  156. // Maybe it's struct/*struct embedded.
  157. if elemFieldType.Anonymous {
  158. elemFieldValue = pointerElemReflectValue.Field(i)
  159. // Ignore the interface attribute if it's nil.
  160. if elemFieldValue.Kind() == reflect.Interface {
  161. elemFieldValue = elemFieldValue.Elem()
  162. if !elemFieldValue.IsValid() {
  163. continue
  164. }
  165. }
  166. if err = doStruct(paramsMap, elemFieldValue, mapping...); err != nil {
  167. return err
  168. }
  169. } else {
  170. tempName = elemFieldType.Name
  171. attrMap[tempName] = utils.RemoveSymbols(tempName)
  172. }
  173. }
  174. if len(attrMap) == 0 {
  175. return nil
  176. }
  177. // The key of the tagMap is the attribute name of the struct,
  178. // and the value is its replaced tag name for later comparison to improve performance.
  179. tagMap := make(map[string]string)
  180. tagToNameMap, err := structs.TagMapName(pointerElemReflectValue, StructTagPriority)
  181. if err != nil {
  182. return err
  183. }
  184. for k, v := range tagToNameMap {
  185. tagMap[v] = utils.RemoveSymbols(k)
  186. }
  187. var (
  188. attrName string
  189. checkName string
  190. )
  191. for mapK, mapV := range paramsMap {
  192. attrName = ""
  193. // It firstly checks the passed mapping rules.
  194. if len(mapping) > 0 && len(mapping[0]) > 0 {
  195. if passedAttrKey, ok := mapping[0][mapK]; ok {
  196. attrName = passedAttrKey
  197. }
  198. }
  199. // It secondly checks the predefined tags and matching rules.
  200. if attrName == "" {
  201. checkName = utils.RemoveSymbols(mapK)
  202. // Loop to find the matched attribute name with or without
  203. // string cases and chars like '-'/'_'/'.'/' '.
  204. // Matching the parameters to struct tag names.
  205. // The <tagV> is the attribute name of the struct.
  206. for attrKey, cmpKey := range tagMap {
  207. if strings.EqualFold(checkName, cmpKey) {
  208. attrName = attrKey
  209. break
  210. }
  211. }
  212. // Matching the parameters to struct attributes.
  213. if attrName == "" {
  214. for attrKey, cmpKey := range attrMap {
  215. // Eg:
  216. // UserName eq user_name
  217. // User-Name eq username
  218. // username eq userName
  219. // etc.
  220. if strings.EqualFold(checkName, cmpKey) {
  221. attrName = attrKey
  222. break
  223. }
  224. }
  225. }
  226. }
  227. // No matching, it gives up this attribute converting.
  228. if attrName == "" {
  229. continue
  230. }
  231. // If the attribute name is already checked converting, then skip it.
  232. if _, ok := doneMap[attrName]; ok {
  233. continue
  234. }
  235. // Mark it done.
  236. doneMap[attrName] = struct{}{}
  237. if err := bindVarToStructAttr(pointerElemReflectValue, attrName, mapV, mapping...); err != nil {
  238. return err
  239. }
  240. }
  241. return nil
  242. }
  243. // bindVarToStructAttr sets value to struct object attribute by name.
  244. func bindVarToStructAttr(elem reflect.Value, name string, value interface{}, mapping ...map[string]string) (err error) {
  245. structFieldValue := elem.FieldByName(name)
  246. if !structFieldValue.IsValid() {
  247. return nil
  248. }
  249. // CanSet checks whether attribute is public accessible.
  250. if !structFieldValue.CanSet() {
  251. return nil
  252. }
  253. defer func() {
  254. if e := recover(); e != nil {
  255. if err = bindVarToReflectValue(structFieldValue, value, mapping...); err != nil {
  256. err = gerror.Wrapf(err, `error binding value to attribute "%s"`, name)
  257. }
  258. }
  259. }()
  260. // Directly converting.
  261. if empty.IsNil(value) {
  262. structFieldValue.Set(reflect.Zero(structFieldValue.Type()))
  263. } else {
  264. structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String())))
  265. }
  266. return nil
  267. }
  268. // bindVarToReflectValueWithInterfaceCheck does binding using common interfaces checks.
  269. func bindVarToReflectValueWithInterfaceCheck(structFieldValue reflect.Value, value interface{}) (err error, ok bool) {
  270. if structFieldValue.CanAddr() {
  271. pointer := structFieldValue.Addr().Interface()
  272. if v, ok := pointer.(apiUnmarshalValue); ok {
  273. return v.UnmarshalValue(value), ok
  274. }
  275. if v, ok := pointer.(apiUnmarshalText); ok {
  276. if s, ok := value.(string); ok {
  277. return v.UnmarshalText([]byte(s)), ok
  278. }
  279. if b, ok := value.([]byte); ok {
  280. return v.UnmarshalText(b), ok
  281. }
  282. }
  283. if v, ok := pointer.(apiSet); ok {
  284. v.Set(value)
  285. return nil, ok
  286. }
  287. }
  288. return nil, false
  289. }
  290. // bindVarToReflectValue sets <value> to reflect value object <structFieldValue>.
  291. func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, mapping ...map[string]string) (err error) {
  292. if err, ok := bindVarToReflectValueWithInterfaceCheck(structFieldValue, value); ok {
  293. return err
  294. }
  295. kind := structFieldValue.Kind()
  296. // Converting using interface, for some kinds.
  297. switch kind {
  298. case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Interface:
  299. if !structFieldValue.IsNil() {
  300. if v, ok := structFieldValue.Interface().(apiSet); ok {
  301. v.Set(value)
  302. return nil
  303. }
  304. }
  305. }
  306. // Converting by kind.
  307. switch kind {
  308. case reflect.Struct:
  309. // Recursively converting for struct attribute.
  310. if err := doStruct(value, structFieldValue); err != nil {
  311. // Note there's reflect conversion mechanism here.
  312. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  313. }
  314. // Note that the slice element might be type of struct,
  315. // so it uses Struct function doing the converting internally.
  316. case reflect.Slice, reflect.Array:
  317. a := reflect.Value{}
  318. v := reflect.ValueOf(value)
  319. if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
  320. a = reflect.MakeSlice(structFieldValue.Type(), v.Len(), v.Len())
  321. if v.Len() > 0 {
  322. t := a.Index(0).Type()
  323. for i := 0; i < v.Len(); i++ {
  324. if t.Kind() == reflect.Ptr {
  325. e := reflect.New(t.Elem()).Elem()
  326. if err := doStruct(v.Index(i).Interface(), e); err != nil {
  327. // Note there's reflect conversion mechanism here.
  328. e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
  329. }
  330. a.Index(i).Set(e.Addr())
  331. } else {
  332. e := reflect.New(t).Elem()
  333. if err := doStruct(v.Index(i).Interface(), e); err != nil {
  334. // Note there's reflect conversion mechanism here.
  335. e.Set(reflect.ValueOf(v.Index(i).Interface()).Convert(t))
  336. }
  337. a.Index(i).Set(e)
  338. }
  339. }
  340. }
  341. } else {
  342. a = reflect.MakeSlice(structFieldValue.Type(), 1, 1)
  343. t := a.Index(0).Type()
  344. if t.Kind() == reflect.Ptr {
  345. e := reflect.New(t.Elem()).Elem()
  346. if err := doStruct(value, e); err != nil {
  347. // Note there's reflect conversion mechanism here.
  348. e.Set(reflect.ValueOf(value).Convert(t))
  349. }
  350. a.Index(0).Set(e.Addr())
  351. } else {
  352. e := reflect.New(t).Elem()
  353. if err := doStruct(value, e); err != nil {
  354. // Note there's reflect conversion mechanism here.
  355. e.Set(reflect.ValueOf(value).Convert(t))
  356. }
  357. a.Index(0).Set(e)
  358. }
  359. }
  360. structFieldValue.Set(a)
  361. case reflect.Ptr:
  362. item := reflect.New(structFieldValue.Type().Elem())
  363. if err, ok := bindVarToReflectValueWithInterfaceCheck(item, value); ok {
  364. structFieldValue.Set(item)
  365. return err
  366. }
  367. elem := item.Elem()
  368. if err = bindVarToReflectValue(elem, value, mapping...); err == nil {
  369. structFieldValue.Set(elem.Addr())
  370. }
  371. // It mainly and specially handles the interface of nil value.
  372. case reflect.Interface:
  373. if value == nil {
  374. // Specially.
  375. structFieldValue.Set(reflect.ValueOf((*interface{})(nil)))
  376. } else {
  377. // Note there's reflect conversion mechanism here.
  378. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  379. }
  380. default:
  381. defer func() {
  382. if e := recover(); e != nil {
  383. err = gerror.New(
  384. fmt.Sprintf(`cannot convert value "%+v" to type "%s"`,
  385. value,
  386. structFieldValue.Type().String(),
  387. ),
  388. )
  389. }
  390. }()
  391. // It here uses reflect converting <value> to type of the attribute and assigns
  392. // the result value to the attribute. It might fail and panic if the usual Go
  393. // conversion rules do not allow conversion.
  394. structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
  395. }
  396. return nil
  397. }