From 19b85b1f1756187bc2a3dfa4b15e559d423a38a7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 3 Jan 2016 09:52:27 +0800 Subject: [PATCH] Compatible with both gorm, sql tag --- main.go | 2 +- model_struct.go | 29 +++++++++++++++-------------- scope_private.go | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 8e5ed1ac..f3e86506 100644 --- a/main.go +++ b/main.go @@ -512,7 +512,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join scope := s.NewScope(source) for _, field := range scope.GetModelStruct().StructFields { if field.Name == column || field.DBName == column { - if many2many := parseTagSetting(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" { + if many2many := parseTagSetting(field.Tag)["MANY2MANY"]; many2many != "" { source := (&Scope{Value: source}).GetModelStruct().ModelType destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType handler.Setup(field.Relationship, many2many, source, destination) diff --git a/model_struct.go b/model_struct.go index 7a47540e..c4ad313b 100644 --- a/model_struct.go +++ b/model_struct.go @@ -145,14 +145,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct { field.IsIgnored = true } - sqlSettings := parseTagSetting(field.Tag.Get("sql")) - gormSettings := parseTagSetting(field.Tag.Get("gorm")) + gormSettings := parseTagSetting(field.Tag) if _, ok := gormSettings["PRIMARY_KEY"]; ok { field.IsPrimaryKey = true modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field) } - if _, ok := sqlSettings["DEFAULT"]; ok { + if _, ok := gormSettings["DEFAULT"]; ok { field.HasDefaultValue = true } @@ -185,7 +184,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } if !field.IsNormal { - gormSettings := parseTagSetting(field.Tag.Get("gorm")) + gormSettings := parseTagSetting(field.Tag) toScope := scope.New(reflect.New(fieldStruct.Type).Interface()) getForeignField := func(column string, fields []*StructField) *StructField { @@ -400,7 +399,7 @@ func (scope *Scope) generateSqlTag(field *StructField) string { structType = structType.Elem() } reflectValue := reflect.Indirect(reflect.New(structType)) - sqlSettings := parseTagSetting(field.Tag.Get("sql")) + sqlSettings := parseTagSetting(field.Tag) if value, ok := sqlSettings["TYPE"]; ok { sqlType = value @@ -447,16 +446,18 @@ func (scope *Scope) generateSqlTag(field *StructField) string { } } -func parseTagSetting(str string) map[string]string { - tags := strings.Split(str, ";") +func parseTagSetting(tags reflect.StructTag) map[string]string { setting := map[string]string{} - for _, value := range tags { - v := strings.Split(value, ":") - k := strings.TrimSpace(strings.ToUpper(v[0])) - if len(v) >= 2 { - setting[k] = strings.Join(v[1:], ":") - } else { - setting[k] = k + for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} { + tags := strings.Split(str, ";") + for _, value := range tags { + v := strings.Split(value, ":") + k := strings.TrimSpace(strings.ToUpper(v[0])) + if len(v) >= 2 { + setting[k] = strings.Join(v[1:], ":") + } else { + setting[k] = k + } } } return setting diff --git a/scope_private.go b/scope_private.go index eddcfcc3..d301e80e 100644 --- a/scope_private.go +++ b/scope_private.go @@ -630,7 +630,7 @@ func (scope *Scope) autoIndex() *Scope { var uniqueIndexes = map[string][]string{} for _, field := range scope.GetStructFields() { - sqlSettings := parseTagSetting(field.Tag.Get("sql")) + sqlSettings := parseTagSetting(field.Tag) if name, ok := sqlSettings["INDEX"]; ok { if name == "INDEX" { name = fmt.Sprintf("idx_%v_%v", scope.TableName(), field.DBName)