Skip to content

Commit

Permalink
chore:update tag format
Browse files Browse the repository at this point in the history
  • Loading branch information
yaofeng-wang committed Jan 9, 2024
1 parent 9107658 commit a9c1520
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
17 changes: 11 additions & 6 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

if field.TagSettings["PRIMARYKEY,PRIORITY"] != "" {
primaryKeyPriority, err := strconv.Atoi(tagSetting["PRIMARYKEY,PRIORITY"])
if err == nil {
field.PrimaryKeyPriority = primaryKeyPriority
priority := ParseTagSetting(field.TagSettings["PRIMARYKEY"], ",")["PRIORITY"]
if priority == "" {
priority = ParseTagSetting(field.TagSettings["PRIMARY_KEY"], ",")["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()
}
Expand Down
10 changes: 5 additions & 5 deletions schema/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 359 in schema/field_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[gofumpt] reported by reviewdog 🐶 Raw Output: schema/field_test.go:359:- {Name: "Baz", DBName: "baz", BindNames: []string{"Baz"}, DataType: schema.Uint, PrimaryKey: true, schema/field_test.go:359:+ { schema/field_test.go:360:+ Name: "Baz", DBName: "baz", BindNames: []string{"Baz"}, DataType: schema.Uint, PrimaryKey: true,
PrimaryKeyPriority: 10, Creatable: true, Updatable: true, Readable: true, Size: 64,
Expand Down

0 comments on commit a9c1520

Please sign in to comment.