From 241b6bc3b945db09332b42451bfbecbda3c25b7d Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 29 Jan 2014 10:35:28 +0800 Subject: [PATCH] refact --- field.go | 4 +--- scope.go | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/field.go b/field.go index e3c05b35..57d94963 100644 --- a/field.go +++ b/field.go @@ -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 diff --git a/scope.go b/scope.go index 01c29515..ca291c4b 100644 --- a/scope.go +++ b/scope.go @@ -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) @@ -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 } @@ -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()