Add Tag for Field

This commit is contained in:
Jinzhu 2015-02-17 18:52:10 +08:00
parent a38f83527a
commit fcb6b06d22
1 changed files with 24 additions and 18 deletions

View File

@ -27,9 +27,8 @@ type StructField struct {
IsNormal bool IsNormal bool
IsIgnored bool IsIgnored bool
DefaultValue *string DefaultValue *string
GormSettings map[string]string
SqlSettings map[string]string
SqlTag string SqlTag string
Tag reflect.StructTag
Struct reflect.StructField Struct reflect.StructField
Relationship *Relationship Relationship *Relationship
} }
@ -47,13 +46,14 @@ type Relationship struct {
func (scope *Scope) generateSqlTag(field *StructField) { func (scope *Scope) generateSqlTag(field *StructField) {
var sqlType string var sqlType string
reflectValue := reflect.Indirect(reflect.New(field.Struct.Type)) reflectValue := reflect.Indirect(reflect.New(field.Struct.Type))
sqlSettings := parseTagSetting(field.Tag.Get("sql"))
if value, ok := field.SqlSettings["TYPE"]; ok { if value, ok := sqlSettings["TYPE"]; ok {
sqlType = value sqlType = value
} }
additionalType := field.SqlSettings["NOT NULL"] + " " + field.SqlSettings["UNIQUE"] additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
if value, ok := field.SqlSettings["DEFAULT"]; ok { if value, ok := sqlSettings["DEFAULT"]; ok {
additionalType = additionalType + "DEFAULT " + value additionalType = additionalType + "DEFAULT " + value
} }
@ -71,7 +71,7 @@ func (scope *Scope) generateSqlTag(field *StructField) {
if sqlType == "" { if sqlType == "" {
var size = 255 var size = 255
if value, ok := field.SqlSettings["SIZE"]; ok { if value, ok := sqlSettings["SIZE"]; ok {
size, _ = strconv.Atoi(value) size, _ = strconv.Atoi(value)
} }
@ -138,22 +138,28 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
continue continue
} }
field := &StructField{Struct: fieldStruct, Name: fieldStruct.Name, Names: []string{fieldStruct.Name}} field := &StructField{
Struct: fieldStruct,
Name: fieldStruct.Name,
Names: []string{fieldStruct.Name},
Tag: fieldStruct.Tag,
}
if fieldStruct.Tag.Get("sql") == "-" { if fieldStruct.Tag.Get("sql") == "-" {
field.IsIgnored = true field.IsIgnored = true
} else { } else {
field.SqlSettings = parseTagSetting(fieldStruct.Tag.Get("sql")) sqlSettings := parseTagSetting(field.Tag.Get("sql"))
field.GormSettings = parseTagSetting(fieldStruct.Tag.Get("gorm")) gormSettings := parseTagSetting(field.Tag.Get("gorm"))
if _, ok := field.GormSettings["PRIMARY_KEY"]; ok { if _, ok := gormSettings["PRIMARY_KEY"]; ok {
field.IsPrimaryKey = true field.IsPrimaryKey = true
modelStruct.PrimaryKeyField = field modelStruct.PrimaryKeyField = field
} }
if value, ok := field.SqlSettings["DEFAULT"]; ok { if value, ok := sqlSettings["DEFAULT"]; ok {
field.DefaultValue = &value field.DefaultValue = &value
} }
if value, ok := field.GormSettings["COLUMN"]; ok { if value, ok := gormSettings["COLUMN"]; ok {
field.DBName = value field.DBName = value
} else { } else {
field.DBName = ToSnake(fieldStruct.Name) field.DBName = ToSnake(fieldStruct.Name)
@ -172,11 +178,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
field.IsTime, field.IsNormal = true, true field.IsTime, field.IsNormal = true, true
} }
many2many := field.GormSettings["MANY2MANY"] many2many := gormSettings["MANY2MANY"]
foreignKey := SnakeToUpperCamel(field.GormSettings["FOREIGNKEY"]) foreignKey := SnakeToUpperCamel(gormSettings["FOREIGNKEY"])
foreignType := SnakeToUpperCamel(field.GormSettings["FOREIGNTYPE"]) foreignType := SnakeToUpperCamel(gormSettings["FOREIGNTYPE"])
associationForeignKey := SnakeToUpperCamel(field.GormSettings["ASSOCIATIONFOREIGNKEY"]) associationForeignKey := SnakeToUpperCamel(gormSettings["ASSOCIATIONFOREIGNKEY"])
if polymorphic := SnakeToUpperCamel(field.GormSettings["POLYMORPHIC"]); polymorphic != "" { if polymorphic := SnakeToUpperCamel(gormSettings["POLYMORPHIC"]); polymorphic != "" {
foreignKey = polymorphic + "Id" foreignKey = polymorphic + "Id"
foreignType = polymorphic + "Type" foreignType = polymorphic + "Type"
} }
@ -219,7 +225,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
field.IsNormal = true field.IsNormal = true
} }
case reflect.Struct: case reflect.Struct:
if _, ok := field.GormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous { if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() { for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
field.Names = append([]string{fieldStruct.Name}, field.Names...) field.Names = append([]string{fieldStruct.Name}, field.Names...)
modelStruct.StructFields = append(modelStruct.StructFields, field) modelStruct.StructFields = append(modelStruct.StructFields, field)