Skip to content

Commit

Permalink
refact
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 29, 2014
1 parent b713479 commit 241b6bc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
4 changes: 1 addition & 3 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type Field struct {
Value interface{}
IsBlank bool
IsIgnored bool
Tag string
AddationalTag string
Size int
Tag reflect.StructTag
SqlTag string
ForeignKey string
BeforeAssociation bool
Expand Down
27 changes: 12 additions & 15 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ func (scope *Scope) CombinedConditionSql() string {
}

func (scope *Scope) SqlTagForField(field *Field) (tag string) {
tag, addationalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier))

if tag == "-" {
field.IsIgnored = true
}

value := field.Value
reflectValue := reflect.ValueOf(value)

Expand All @@ -260,17 +266,16 @@ func (scope *Scope) SqlTagForField(field *Field) (tag string) {
}
}

tag = field.Tag
if len(tag) == 0 && tag != "-" {
if field.isPrimaryKey {
tag = scope.Dialect().PrimaryKeyTag(value, field.Size)
tag = scope.Dialect().PrimaryKeyTag(value, size)
} else {
tag = scope.Dialect().SqlTag(value, field.Size)
tag = scope.Dialect().SqlTag(value, size)
}
}

if len(field.AddationalTag) > 0 {
tag = tag + " " + field.AddationalTag
if len(addationalTag) > 0 {
tag = tag + " " + addationalTag
}
return
}
Expand All @@ -297,20 +302,12 @@ func (scope *Scope) Fields() []*Field {
value := indirectValue.FieldByName(fieldStruct.Name)
field.Value = value.Interface()
field.IsBlank = isBlank(value)
field.isPrimaryKey = scope.PrimaryKey() == field.DBName

if scope.db != nil {
tag, addationalTag, size := parseSqlTag(fieldStruct.Tag.Get(scope.db.parent.tagIdentifier))
field.Tag = tag
field.AddationalTag = addationalTag
field.isPrimaryKey = scope.PrimaryKey() == field.DBName
field.Size = size

field.Tag = fieldStruct.Tag
field.SqlTag = scope.SqlTagForField(&field)

if tag == "-" {
field.IsIgnored = true
}

// parse association
elem := reflect.Indirect(value)
typ := elem.Type()
Expand Down

0 comments on commit 241b6bc

Please sign in to comment.