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 { if field.PrimaryKey {
// default priority is 10, to be consistent with composite index
field.PrimaryKeyPriority = 10 field.PrimaryKeyPriority = 10
}
if field.TagSettings["PRIMARYKEY,PRIORITY"] != "" { priority := ParseTagSetting(field.TagSettings["PRIMARYKEY"], ",")["PRIORITY"]
primaryKeyPriority, err := strconv.Atoi(tagSetting["PRIMARYKEY,PRIORITY"]) if priority == "" {
if err == nil { priority = ParseTagSetting(field.TagSettings["PRIMARY_KEY"], ",")["PRIORITY"]
field.PrimaryKeyPriority = primaryKeyPriority }
if priority != "" {
primaryKeyPriority, err := strconv.Atoi(priority)
if err == nil {
field.PrimaryKeyPriority = primaryKeyPriority
}
} }
} }
for field.IndirectFieldType.Kind() == reflect.Ptr { for field.IndirectFieldType.Kind() == reflect.Ptr {
field.IndirectFieldType = field.IndirectFieldType.Elem() field.IndirectFieldType = field.IndirectFieldType.Elem()
} }

View File

@ -334,27 +334,27 @@ func TestTypeAliasField(t *testing.T) {
} }
type UserWithCompositePrimaryKey struct { type UserWithCompositePrimaryKey struct {
Foo uint `gorm:"primaryKey,priority:2;autoIncrement:false"` Foo uint `gorm:"primaryKey:,priority:2;autoIncrement:false"`
Bar uint `gorm:"primaryKey,priority:1"` Bar uint `gorm:"primaryKey:,priority:1"`
Baz uint `gorm:"primaryKey"` Baz uint `gorm:"primaryKey"`
} }
func TestParseFieldWithCompositePrimaryKey(t *testing.T) { func TestParseFieldWithCompositePrimaryKey(t *testing.T) {
user, err := schema.Parse(&UserWithCompositePrimaryKey{}, &sync.Map{}, schema.NamingStrategy{}) user, err := schema.Parse(&UserWithCompositePrimaryKey{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil { 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{ fields := []*schema.Field{
{ {
Name: "Foo", DBName: "foo", BindNames: []string{"Foo"}, DataType: schema.Uint, PrimaryKey: true, Name: "Foo", DBName: "foo", BindNames: []string{"Foo"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 2, Creatable: true, Updatable: true, Readable: true, Size: 64, 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, Name: "Bar", DBName: "bar", BindNames: []string{"Bar"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 1, Creatable: true, Updatable: true, Readable: true, Size: 64, 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, {Name: "Baz", DBName: "baz", BindNames: []string{"Baz"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 10, Creatable: true, Updatable: true, Readable: true, Size: 64, PrimaryKeyPriority: 10, Creatable: true, Updatable: true, Readable: true, Size: 64,