Skip to content

Commit

Permalink
fix: gorm tag of multi index
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed May 9, 2023
1 parent 1ec6569 commit 79edf1f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 34 deletions.
66 changes: 50 additions & 16 deletions field/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func (tag Tag) Build() string {
}

tags := make([]string, 0, len(tag))
keys := tagKeySort(tag)
for _, k := range keys {
for _, k := range tagKeys(tag) {
v := tag[k]
if k == "" || v == "" {
continue
Expand All @@ -71,10 +70,19 @@ func (tag Tag) Build() string {
return strings.Join(tags, " ")
}

type GormTag Tag
type GormTag map[string][]string

func (tag GormTag) Set(key, value string) GormTag {
tag[key] = value
func (tag GormTag) Append(key string, values ...string) GormTag {
if _, ok := tag[key]; ok {
tag[key] = append(tag[key], values...)
} else {
tag[key] = values
}
return tag
}

func (tag GormTag) Set(key string, values ...string) GormTag {
tag[key] = values
return tag
}

Expand All @@ -88,33 +96,59 @@ func (tag GormTag) Build() string {
return ""
}
tags := make([]string, 0, len(tag))
keys := tagKeySort(Tag(tag))
for _, k := range keys {
v := tag[k]
if k == "" && v == "" {
for _, k := range gormKeys(tag) {
vs := tag[k]
if len(vs) == 0 && k == "" {
continue
}
tv := make([]string, 0, 2)
if k != "" {
tv = append(tv, k)
if len(vs) == 0 {
tags = append(tags, k)
continue
}
if v != "" {
tv = append(tv, v)
for _, v := range vs {
if k == "" && v == "" {
continue
}
tv := make([]string, 0, 2)
if k != "" {
tv = append(tv, k)
}
if v != "" {
tv = append(tv, v)
}
tags = append(tags, strings.Join(tv, ":"))
}
tags = append(tags, strings.Join(tv, ":"))
}

return strings.Join(tags, ";")
}

func tagKeySort(tag Tag) []string {
func tagKeys(tag Tag) []string {
keys := make([]string, 0, len(tag))
if len(tag) == 0 {
return keys
}
for k, _ := range tag {
keys = append(keys, k)
}
return keySort(keys)
}

func gormKeys(tag GormTag) []string {
keys := make([]string, 0, len(tag))
if len(tag) == 0 {
return keys
}
for k, _ := range tag {
keys = append(keys, k)
}
return keySort(keys)
}

func keySort(keys []string) []string {
if len(keys) == 0 {
return keys
}
sort.Slice(keys, func(i, j int) bool {
if tagKeyPriorities[keys[i]] == tagKeyPriorities[keys[j]] {
return keys[i] <= keys[j]
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require (
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c
gorm.io/driver/clickhouse v0.5.0
gorm.io/driver/mysql v1.4.4
gorm.io/driver/mysql v1.5.1-0.20230509030346-3715c134c25b
gorm.io/driver/postgres v1.4.5
gorm.io/driver/sqlite v1.4.3
gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.2
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677
gorm.io/hints v1.1.0
gorm.io/plugin/dbresolver v1.3.0
)
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c/go.mod h1:SH2K9R+2RMjuX1C
gorm.io/driver/clickhouse v0.5.0 h1:aT6fJOQ3PLBJ7q0SGPSXgIv/EOoYjOaHJ0e+1MLm7yw=
gorm.io/driver/clickhouse v0.5.0/go.mod h1:cIKAlFw+IVK75g0bDcm0M9qRA4EAgsn23Si+zCXQ1Lc=
gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U=
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
gorm.io/driver/mysql v1.5.1-0.20230509030346-3715c134c25b h1:O7DuK4tml7U+sG1NJmGXz3LXaO6Goblps5Gx4NBuxis=
gorm.io/driver/mysql v1.5.1-0.20230509030346-3715c134c25b/go.mod h1:RpAr+f2lUtJUm0e2FAbttXiUKgAqKSUtzI1ulJfz9xU=
gorm.io/driver/postgres v1.4.5 h1:mTeXTTtHAgnS9PgmhN2YeUbazYpLhUI1doLnw42XUZc=
gorm.io/driver/postgres v1.4.5/go.mod h1:GKNQYSJ14qvWkvPwXljMGehpKrhlDNsqYRr5HnYGncg=
gorm.io/driver/sqlite v1.1.6/go.mod h1:W8LmC/6UvVbHKah0+QOC7Ja66EaZXHwUTjgXY8YNWX8=
Expand All @@ -377,12 +377,11 @@ gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHD
gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.10/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.1-0.20221019064659-5dd2bb482755/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677 h1:+vFpygNj+UclZztgzy/5ZGvkag6EH6moMm/MNTxkShk=
gorm.io/gorm v1.25.1-0.20230505075827-e61b98d69677/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/hints v1.1.0 h1:Lp4z3rxREufSdxn4qmkK3TLDltrM10FLTHiuqwDPvXw=
gorm.io/hints v1.1.0/go.mod h1:lKQ0JjySsPBj3uslFzY3JhYDtqEwzm+G1hv8rWujB6Y=
gorm.io/plugin/dbresolver v1.3.0 h1:uFDX3bIuH9Lhj5LY2oyqR/bU6pqWuDgas35NAPF4X3M=
Expand Down
8 changes: 4 additions & 4 deletions internal/model/tbl_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (c *Column) multilineComment() bool {

func (c *Column) buildGormTag() field.GormTag {
tag := field.GormTag{
field.TagKeyGormColumn: c.Name(),
field.TagKeyGormType: c.columnType(),
field.TagKeyGormColumn: []string{c.Name()},
field.TagKeyGormType: []string{c.columnType()},
}
isPriKey, ok := c.PrimaryKey()
isValidPriKey := ok && isPriKey
Expand All @@ -105,9 +105,9 @@ func (c *Column) buildGormTag() field.GormTag {
continue
}
if uniq, _ := idx.Unique(); uniq {
tag.Set(field.TagKeyGormUniqueIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority))
tag.Append(field.TagKeyGormUniqueIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority))
} else {
tag.Set(field.TagKeyGormIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority))
tag.Append(field.TagKeyGormIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority))
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/.expect/dal_2/model/users.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/.expect/dal_3/model/users.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/.expect/dal_4/model/users.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ CREATE TABLE `users` (
`company_id` bigint(20) unsigned DEFAULT '666',
`private_url` varchar(255) DEFAULT 'https://a.b.c ',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`) USING BTREE
KEY `idx_name` (`name`) USING BTREE,
KEY `idx_name_company_id` (`name`,`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


Expand Down

0 comments on commit 79edf1f

Please sign in to comment.