Refactor ParseTagSetting

This commit is contained in:
Jinzhu 2020-02-22 00:02:05 +08:00
parent ad419855e9
commit ea0b13f7a3
5 changed files with 79 additions and 80 deletions

View File

@ -74,7 +74,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
Creatable: true, Creatable: true,
Updatable: true, Updatable: true,
Tag: fieldStruct.Tag, Tag: fieldStruct.Tag,
TagSettings: ParseTagSetting(fieldStruct.Tag), TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"),
Schema: schema, Schema: schema,
} }
@ -104,7 +104,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} }
// copy tag settings from valuer // copy tag settings from valuer
for key, value := range ParseTagSetting(field.IndirectFieldType.Field(i).Tag) { for key, value := range ParseTagSetting(field.IndirectFieldType.Field(i).Tag.Get("gorm"), ";") {
if _, ok := field.TagSettings[key]; !ok { if _, ok := field.TagSettings[key]; !ok {
field.TagSettings[key] = value field.TagSettings[key] = value
} }

View File

@ -6,9 +6,12 @@ import (
) )
type Index struct { type Index struct {
Name string Name string
Class string // UNIQUE | FULLTEXT | SPATIAL Class string // UNIQUE | FULLTEXT | SPATIAL
Fields []IndexOption Type string // btree, hash, gist, spgist, gin, and brin
Where string
Comment string
Fields []IndexOption
} }
type IndexOption struct { type IndexOption struct {
@ -17,9 +20,6 @@ type IndexOption struct {
Sort string // DESC, ASC Sort string // DESC, ASC
Collate string Collate string
Length int Length int
Type string // btree, hash, gist, spgist, gin, and brin
Where string
Comment string
} }
// ParseIndexes parse schema indexes // ParseIndexes parse schema indexes
@ -34,6 +34,15 @@ func (schema *Schema) ParseIndexes() map[string]Index {
if idx.Class == "" { if idx.Class == "" {
idx.Class = index.Class idx.Class = index.Class
} }
if idx.Type == "" {
idx.Type = index.Type
}
if idx.Where == "" {
idx.Where = index.Where
}
if idx.Comment == "" {
idx.Comment = index.Comment
}
idx.Fields = append(idx.Fields, index.Fields...) idx.Fields = append(idx.Fields, index.Fields...)
indexes[index.Name] = idx indexes[index.Name] = idx
} }
@ -50,62 +59,37 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
k := strings.TrimSpace(strings.ToUpper(v[0])) k := strings.TrimSpace(strings.ToUpper(v[0]))
if k == "INDEX" || k == "UNIQUE_INDEX" { if k == "INDEX" || k == "UNIQUE_INDEX" {
var ( var (
name string name string
tag = strings.Join(v[1:], ":") tag = strings.Join(v[1:], ":")
settings = map[string]string{} idx = strings.Index(tag, ",")
settings = ParseTagSetting(tag, ",")
length, _ = strconv.Atoi(settings["LENGTH"])
) )
names := strings.Split(tag, ",") if idx != -1 {
for i := 0; i < len(names); i++ { name = tag[0:idx]
if len(names[i]) > 0 {
j := i
for {
if names[j][len(names[j])-1] == '\\' {
i++
names[j] = names[j][0:len(names[j])-1] + names[i]
names[i] = ""
} else {
break
}
}
}
if i == 0 {
name = names[0]
}
values := strings.Split(names[i], ":")
k := strings.TrimSpace(strings.ToUpper(values[0]))
if len(values) >= 2 {
settings[k] = strings.Join(values[1:], ":")
} else if k != "" {
settings[k] = k
}
} }
if name == "" { if name == "" {
name = field.Schema.namer.IndexName(field.Schema.Table, field.Name) name = field.Schema.namer.IndexName(field.Schema.Table, field.Name)
} }
length, _ := strconv.Atoi(settings["LENGTH"])
if (k == "UNIQUE_INDEX") || settings["UNIQUE"] != "" { if (k == "UNIQUE_INDEX") || settings["UNIQUE"] != "" {
settings["CLASS"] = "UNIQUE" settings["CLASS"] = "UNIQUE"
} }
indexes = append(indexes, Index{ indexes = append(indexes, Index{
Name: name, Name: name,
Class: settings["CLASS"], Class: settings["CLASS"],
Type: settings["TYPE"],
Where: settings["WHERE"],
Comment: settings["COMMENT"],
Fields: []IndexOption{{ Fields: []IndexOption{{
Field: field, Field: field,
Expression: settings["EXPRESSION"], Expression: settings["EXPRESSION"],
Sort: settings["SORT"], Sort: settings["SORT"],
Collate: settings["COLLATE"], Collate: settings["COLLATE"],
Type: settings["TYPE"],
Length: length, Length: length,
Where: settings["WHERE"],
Comment: settings["COMMENT"],
}}, }},
}) })
} }

View File

@ -35,13 +35,13 @@ func TestParseIndex(t *testing.T) {
Fields: []schema.IndexOption{{}}, Fields: []schema.IndexOption{{}},
}, },
"idx_user_indices_name3": { "idx_user_indices_name3": {
Name: "idx_user_indices_name3", Name: "idx_user_indices_name3",
Type: "btree",
Where: "name3 != 'jinzhu'",
Fields: []schema.IndexOption{{ Fields: []schema.IndexOption{{
Sort: "desc", Sort: "desc",
Collate: "utf8", Collate: "utf8",
Length: 10, Length: 10,
Type: "btree",
Where: "name3 != 'jinzhu'",
}}, }},
}, },
"idx_user_indices_name4": { "idx_user_indices_name4": {
@ -50,19 +50,17 @@ func TestParseIndex(t *testing.T) {
Fields: []schema.IndexOption{{}}, Fields: []schema.IndexOption{{}},
}, },
"idx_user_indices_name5": { "idx_user_indices_name5": {
Name: "idx_user_indices_name5", Name: "idx_user_indices_name5",
Class: "FULLTEXT", Class: "FULLTEXT",
Fields: []schema.IndexOption{{ Comment: "hello , world",
Comment: "hello , world", Where: "age > 10",
Where: "age > 10", Fields: []schema.IndexOption{{}},
}},
}, },
"profile": { "profile": {
Name: "profile", Name: "profile",
Fields: []schema.IndexOption{{ Comment: "hello , world",
Comment: "hello , world", Where: "age > 10",
Where: "age > 10", Fields: []schema.IndexOption{{}, {
}, {
Expression: "(age+10)", Expression: "(age+10)",
}}, }},
}, },
@ -76,19 +74,23 @@ func TestParseIndex(t *testing.T) {
t.Errorf("Failed to found index %v from parsed indices %+v", k, indices) t.Errorf("Failed to found index %v from parsed indices %+v", k, indices)
} }
if result.Name != v.Name { for _, name := range []string{"Name", "Class", "Type", "Where", "Comment"} {
t.Errorf("index %v name should equal, expects %v, got %v", k, result.Name, v.Name) if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() {
} t.Errorf(
"index %v %v should equal, expects %v, got %v",
if result.Class != v.Class { k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(),
t.Errorf("index %v Class should equal, expects %v, got %v", k, result.Class, v.Class) )
}
} }
for idx, ef := range result.Fields { for idx, ef := range result.Fields {
rf := v.Fields[idx] rf := v.Fields[idx]
for _, name := range []string{"Expression", "Sort", "Collate", "Length", "Type", "Where"} { for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() { if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {
t.Errorf("index %v field #%v's %v should equal, expects %v, got %v", k, idx+1, name, reflect.ValueOf(ef).FieldByName(name).Interface(), reflect.ValueOf(rf).FieldByName(name).Interface()) t.Errorf(
"index %v field #%v's %v should equal, expects %v, got %v", k, idx+1, name,
reflect.ValueOf(ef).FieldByName(name).Interface(), reflect.ValueOf(rf).FieldByName(name).Interface(),
)
} }
} }
} }

