model_struct.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package gorm
  2. import (
  3. "database/sql"
  4. "errors"
  5. "go/ast"
  6. "reflect"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/jinzhu/inflection"
  11. )
  12. // DefaultTableNameHandler default table name handler
  13. var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
  14. return defaultTableName
  15. }
  16. type safeModelStructsMap struct {
  17. m map[reflect.Type]*ModelStruct
  18. l *sync.RWMutex
  19. }
  20. func (s *safeModelStructsMap) Set(key reflect.Type, value *ModelStruct) {
  21. s.l.Lock()
  22. defer s.l.Unlock()
  23. s.m[key] = value
  24. }
  25. func (s *safeModelStructsMap) Get(key reflect.Type) *ModelStruct {
  26. s.l.RLock()
  27. defer s.l.RUnlock()
  28. return s.m[key]
  29. }
  30. func newModelStructsMap() *safeModelStructsMap {
  31. return &safeModelStructsMap{l: new(sync.RWMutex), m: make(map[reflect.Type]*ModelStruct)}
  32. }
  33. var modelStructsMap = newModelStructsMap()
  34. // ModelStruct model definition
  35. type ModelStruct struct {
  36. PrimaryFields []*StructField
  37. StructFields []*StructField
  38. ModelType reflect.Type
  39. defaultTableName string
  40. }
  41. // TableName get model's table name
  42. func (s *ModelStruct) TableName(db *DB) string {
  43. if s.defaultTableName == "" && db != nil && s.ModelType != nil {
  44. // Set default table name
  45. if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
  46. s.defaultTableName = tabler.TableName()
  47. } else {
  48. tableName := ToDBName(s.ModelType.Name())
  49. if db == nil || !db.parent.singularTable {
  50. tableName = inflection.Plural(tableName)
  51. }
  52. s.defaultTableName = tableName
  53. }
  54. }
  55. return DefaultTableNameHandler(db, s.defaultTableName)
  56. }
  57. // StructField model field's struct definition
  58. type StructField struct {
  59. DBName string
  60. Name string
  61. Names []string
  62. IsPrimaryKey bool
  63. IsNormal bool
  64. IsIgnored bool
  65. IsScanner bool
  66. HasDefaultValue bool
  67. Tag reflect.StructTag
  68. TagSettings map[string]string
  69. Struct reflect.StructField
  70. IsForeignKey bool
  71. Relationship *Relationship
  72. }
  73. func (structField *StructField) clone() *StructField {
  74. clone := &StructField{
  75. DBName: structField.DBName,
  76. Name: structField.Name,
  77. Names: structField.Names,
  78. IsPrimaryKey: structField.IsPrimaryKey,
  79. IsNormal: structField.IsNormal,
  80. IsIgnored: structField.IsIgnored,
  81. IsScanner: structField.IsScanner,
  82. HasDefaultValue: structField.HasDefaultValue,
  83. Tag: structField.Tag,
  84. TagSettings: map[string]string{},
  85. Struct: structField.Struct,
  86. IsForeignKey: structField.IsForeignKey,
  87. }
  88. if structField.Relationship != nil {
  89. relationship := *structField.Relationship
  90. clone.Relationship = &relationship
  91. }
  92. for key, value := range structField.TagSettings {
  93. clone.TagSettings[key] = value
  94. }
  95. return clone
  96. }
  97. // Relationship described the relationship between models
  98. type Relationship struct {
  99. Kind string
  100. PolymorphicType string
  101. PolymorphicDBName string
  102. PolymorphicValue string
  103. ForeignFieldNames []string
  104. ForeignDBNames []string
  105. AssociationForeignFieldNames []string
  106. AssociationForeignDBNames []string
  107. JoinTableHandler JoinTableHandlerInterface
  108. }
  109. func getForeignField(column string, fields []*StructField) *StructField {
  110. for _, field := range fields {
  111. if field.Name == column || field.DBName == column || field.DBName == ToDBName(column) {
  112. return field
  113. }
  114. }
  115. return nil
  116. }
  117. // GetModelStruct get value's model struct, relationships based on struct and tag definition
  118. func (scope *Scope) GetModelStruct() *ModelStruct {
  119. var modelStruct ModelStruct
  120. // Scope value can't be nil
  121. if scope.Value == nil {
  122. return &modelStruct
  123. }
  124. reflectType := reflect.ValueOf(scope.Value).Type()
  125. for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
  126. reflectType = reflectType.Elem()
  127. }
  128. // Scope value need to be a struct
  129. if reflectType.Kind() != reflect.Struct {
  130. return &modelStruct
  131. }
  132. // Get Cached model struct
  133. if value := modelStructsMap.Get(reflectType); value != nil {
  134. return value
  135. }
  136. modelStruct.ModelType = reflectType
  137. // Get all fields
  138. for i := 0; i < reflectType.NumField(); i++ {
  139. if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
  140. field := &StructField{
  141. Struct: fieldStruct,
  142. Name: fieldStruct.Name,
  143. Names: []string{fieldStruct.Name},
  144. Tag: fieldStruct.Tag,
  145. TagSettings: parseTagSetting(fieldStruct.Tag),
  146. }
  147. // is ignored field
  148. if _, ok := field.TagSettings["-"]; ok {
  149. field.IsIgnored = true
  150. } else {
  151. if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
  152. field.IsPrimaryKey = true
  153. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
  154. }
  155. if _, ok := field.TagSettings["DEFAULT"]; ok {
  156. field.HasDefaultValue = true
  157. }
  158. if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsPrimaryKey {
  159. field.HasDefaultValue = true
  160. }
  161. indirectType := fieldStruct.Type
  162. for indirectType.Kind() == reflect.Ptr {
  163. indirectType = indirectType.Elem()
  164. }
  165. fieldValue := reflect.New(indirectType).Interface()
  166. if _, isScanner := fieldValue.(sql.Scanner); isScanner {
  167. // is scanner
  168. field.IsScanner, field.IsNormal = true, true
  169. if indirectType.Kind() == reflect.Struct {
  170. for i := 0; i < indirectType.NumField(); i++ {
  171. for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
  172. if _, ok := field.TagSettings[key]; !ok {
  173. field.TagSettings[key] = value
  174. }
  175. }
  176. }
  177. }
  178. } else if _, isTime := fieldValue.(*time.Time); isTime {
  179. // is time
  180. field.IsNormal = true
  181. } else if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
  182. // is embedded struct
  183. for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
  184. subField = subField.clone()
  185. subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
  186. if prefix, ok := field.TagSettings["EMBEDDED_PREFIX"]; ok {
  187. subField.DBName = prefix + subField.DBName
  188. }
  189. if subField.IsPrimaryKey {
  190. if _, ok := subField.TagSettings["PRIMARY_KEY"]; ok {
  191. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
  192. } else {
  193. subField.IsPrimaryKey = false
  194. }
  195. }
  196. if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
  197. if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
  198. newJoinTableHandler := &JoinTableHandler{}
  199. newJoinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
  200. subField.Relationship.JoinTableHandler = newJoinTableHandler
  201. }
  202. }
  203. modelStruct.StructFields = append(modelStruct.StructFields, subField)
  204. }
  205. continue
  206. } else {
  207. // build relationships
  208. switch indirectType.Kind() {
  209. case reflect.Slice:
  210. defer func(field *StructField) {
  211. var (
  212. relationship = &Relationship{}
  213. toScope = scope.New(reflect.New(field.Struct.Type).Interface())
  214. foreignKeys []string
  215. associationForeignKeys []string
  216. elemType = field.Struct.Type
  217. )
  218. if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
  219. foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
  220. }
  221. if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
  222. associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
  223. }
  224. for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
  225. elemType = elemType.Elem()
  226. }
  227. if elemType.Kind() == reflect.Struct {
  228. if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
  229. relationship.Kind = "many_to_many"
  230. // if no foreign keys defined with tag
  231. if len(foreignKeys) == 0 {
  232. for _, field := range modelStruct.PrimaryFields {
  233. foreignKeys = append(foreignKeys, field.DBName)
  234. }
  235. }
  236. for _, foreignKey := range foreignKeys {
  237. if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
  238. // source foreign keys (db names)
  239. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)
  240. // join table foreign keys for source
  241. joinTableDBName := ToDBName(reflectType.Name()) + "_" + foreignField.DBName
  242. relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
  243. }
  244. }
  245. // if no association foreign keys defined with tag
  246. if len(associationForeignKeys) == 0 {
  247. for _, field := range toScope.PrimaryFields() {
  248. associationForeignKeys = append(associationForeignKeys, field.DBName)
  249. }
  250. }
  251. for _, name := range associationForeignKeys {
  252. if field, ok := toScope.FieldByName(name); ok {
  253. // association foreign keys (db names)
  254. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
  255. // join table foreign keys for association
  256. joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
  257. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
  258. }
  259. }
  260. joinTableHandler := JoinTableHandler{}
  261. joinTableHandler.Setup(relationship, many2many, reflectType, elemType)
  262. relationship.JoinTableHandler = &joinTableHandler
  263. field.Relationship = relationship
  264. } else {
  265. // User has many comments, associationType is User, comment use UserID as foreign key
  266. var associationType = reflectType.Name()
  267. var toFields = toScope.GetStructFields()
  268. relationship.Kind = "has_many"
  269. if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
  270. // Dog has many toys, tag polymorphic is Owner, then associationType is Owner
  271. // Toy use OwnerID, OwnerType ('dogs') as foreign key
  272. if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
  273. associationType = polymorphic
  274. relationship.PolymorphicType = polymorphicType.Name
  275. relationship.PolymorphicDBName = polymorphicType.DBName
  276. // if Dog has multiple set of toys set name of the set (instead of default 'dogs')
  277. if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
  278. relationship.PolymorphicValue = value
  279. } else {
  280. relationship.PolymorphicValue = scope.TableName()
  281. }
  282. polymorphicType.IsForeignKey = true
  283. }
  284. }
  285. // if no foreign keys defined with tag
  286. if len(foreignKeys) == 0 {
  287. // if no association foreign keys defined with tag
  288. if len(associationForeignKeys) == 0 {
  289. for _, field := range modelStruct.PrimaryFields {
  290. foreignKeys = append(foreignKeys, associationType+field.Name)
  291. associationForeignKeys = append(associationForeignKeys, field.Name)
  292. }
  293. } else {
  294. // generate foreign keys from defined association foreign keys
  295. for _, scopeFieldName := range associationForeignKeys {
  296. if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil {
  297. foreignKeys = append(foreignKeys, associationType+foreignField.Name)
  298. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  299. }
  300. }
  301. }
  302. } else {
  303. // generate association foreign keys from foreign keys
  304. if len(associationForeignKeys) == 0 {
  305. for _, foreignKey := range foreignKeys {
  306. if strings.HasPrefix(foreignKey, associationType) {
  307. associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
  308. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  309. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  310. }
  311. }
  312. }
  313. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  314. associationForeignKeys = []string{scope.PrimaryKey()}
  315. }
  316. } else if len(foreignKeys) != len(associationForeignKeys) {
  317. scope.Err(errors.New("invalid foreign keys, should have same length"))
  318. return
  319. }
  320. }
  321. for idx, foreignKey := range foreignKeys {
  322. if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
  323. if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
  324. // source foreign keys
  325. foreignField.IsForeignKey = true
  326. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
  327. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
  328. // association foreign keys
  329. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  330. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  331. }
  332. }
  333. }
  334. if len(relationship.ForeignFieldNames) != 0 {
  335. field.Relationship = relationship
  336. }
  337. }
  338. } else {
  339. field.IsNormal = true
  340. }
  341. }(field)
  342. case reflect.Struct:
  343. defer func(field *StructField) {
  344. var (
  345. // user has one profile, associationType is User, profile use UserID as foreign key
  346. // user belongs to profile, associationType is Profile, user use ProfileID as foreign key
  347. associationType = reflectType.Name()
  348. relationship = &Relationship{}
  349. toScope = scope.New(reflect.New(field.Struct.Type).Interface())
  350. toFields = toScope.GetStructFields()
  351. tagForeignKeys []string
  352. tagAssociationForeignKeys []string
  353. )
  354. if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
  355. tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ",")
  356. }
  357. if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
  358. tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ",")
  359. }
  360. if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
  361. // Cat has one toy, tag polymorphic is Owner, then associationType is Owner
  362. // Toy use OwnerID, OwnerType ('cats') as foreign key
  363. if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
  364. associationType = polymorphic
  365. relationship.PolymorphicType = polymorphicType.Name
  366. relationship.PolymorphicDBName = polymorphicType.DBName
  367. // if Cat has several different types of toys set name for each (instead of default 'cats')
  368. if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
  369. relationship.PolymorphicValue = value
  370. } else {
  371. relationship.PolymorphicValue = scope.TableName()
  372. }
  373. polymorphicType.IsForeignKey = true
  374. }
  375. }
  376. // Has One
  377. {
  378. var foreignKeys = tagForeignKeys
  379. var associationForeignKeys = tagAssociationForeignKeys
  380. // if no foreign keys defined with tag
  381. if len(foreignKeys) == 0 {
  382. // if no association foreign keys defined with tag
  383. if len(associationForeignKeys) == 0 {
  384. for _, primaryField := range modelStruct.PrimaryFields {
  385. foreignKeys = append(foreignKeys, associationType+primaryField.Name)
  386. associationForeignKeys = append(associationForeignKeys, primaryField.Name)
  387. }
  388. } else {
  389. // generate foreign keys form association foreign keys
  390. for _, associationForeignKey := range tagAssociationForeignKeys {
  391. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  392. foreignKeys = append(foreignKeys, associationType+foreignField.Name)
  393. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  394. }
  395. }
  396. }
  397. } else {
  398. // generate association foreign keys from foreign keys
  399. if len(associationForeignKeys) == 0 {
  400. for _, foreignKey := range foreignKeys {
  401. if strings.HasPrefix(foreignKey, associationType) {
  402. associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
  403. if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
  404. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  405. }
  406. }
  407. }
  408. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  409. associationForeignKeys = []string{scope.PrimaryKey()}
  410. }
  411. } else if len(foreignKeys) != len(associationForeignKeys) {
  412. scope.Err(errors.New("invalid foreign keys, should have same length"))
  413. return
  414. }
  415. }
  416. for idx, foreignKey := range foreignKeys {
  417. if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
  418. if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
  419. foreignField.IsForeignKey = true
  420. // source foreign keys
  421. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
  422. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
  423. // association foreign keys
  424. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  425. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  426. }
  427. }
  428. }
  429. }
  430. if len(relationship.ForeignFieldNames) != 0 {
  431. relationship.Kind = "has_one"
  432. field.Relationship = relationship
  433. } else {
  434. var foreignKeys = tagForeignKeys
  435. var associationForeignKeys = tagAssociationForeignKeys
  436. if len(foreignKeys) == 0 {
  437. // generate foreign keys & association foreign keys
  438. if len(associationForeignKeys) == 0 {
  439. for _, primaryField := range toScope.PrimaryFields() {
  440. foreignKeys = append(foreignKeys, field.Name+primaryField.Name)
  441. associationForeignKeys = append(associationForeignKeys, primaryField.Name)
  442. }
  443. } else {
  444. // generate foreign keys with association foreign keys
  445. for _, associationForeignKey := range associationForeignKeys {
  446. if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
  447. foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
  448. associationForeignKeys = append(associationForeignKeys, foreignField.Name)
  449. }
  450. }
  451. }
  452. } else {
  453. // generate foreign keys & association foreign keys
  454. if len(associationForeignKeys) == 0 {
  455. for _, foreignKey := range foreignKeys {
  456. if strings.HasPrefix(foreignKey, field.Name) {
  457. associationForeignKey := strings.TrimPrefix(foreignKey, field.Name)
  458. if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
  459. associationForeignKeys = append(associationForeignKeys, associationForeignKey)
  460. }
  461. }
  462. }
  463. if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
  464. associationForeignKeys = []string{toScope.PrimaryKey()}
  465. }
  466. } else if len(foreignKeys) != len(associationForeignKeys) {
  467. scope.Err(errors.New("invalid foreign keys, should have same length"))
  468. return
  469. }
  470. }
  471. for idx, foreignKey := range foreignKeys {
  472. if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
  473. if associationField := getForeignField(associationForeignKeys[idx], toFields); associationField != nil {
  474. foreignField.IsForeignKey = true
  475. // association foreign keys
  476. relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
  477. relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
  478. // source foreign keys
  479. relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
  480. relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
  481. }
  482. }
  483. }
  484. if len(relationship.ForeignFieldNames) != 0 {
  485. relationship.Kind = "belongs_to"
  486. field.Relationship = relationship
  487. }
  488. }
  489. }(field)
  490. default:
  491. field.IsNormal = true
  492. }
  493. }
  494. }
  495. // Even it is ignored, also possible to decode db value into the field
  496. if value, ok := field.TagSettings["COLUMN"]; ok {
  497. field.DBName = value
  498. } else {
  499. field.DBName = ToDBName(fieldStruct.Name)
  500. }
  501. modelStruct.StructFields = append(modelStruct.StructFields, field)
  502. }
  503. }
  504. if len(modelStruct.PrimaryFields) == 0 {
  505. if field := getForeignField("id", modelStruct.StructFields); field != nil {
  506. field.IsPrimaryKey = true
  507. modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
  508. }
  509. }
  510. modelStructsMap.Set(reflectType, &modelStruct)
  511. return &modelStruct
  512. }
  513. // GetStructFields get model's field structs
  514. func (scope *Scope) GetStructFields() (fields []*StructField) {
  515. return scope.GetModelStruct().StructFields
  516. }
  517. func parseTagSetting(tags reflect.StructTag) map[string]string {
  518. setting := map[string]string{}
  519. for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} {
  520. tags := strings.Split(str, ";")
  521. for _, value := range tags {
  522. v := strings.Split(value, ":")
  523. k := strings.TrimSpace(strings.ToUpper(v[0]))
  524. if len(v) >= 2 {
  525. setting[k] = strings.Join(v[1:], ":")
  526. } else {
  527. setting[k] = k
  528. }
  529. }
  530. }
  531. return setting
  532. }