chore:update tag format

This commit is contained in:
yaofeng-wang 2024-01-09 22:35:09 +08:00
parent 9107658c97
commit a9c1520b58
2 changed files with 16 additions and 11 deletions

View File

@ -127,16 +127,21 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
}
if field.PrimaryKey {
// default priority is 10, to be consistent with composite index
field.PrimaryKeyPriority = 10
priority := ParseTagSetting(field.TagSettings["PRIMARYKEY"], ",")["PRIORITY"]
if priority == "" {
priority = ParseTagSetting(field.TagSettings["PRIMARY_KEY"], ",")["PRIORITY"]
}
if field.TagSettings["PRIMARYKEY,PRIORITY"] != "" {
primaryKeyPriority, err := strconv.Atoi(tagSetting["PRIMARYKEY,PRIORITY"])
if priority != "" {
primaryKeyPriority, err := strconv.Atoi(priority)
if err == nil {
field.PrimaryKeyPriority = primaryKeyPriority
}
}
}
for field.IndirectFieldType.Kind() == reflect.Ptr {
field.IndirectFieldType = field.IndirectFieldType.Elem()
}

View File

@ -334,27 +334,27 @@ func TestTypeAliasField(t *testing.T) {
}
type UserWithCompositePrimaryKey struct {
Foo uint `gorm:"primaryKey,priority:2;autoIncrement:false"`
Bar uint `gorm:"primaryKey,priority:1"`
Foo uint `gorm:"primaryKey:,priority:2;autoIncrement:false"`
Bar uint `gorm:"primaryKey:,priority:1"`
Baz uint `gorm:"primaryKey"`
}
func TestParseFieldWithCompositePrimaryKey(t *testing.T) {
user, err := schema.Parse(&UserWithCompositePrimaryKey{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("Failed to parse user with permission, got error %v", err)
t.Fatalf("Failed to parse user with composite key, got error %v", err)
}
fields := []*schema.Field{
{
Name: "Foo", DBName: "foo", BindNames: []string{"Foo"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 2, Creatable: true, Updatable: true, Readable: true, Size: 64,
TagSettings: map[string]string{"AUTOINCREMENT": "false", "PRIMARYKEY,PRIORITY": "2"},
TagSettings: map[string]string{"AUTOINCREMENT": "false", "PRIMARYKEY": ",priority:2"},
},
{
Name: "Bar", DBName: "bar", BindNames: []string{"Bar"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 1, Creatable: true, Updatable: true, Readable: true, Size: 64,
TagSettings: map[string]string{"PRIMARYKEY,PRIORITY": "1"},
TagSettings: map[string]string{"PRIMARYKEY": ",priority:1"},
},
{Name: "Baz", DBName: "baz", BindNames: []string{"Baz"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 10, Creatable: true, Updatable: true, Readable: true, Size: 64,