View File

@ -44,7 +44,7 @@ func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*
if f.TagSettings == nil { if f.TagSettings == nil {
if f.Tag != "" { if f.Tag != "" {
f.TagSettings = schema.ParseTagSetting(f.Tag) f.TagSettings = schema.ParseTagSetting(f.Tag.Get("gorm"), ";")
} else { } else {
f.TagSettings = map[string]string{} f.TagSettings = map[string]string{}
} }

View File

@ -6,22 +6,35 @@ import (
"strings" "strings"
) )
func ParseTagSetting(tags reflect.StructTag) map[string]string { func ParseTagSetting(str string, sep string) map[string]string {
setting := map[string]string{} settings := map[string]string{}
names := strings.Split(str, sep)
for _, value := range strings.Split(tags.Get("gorm"), ";") { for i := 0; i < len(names); i++ {
if value != "" { j := i
v := strings.Split(value, ":") if len(names[j]) > 0 {
k := strings.TrimSpace(strings.ToUpper(v[0])) for {
if names[j][len(names[j])-1] == '\\' {
if len(v) >= 2 { i++
setting[k] = strings.Join(v[1:], ":") names[j] = names[j][0:len(names[j])-1] + sep + names[i]
} else { names[i] = ""
setting[k] = k } else {
break
}
} }
} }
values := strings.Split(names[j], ":")
k := strings.TrimSpace(strings.ToUpper(values[0]))
if len(values) >= 2 {
settings[k] = strings.Join(values[1:], ":")
} else if k != "" {
settings[k] = k
}
} }
return setting
return settings
} }
func checkTruth(val string) bool { func checkTruth(val string) bool {