forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: field tag * feat: rm new field * feat: test * feat: sort tag * feat: support comment tag and default tag for primary key * feat: comment tag test
- Loading branch information
Showing
18 changed files
with
246 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package field | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const ( | ||
TagKeyGorm = "gorm" | ||
TagKeyJson = "json" | ||
|
||
//gorm tag | ||
TagKeyGormColumn = "column" | ||
TagKeyGormType = "type" | ||
TagKeyGormPrimaryKey = "primaryKey" | ||
TagKeyGormAutoIncrement = "autoIncrement" | ||
TagKeyGormNotNull = "not null" | ||
TagKeyGormUniqueIndex = "uniqueIndex" | ||
TagKeyGormIndex = "index" | ||
TagKeyGormDefault = "default" | ||
TagKeyGormComment = "comment" | ||
) | ||
|
||
var ( | ||
tagKeyPriorities = map[string]int16{ | ||
TagKeyGorm: 100, | ||
TagKeyJson: 99, | ||
|
||
TagKeyGormColumn: 10, | ||
TagKeyGormType: 9, | ||
TagKeyGormPrimaryKey: 8, | ||
TagKeyGormAutoIncrement: 7, | ||
TagKeyGormNotNull: 6, | ||
TagKeyGormUniqueIndex: 5, | ||
TagKeyGormIndex: 4, | ||
TagKeyGormDefault: 3, | ||
TagKeyGormComment: 0, | ||
} | ||
) | ||
|
||
type TagBuilder interface { | ||
Build() string | ||
} | ||
|
||
type Tag map[string]string | ||
|
||
func NewTag() Tag { | ||
return Tag{} | ||
} | ||
|
||
func (tag Tag) Set(key, value string) { | ||
tag[key] = value | ||
} | ||
|
||
func (tag Tag) Remove(key string) { | ||
delete(tag, key) | ||
} | ||
|
||
func (tag Tag) Build() string { | ||
if tag == nil || len(tag) == 0 { | ||
return "" | ||
} | ||
|
||
tags := make([]string, 0, len(tag)) | ||
keys := tagKeySort(tag) | ||
for _, k := range keys { | ||
v := tag[k] | ||
if k == "" || v == "" { | ||
continue | ||
} | ||
tags = append(tags, k+":\""+v+"\"") | ||
} | ||
return strings.Join(tags, " ") | ||
} | ||
|
||
type GormTag Tag | ||
|
||
func NewGormTag() GormTag { | ||
return GormTag{} | ||
} | ||
|
||
func (tag GormTag) Set(key, value string) { | ||
tag[key] = value | ||
} | ||
|
||
func (tag GormTag) Remove(key string) { | ||
delete(tag, key) | ||
} | ||
|
||
func (tag GormTag) Build() string { | ||
if tag == nil || len(tag) == 0 { | ||
return "" | ||
} | ||
tags := make([]string, 0, len(tag)) | ||
keys := tagKeySort(Tag(tag)) | ||
for _, k := range keys { | ||
v := tag[k] | ||
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, ":")) | ||
} | ||
|
||
return strings.Join(tags, ";") | ||
} | ||
|
||
func tagKeySort(tag Tag) []string { | ||
keys := make([]string, 0, len(tag)) | ||
if len(tag) == 0 { | ||
return keys | ||
} | ||
for k, _ := range tag { | ||
keys = append(keys, k) | ||
} | ||
sort.Slice(keys, func(i, j int) bool { | ||
if tagKeyPriorities[keys[i]] == tagKeyPriorities[keys[j]] { | ||
return keys[i] <= keys[j] | ||
} | ||
return tagKeyPriorities[keys[i]] > tagKeyPriorities[keys[j]] | ||
}) | ||
return keys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.