02GORM源碼解讀

簡介

GORM 源碼解讀, 基于 v1.9.11 版本.

定義模型

GORM 是 ORM, 所以模型定義是最重要的部分, 這一次來探究下具體實現(xiàn).

type User struct {
  gorm.Model
  Name         string
  Age          sql.NullInt64
  Birthday     *time.Time
  Email        string  `gorm:"type:varchar(100);unique_index"`
  Role         string  `gorm:"size:255"` // 設(shè)置字段大小為255
  MemberNumber *string `gorm:"unique;not null"` // 設(shè)置會員號(member number)唯一并且不為空
  Num          int     `gorm:"AUTO_INCREMENT"` // 設(shè)置 num 為自增類型
  Address      string  `gorm:"index:addr"` // 給address字段創(chuàng)建名為addr的索引
  IgnoreMe     int     `gorm:"-"` // 忽略本字段
}

這是官方文檔上的一個模型定義. 和普通的結(jié)構(gòu)體類似, 但多了屬于 gorm 的 tags.

所有的模型都應(yīng)該包含 gorm.Model, 看一下它的定義:

// Model base model definition, including fields `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`, which could be embedded in your models
//    type User struct {
//      gorm.Model
//    }
type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

當然, 這并不是強制要求, 也可以不包含 gorm.Model, 它只是定義了一些非常基礎(chǔ)且實用的字段.

定義表的時候, 文檔上介紹了很多預(yù)設(shè), 比如默認 ID 是表的主鍵, 表名是結(jié)構(gòu)體名稱的復數(shù)等.

ModelStruct

要深入了解模型定義, 要從 ModelStruct 開始:

// ModelStruct model definition
type ModelStruct struct {
    PrimaryFields []*StructField
    StructFields  []*StructField
    ModelType     reflect.Type

    defaultTableName string
    l                sync.Mutex
}

ModelStruct 定義了模型結(jié)構(gòu)體的輪廓, 包含主鍵字段的切片, 普通字段的切片, 模型類型, 默認表名.

獲取表名

ModelStruct 有一個方法獲取模型的表名, 看一下它的具體代碼:

// TableName returns model's table name
func (s *ModelStruct) TableName(db *DB) string {
    s.l.Lock()
    defer s.l.Unlock()

    if s.defaultTableName == "" && db != nil && s.ModelType != nil {
        // Set default table name
        if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
            s.defaultTableName = tabler.TableName()
        } else {
            tableName := ToTableName(s.ModelType.Name())
            db.parent.RLock()
            if db == nil || (db.parent != nil && !db.parent.singularTable) {
                tableName = inflection.Plural(tableName)
            }
            db.parent.RUnlock()
            s.defaultTableName = tableName
        }
    }

    return DefaultTableNameHandler(db, s.defaultTableName)
}

首先, 使用反射檢查是否實現(xiàn)了 tabler 接口, 如果實現(xiàn)了, 直接調(diào)用 TableName() 方法;
沒有實現(xiàn)就使用 ToTableName 轉(zhuǎn)換表名, 有條件地將表名轉(zhuǎn)換為復數(shù)形式;
最后一步, 對于所有的表名使用 DefaultTableNameHandler 鉤子函數(shù)進行再次轉(zhuǎn)換.

看過源碼之后, 就能更好的理解文檔上關(guān)于表名的說明了.

StructField

看一下 StructField 的定義, 即表中的字段是如何表示的:

// StructField model field's struct definition
type StructField struct {
    DBName          string
    Name            string
    Names           []string
    IsPrimaryKey    bool
    IsNormal        bool
    IsIgnored       bool
    IsScanner       bool
    HasDefaultValue bool
    Tag             reflect.StructTag
    TagSettings     map[string]string
    Struct          reflect.StructField
    IsForeignKey    bool
    Relationship    *Relationship

    tagSettingsLock sync.RWMutex
}

定義了很多字段, 從字段的名字中可以猜測出很多信息, 比如該字段是否是主鍵等.

注意到有個 TagSettings 字段, 以及配套的 tagSettingsLock 讀寫鎖.

// TagSettingsSet Sets a tag in the tag settings map
func (sf *StructField) TagSettingsSet(key, val string) {
    sf.tagSettingsLock.Lock()
    defer sf.tagSettingsLock.Unlock()
    sf.TagSettings[key] = val
}

// TagSettingsGet returns a tag from the tag settings
func (sf *StructField) TagSettingsGet(key string) (string, bool) {
    sf.tagSettingsLock.RLock()
    defer sf.tagSettingsLock.RUnlock()
    val, ok := sf.TagSettings[key]
    return val, ok
}

// TagSettingsDelete deletes a tag
func (sf *StructField) TagSettingsDelete(key string) {
    sf.tagSettingsLock.Lock()
    defer sf.tagSettingsLock.Unlock()
    delete(sf.TagSettings, key)
}

這些方法都是和 TagSettings 有關(guān)的, 也可以看作是讀寫鎖 sync.RWMutex 的使用范例.

最后一個方法是關(guān)于復制結(jié)構(gòu)體的.

func (sf *StructField) clone() *StructField {
    clone := &StructField{
        DBName:          sf.DBName,
        Name:            sf.Name,
        Names:           sf.Names,
        IsPrimaryKey:    sf.IsPrimaryKey,
        IsNormal:        sf.IsNormal,
        IsIgnored:       sf.IsIgnored,
        IsScanner:       sf.IsScanner,
        HasDefaultValue: sf.HasDefaultValue,
        Tag:             sf.Tag,
        TagSettings:     map[string]string{},
        Struct:          sf.Struct,
        IsForeignKey:    sf.IsForeignKey,
    }

    if sf.Relationship != nil {
        relationship := *sf.Relationship
        clone.Relationship = &relationship
    }

    // copy the struct field tagSettings, they should be read-locked while they are copied
    sf.tagSettingsLock.Lock()
    defer sf.tagSettingsLock.Unlock()
    for key, value := range sf.TagSettings {
        clone.TagSettings[key] = value
    }

    return clone
}

復制 tagSettingsLock 中的字段時, 也用到了讀鎖.

Relationship

結(jié)構(gòu)體 Relationship 定義了關(guān)系類型.

type Relationship struct {
    Kind                         string
    PolymorphicType              string
    PolymorphicDBName            string
    PolymorphicValue             string
    ForeignFieldNames            []string
    ForeignDBNames               []string
    AssociationForeignFieldNames []string
    AssociationForeignDBNames    []string
    JoinTableHandler             JoinTableHandlerInterface
}
func getForeignField(column string, fields []*StructField) *StructField {
    for _, field := range fields {
        if field.Name == column || field.DBName == column || field.DBName == ToColumnName(column) {
            return field
        }
    }
    return nil
}

更多

在繼續(xù)探索如何解析模型定義之前, 先來了解一下 Scope 結(jié)構(gòu)體.

Scope

// Scope contain current operation's information when you perform any operation on the database
type Scope struct {
    Search          *search
    Value           interface{}
    SQL             string
    SQLVars         []interface{}
    db              *DB
    instanceID      string
    primaryKeyField *Field
    skipLeft        bool
    fields          *[]*Field
    selectAttrs     *[]string
}

Scope 是非常重要的一部分, 注釋中寫道, 當你在數(shù)據(jù)庫上執(zhí)行任何操作時, Scope 都會記錄當前操作的信息.

// IndirectValue return scope's reflect value's indirect value
func (scope *Scope) IndirectValue() reflect.Value {
    return indirect(reflect.ValueOf(scope.Value))
}

func indirect(reflectValue reflect.Value) reflect.Value {
    for reflectValue.Kind() == reflect.Ptr {
        reflectValue = reflectValue.Elem()
    }
    return reflectValue
}

// New create a new Scope without search information
func (scope *Scope) New(value interface{}) *Scope {
    return &Scope{db: scope.NewDB(), Search: &search{}, Value: value}
}

// NewDB create a new DB without search information
func (scope *Scope) NewDB() *DB {
    if scope.db != nil {
        db := scope.db.clone()
        db.search = nil
        db.Value = nil
        return db
    }
    return nil
}

Scope 下有很多方法, 先暫時不看. 對它的結(jié)構(gòu)有所了解之后, 回到模型解析上來.

模型解析

用戶定義模型之后, 就需要解析模型, 而這個工作是在 Scope 范圍內(nèi)完成的, 所以是其上的方法.

代碼很長, 先略覽它個大概, 感受一下整體結(jié)構(gòu).

// GetModelStruct get value's model struct, relationships based on struct and tag definition
func (scope *Scope) GetModelStruct() *ModelStruct {
    var modelStruct ModelStruct
    // Scope value can't be nil
    if scope.Value == nil {
        return &modelStruct
    }

    reflectType := reflect.ValueOf(scope.Value).Type()
    for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
        reflectType = reflectType.Elem()
    }

    // Scope value need to be a struct
    if reflectType.Kind() != reflect.Struct {
        return &modelStruct
    }

    // Get Cached model struct
    isSingularTable := false
    if scope.db != nil && scope.db.parent != nil {
        scope.db.parent.RLock()
        isSingularTable = scope.db.parent.singularTable
        scope.db.parent.RUnlock()
    }

    hashKey := struct {
        singularTable bool
        reflectType   reflect.Type
    }{isSingularTable, reflectType}
    if value, ok := modelStructsMap.Load(hashKey); ok && value != nil {
        return value.(*ModelStruct)
    }

    modelStruct.ModelType = reflectType

    // Get all fields
    for i := 0; i < reflectType.NumField(); i++ {
        if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
            field := &StructField{
                Struct:      fieldStruct,
                Name:        fieldStruct.Name,
                Names:       []string{fieldStruct.Name},
                Tag:         fieldStruct.Tag,
                TagSettings: parseTagSetting(fieldStruct.Tag),
            }

            // is ignored field
            if _, ok := field.TagSettingsGet("-"); ok {
                field.IsIgnored = true
            } else {
                if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok {
                    field.IsPrimaryKey = true
                    modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
                }

                if _, ok := field.TagSettingsGet("DEFAULT"); ok && !field.IsPrimaryKey {
                    field.HasDefaultValue = true
                }

                if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey {
                    field.HasDefaultValue = true
                }

                indirectType := fieldStruct.Type
                for indirectType.Kind() == reflect.Ptr {
                    indirectType = indirectType.Elem()
                }

                fieldValue := reflect.New(indirectType).Interface()
                if _, isScanner := fieldValue.(sql.Scanner); isScanner {
                    // is scanner
                    field.IsScanner, field.IsNormal = true, true
                    if indirectType.Kind() == reflect.Struct {
                        for i := 0; i < indirectType.NumField(); i++ {
                            for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
                                if _, ok := field.TagSettingsGet(key); !ok {
                                    field.TagSettingsSet(key, value)
                                }
                            }
                        }
                    }
                } else if _, isTime := fieldValue.(*time.Time); isTime {
                    // is time
                    field.IsNormal = true
                } else if _, ok := field.TagSettingsGet("EMBEDDED"); ok || fieldStruct.Anonymous {
                    // is embedded struct
                    for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
                        subField = subField.clone()
                        subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
                        if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
                            subField.DBName = prefix + subField.DBName
                        }

                        if subField.IsPrimaryKey {
                            if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
                                modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
                            } else {
                                subField.IsPrimaryKey = false
                            }
                        }

                        if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
                            if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
                                newJoinTableHandler := &JoinTableHandler{}
                                newJoinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
                                subField.Relationship.JoinTableHandler = newJoinTableHandler
                            }
                        }

                        modelStruct.StructFields = append(modelStruct.StructFields, subField)
                    }
                    continue
                } else {
                    // build relationships
                    switch indirectType.Kind() {
                    case reflect.Slice:
                        defer func(field *StructField) {
                            var (
                                relationship           = &Relationship{}
                                toScope                = scope.New(reflect.New(field.Struct.Type).Interface())
                                foreignKeys            []string
                                associationForeignKeys []string
                                elemType               = field.Struct.Type
                            )

                            if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
                                foreignKeys = strings.Split(foreignKey, ",")
                            }

                            if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
                                associationForeignKeys = strings.Split(foreignKey, ",")
                            } else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
                                associationForeignKeys = strings.Split(foreignKey, ",")
                            }

                            for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
                                elemType = elemType.Elem()
                            }

                            if elemType.Kind() == reflect.Struct {
                                if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" {
                                    relationship.Kind = "many_to_many"

                                    { // Foreign Keys for Source
                                        joinTableDBNames := []string{}

                                        if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
                                            joinTableDBNames = strings.Split(foreignKey, ",")
                                        }

                                        // if no foreign keys defined with tag
                                        if len(foreignKeys) == 0 {
                                            for _, field := range modelStruct.PrimaryFields {
                                                foreignKeys = append(foreignKeys, field.DBName)
                                            }
                                        }

                                        for idx, foreignKey := range foreignKeys {
                                            if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
                                                // source foreign keys (db names)
                                                relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)

                                                // setup join table foreign keys for source
                                                if len(joinTableDBNames) > idx {
                                                    // if defined join table's foreign key
                                                    relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx])
                                                } else {
                                                    defaultJointableForeignKey := ToColumnName(reflectType.Name()) + "_" + foreignField.DBName
                                                    relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey)
                                                }
                                            }
                                        }
                                    }

                                    { // Foreign Keys for Association (Destination)
                                        associationJoinTableDBNames := []string{}

                                        if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" {
                                            associationJoinTableDBNames = strings.Split(foreignKey, ",")
                                        }

                                        // if no association foreign keys defined with tag
                                        if len(associationForeignKeys) == 0 {
                                            for _, field := range toScope.PrimaryFields() {
                                                associationForeignKeys = append(associationForeignKeys, field.DBName)
                                            }
                                        }

                                        for idx, name := range associationForeignKeys {
                                            if field, ok := toScope.FieldByName(name); ok {
                                                // association foreign keys (db names)
                                                relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)

                                                // setup join table foreign keys for association
                                                if len(associationJoinTableDBNames) > idx {
                                                    relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx])
                                                } else {
                                                    // join table foreign keys for association
                                                    joinTableDBName := ToColumnName(elemType.Name()) + "_" + field.DBName
                                                    relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
                                                }
                                            }
                                        }
                                    }

                                    joinTableHandler := JoinTableHandler{}
                                    joinTableHandler.Setup(relationship, many2many, reflectType, elemType)
                                    relationship.JoinTableHandler = &joinTableHandler
                                    field.Relationship = relationship
                                } else {
                                    // User has many comments, associationType is User, comment use UserID as foreign key
                                    var associationType = reflectType.Name()
                                    var toFields = toScope.GetStructFields()
                                    relationship.Kind = "has_many"

                                    if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
                                        // Dog has many toys, tag polymorphic is Owner, then associationType is Owner
                                        // Toy use OwnerID, OwnerType ('dogs') as foreign key
                                        if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
                                            associationType = polymorphic
                                            relationship.PolymorphicType = polymorphicType.Name
                                            relationship.PolymorphicDBName = polymorphicType.DBName
                                            // if Dog has multiple set of toys set name of the set (instead of default 'dogs')
                                            if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
                                                relationship.PolymorphicValue = value
                                            } else {
                                                relationship.PolymorphicValue = scope.TableName()
                                            }
                                            polymorphicType.IsForeignKey = true
                                        }
                                    }

                                    // if no foreign keys defined with tag
                                    if len(foreignKeys) == 0 {
                                        // if no association foreign keys defined with tag
                                        if len(associationForeignKeys) == 0 {
                                            for _, field := range modelStruct.PrimaryFields {
                                                foreignKeys = append(foreignKeys, associationType+field.Name)
                                                associationForeignKeys = append(associationForeignKeys, field.Name)
                                            }
                                        } else {
                                            // generate foreign keys from defined association foreign keys
                                            for _, scopeFieldName := range associationForeignKeys {
                                                if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil {
                                                    foreignKeys = append(foreignKeys, associationType+foreignField.Name)
                                                    associationForeignKeys = append(associationForeignKeys, foreignField.Name)
                                                }
                                            }
                                        }
                                    } else {
                                        // generate association foreign keys from foreign keys
                                        if len(associationForeignKeys) == 0 {
                                            for _, foreignKey := range foreignKeys {
                                                if strings.HasPrefix(foreignKey, associationType) {
                                                    associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
                                                    if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
                                                        associationForeignKeys = append(associationForeignKeys, associationForeignKey)
                                                    }
                                                }
                                            }
                                            if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
                                                associationForeignKeys = []string{scope.PrimaryKey()}
                                            }
                                        } else if len(foreignKeys) != len(associationForeignKeys) {
                                            scope.Err(errors.New("invalid foreign keys, should have same length"))
                                            return
                                        }
                                    }

                                    for idx, foreignKey := range foreignKeys {
                                        if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
                                            if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
                                                // source foreign keys
                                                foreignField.IsForeignKey = true
                                                relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
                                                relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)

                                                // association foreign keys
                                                relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
                                                relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
                                            }
                                        }
                                    }

                                    if len(relationship.ForeignFieldNames) != 0 {
                                        field.Relationship = relationship
                                    }
                                }
                            } else {
                                field.IsNormal = true
                            }
                        }(field)
                    case reflect.Struct:
                        defer func(field *StructField) {
                            var (
                                // user has one profile, associationType is User, profile use UserID as foreign key
                                // user belongs to profile, associationType is Profile, user use ProfileID as foreign key
                                associationType           = reflectType.Name()
                                relationship              = &Relationship{}
                                toScope                   = scope.New(reflect.New(field.Struct.Type).Interface())
                                toFields                  = toScope.GetStructFields()
                                tagForeignKeys            []string
                                tagAssociationForeignKeys []string
                            )

                            if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
                                tagForeignKeys = strings.Split(foreignKey, ",")
                            }

                            if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
                                tagAssociationForeignKeys = strings.Split(foreignKey, ",")
                            } else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
                                tagAssociationForeignKeys = strings.Split(foreignKey, ",")
                            }

                            if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
                                // Cat has one toy, tag polymorphic is Owner, then associationType is Owner
                                // Toy use OwnerID, OwnerType ('cats') as foreign key
                                if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
                                    associationType = polymorphic
                                    relationship.PolymorphicType = polymorphicType.Name
                                    relationship.PolymorphicDBName = polymorphicType.DBName
                                    // if Cat has several different types of toys set name for each (instead of default 'cats')
                                    if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
                                        relationship.PolymorphicValue = value
                                    } else {
                                        relationship.PolymorphicValue = scope.TableName()
                                    }
                                    polymorphicType.IsForeignKey = true
                                }
                            }

                            // Has One
                            {
                                var foreignKeys = tagForeignKeys
                                var associationForeignKeys = tagAssociationForeignKeys
                                // if no foreign keys defined with tag
                                if len(foreignKeys) == 0 {
                                    // if no association foreign keys defined with tag
                                    if len(associationForeignKeys) == 0 {
                                        for _, primaryField := range modelStruct.PrimaryFields {
                                            foreignKeys = append(foreignKeys, associationType+primaryField.Name)
                                            associationForeignKeys = append(associationForeignKeys, primaryField.Name)
                                        }
                                    } else {
                                        // generate foreign keys form association foreign keys
                                        for _, associationForeignKey := range tagAssociationForeignKeys {
                                            if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
                                                foreignKeys = append(foreignKeys, associationType+foreignField.Name)
                                                associationForeignKeys = append(associationForeignKeys, foreignField.Name)
                                            }
                                        }
                                    }
                                } else {
                                    // generate association foreign keys from foreign keys
                                    if len(associationForeignKeys) == 0 {
                                        for _, foreignKey := range foreignKeys {
                                            if strings.HasPrefix(foreignKey, associationType) {
                                                associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
                                                if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
                                                    associationForeignKeys = append(associationForeignKeys, associationForeignKey)
                                                }
                                            }
                                        }
                                        if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
                                            associationForeignKeys = []string{scope.PrimaryKey()}
                                        }
                                    } else if len(foreignKeys) != len(associationForeignKeys) {
                                        scope.Err(errors.New("invalid foreign keys, should have same length"))
                                        return
                                    }
                                }

                                for idx, foreignKey := range foreignKeys {
                                    if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
                                        if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
                                            foreignField.IsForeignKey = true
                                            // source foreign keys
                                            relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
                                            relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)

                                            // association foreign keys
                                            relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
                                            relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
                                        }
                                    }
                                }
                            }

                            if len(relationship.ForeignFieldNames) != 0 {
                                relationship.Kind = "has_one"
                                field.Relationship = relationship
                            } else {
                                var foreignKeys = tagForeignKeys
                                var associationForeignKeys = tagAssociationForeignKeys

                                if len(foreignKeys) == 0 {
                                    // generate foreign keys & association foreign keys
                                    if len(associationForeignKeys) == 0 {
                                        for _, primaryField := range toScope.PrimaryFields() {
                                            foreignKeys = append(foreignKeys, field.Name+primaryField.Name)
                                            associationForeignKeys = append(associationForeignKeys, primaryField.Name)
                                        }
                                    } else {
                                        // generate foreign keys with association foreign keys
                                        for _, associationForeignKey := range associationForeignKeys {
                                            if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
                                                foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
                                                associationForeignKeys = append(associationForeignKeys, foreignField.Name)
                                            }
                                        }
                                    }
                                } else {
                                    // generate foreign keys & association foreign keys
                                    if len(associationForeignKeys) == 0 {
                                        for _, foreignKey := range foreignKeys {
                                            if strings.HasPrefix(foreignKey, field.Name) {
                                                associationForeignKey := strings.TrimPrefix(foreignKey, field.Name)
                                                if foreignField := getForeignField(associationForeignKey, toFields); foreignField != nil {
                                                    associationForeignKeys = append(associationForeignKeys, associationForeignKey)
                                                }
                                            }
                                        }
                                        if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
                                            associationForeignKeys = []string{toScope.PrimaryKey()}
                                        }
                                    } else if len(foreignKeys) != len(associationForeignKeys) {
                                        scope.Err(errors.New("invalid foreign keys, should have same length"))
                                        return
                                    }
                                }

                                for idx, foreignKey := range foreignKeys {
                                    if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
                                        if associationField := getForeignField(associationForeignKeys[idx], toFields); associationField != nil {
                                            foreignField.IsForeignKey = true

                                            // association foreign keys
                                            relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
                                            relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)

                                            // source foreign keys
                                            relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
                                            relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
                                        }
                                    }
                                }

                                if len(relationship.ForeignFieldNames) != 0 {
                                    relationship.Kind = "belongs_to"
                                    field.Relationship = relationship
                                }
                            }
                        }(field)
                    default:
                        field.IsNormal = true
                    }
                }
            }

            // Even it is ignored, also possible to decode db value into the field
            if value, ok := field.TagSettingsGet("COLUMN"); ok {
                field.DBName = value
            } else {
                field.DBName = ToColumnName(fieldStruct.Name)
            }

            modelStruct.StructFields = append(modelStruct.StructFields, field)
        }
    }

    if len(modelStruct.PrimaryFields) == 0 {
        if field := getForeignField("id", modelStruct.StructFields); field != nil {
            field.IsPrimaryKey = true
            modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
        }
    }

    modelStructsMap.Store(hashKey, &modelStruct)

    return &modelStruct
}

其實首先折疊一下中間的 for 循環(huán)會好很多.

// GetModelStruct get value's model struct, relationships based on struct and tag definition
func (scope *Scope) GetModelStruct() *ModelStruct {
    var modelStruct ModelStruct
    // Scope value can't be nil
    if scope.Value == nil {
        return &modelStruct
    }

    reflectType := reflect.ValueOf(scope.Value).Type()
    for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
        reflectType = reflectType.Elem()
    }

    // Scope value need to be a struct
    if reflectType.Kind() != reflect.Struct {
        return &modelStruct
    }

    // Get Cached model struct
    isSingularTable := false
    if scope.db != nil && scope.db.parent != nil {
        scope.db.parent.RLock()
        isSingularTable = scope.db.parent.singularTable
        scope.db.parent.RUnlock()
    }

    hashKey := struct {
        singularTable bool
        reflectType   reflect.Type
    }{isSingularTable, reflectType}
    if value, ok := modelStructsMap.Load(hashKey); ok && value != nil {
        return value.(*ModelStruct)
    }

  modelStruct.ModelType = reflectType

  // Get all fields
    for i := 0; i < reflectType.NumField(); i++ {
    ... // 折疊先不看
  }

  if len(modelStruct.PrimaryFields) == 0 {
        if field := getForeignField("id", modelStruct.StructFields); field != nil {
            field.IsPrimaryKey = true
            modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
        }
    }

    modelStructsMap.Store(hashKey, &modelStruct)

  return &modelStruct
}

開頭初始化了 var modelStruct ModelStruct, 這也是最后要返回的結(jié)果.

一開始先判斷了 scope.Value 不能為空, 否則就直接返回.

然后解析了 scope.Value 的具體類型, 對于切片或指針, 要看具

reflectType := reflect.ValueOf(scope.Value).Type()
for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
  reflectType = reflectType.Elem()
}

如果 scope.Value 的具體類型不是 Struct, 也是直接返回.

然后, 判斷是否有 ModelStruct 的緩存:

// Get Cached model struct
isSingularTable := false
if scope.db != nil && scope.db.parent != nil {
  scope.db.parent.RLock()
  isSingularTable = scope.db.parent.singularTable
  scope.db.parent.RUnlock()
}

hashKey := struct {
  singularTable bool
  reflectType   reflect.Type
}{isSingularTable, reflectType}
if value, ok := modelStructsMap.Load(hashKey); ok && value != nil {
  return value.(*ModelStruct)
}

modelStructsMap 是定義在外部的, 用于共享緩存.

var modelStructsMap sync.Map

如果可以從 modelStructsMap 找到, 就可以直接返回緩存.

modelStruct.ModelType = reflectType

略過 Get all fields 部分, 直接看后面的部分.

if len(modelStruct.PrimaryFields) == 0 {
  if field := getForeignField("id", modelStruct.StructFields); field != nil {
    field.IsPrimaryKey = true
    modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
  }
}

如果沒有找到主鍵, 就會把 ID 作為主鍵.

modelStructsMap.Store(hashKey, &modelStruct)

return &modelStruct

將解析好的結(jié)果保存到 modelStructsMap, 作為緩存, 加快后面解析的過程. 最后返回結(jié)果.

現(xiàn)在, 已經(jīng)將整個解析流程看完了, 除了獲取字段的過程不清楚, 其他都應(yīng)該清楚了.

解析的過程中用到了緩存, 也是我們可以借鑒的地方, sync.Map 可以安全地用于 goroutine 中共享.
另一點是將結(jié)構(gòu)體作為 key, 同時兼顧了單數(shù)形式的表名和復數(shù)形式的表名.

字段解析

前面的過程中省略了解析字段的過程, 這是非常重要的一部分. GetModelStruct 方法的大部分的代碼都集中在這一部分中.

for i := 0; i < reflectType.NumField(); i++ {

reflectType.NumField() 可以獲取結(jié)構(gòu)體中的字段總數(shù).

if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {

只解析可以導出的字段. 使用 reflectType.Field(i) 和索引 i, 可以獲取到結(jié)構(gòu)體中的字段.

field := &StructField{
  Struct:      fieldStruct,
  Name:        fieldStruct.Name,
  Names:       []string{fieldStruct.Name},
  Tag:         fieldStruct.Tag,
  TagSettings: parseTagSetting(fieldStruct.Tag),
}

StructField 初始化, 可以看到很多信息都是從 fieldStruct 中獲取的.

這一部分對于學習如何解析結(jié)構(gòu)體中的 Tag 非常有幫助, 仔細看一下.

fieldStruct.Tag 可以獲取字段中的 tag 部分, 比如:

type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}

ID 字段中的 gorm:"primary_key" 部分.

fieldStruct.Name 可以獲取字段的名字.

看一下具體是如何解析 Tag 字符串的.

func parseTagSetting(tags reflect.StructTag) map[string]string {
    setting := map[string]string{}
    for _, str := range []string{tags.Get("sql"), tags.Get("gorm")} {
        if str == "" {
            continue
        }
        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
}

tags 的類型是 reflect.StructTag, 包含一些實用的方法, 比如 Get 方法可以獲取特定的部分.
這里獲取了 sqlgorm 部分.

每個 tag 部分中, 都是使用 ; 分隔的選項. 每個選項又可能是 key/value 類型的, 由 : 分隔,
也可能是一個單獨的值.

具體看一個例子:

type User struct {
  gorm.Model
  Name         string
  Age          sql.NullInt64
  Birthday     *time.Time
  Email        string  `gorm:"type:varchar(100);unique_index"`
  Role         string  `gorm:"size:255"` // 設(shè)置字段大小為255
  MemberNumber *string `gorm:"unique;not null"` // 設(shè)置會員號(member number)唯一并且不為空
  Num          int     `gorm:"AUTO_INCREMENT"` // 設(shè)置 num 為自增類型
  Address      string  `gorm:"index:addr"` // 給address字段創(chuàng)建名為addr的索引
  IgnoreMe     int     `gorm:"-"` // 忽略本字段
}

比如 Email 字段的 tags 中 gorm 部分有兩個選項, 一個是 type:varchar(100), 另一個是 unique_index.

結(jié)合上面的 parseTagSetting 代碼, 我們知道這個字段的 tags 是如何被解析的了.

對于導出的字段, 也有辦法設(shè)置忽略該字段, 設(shè)置選項為 - 就行了.

// is ignored field
if _, ok := field.TagSettingsGet("-"); ok {
  field.IsIgnored = true
}

然后就是解析每一個選項了. 主要的代碼都在這里, 一點點看:

if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok {
  field.IsPrimaryKey = true
  modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
}

if _, ok := field.TagSettingsGet("DEFAULT"); ok && !field.IsPrimaryKey {
  field.HasDefaultValue = true
}

if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey {
  field.HasDefaultValue = true
}

設(shè)置 IsPrimaryKeyHasDefaultValue 屬性. 如果是主鍵的話, 還會添加到 PrimaryFields 中.

indirectType := fieldStruct.Type
for indirectType.Kind() == reflect.Ptr {
  indirectType = indirectType.Elem()
}

獲取字段的類型.

fieldValue := reflect.New(indirectType).Interface()

獲取字段對應(yīng)的值.

然后是根據(jù) fieldValue 的類型進行了一堆判斷, 一個個看.

  • 判斷一
if _, isScanner := fieldValue.(sql.Scanner); isScanner {
  // is scanner
  field.IsScanner, field.IsNormal = true, true
  if indirectType.Kind() == reflect.Struct {
    for i := 0; i < indirectType.NumField(); i++ {
      for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
        if _, ok := field.TagSettingsGet(key); !ok {
          field.TagSettingsSet(key, value)
        }
      }
    }
  }
}

如果實現(xiàn)了 sql.Scanner 接口, 設(shè)置了兩個屬性為 true.

  • 判斷二

如果該字段是結(jié)構(gòu)體, 將結(jié)構(gòu)體中的每個 tag 設(shè)置都添加一遍.

else if _, isTime := fieldValue.(*time.Time); isTime {
  // is time
  field.IsNormal = true
}

如果是 *time.Time 結(jié)構(gòu)體, 設(shè)置 IsNormal 為 true.

else if _, ok := field.TagSettingsGet("EMBEDDED"); ok || fieldStruct.Anonymous {
  // is embedded struct
  for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
    subField = subField.clone()
    subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
    if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
      subField.DBName = prefix + subField.DBName
    }

    if subField.IsPrimaryKey {
      if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
        modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
      } else {
        subField.IsPrimaryKey = false
      }
    }

    if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
      if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
        newJoinTableHandler := &JoinTableHandler{}
        newJoinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
        subField.Relationship.JoinTableHandler = newJoinTableHandler
      }
    }

    modelStruct.StructFields = append(modelStruct.StructFields, subField)
  }
  continue
}
  • 判斷三

如果 tag 設(shè)置中有 EMBEDDED 字段, 表示是一個嵌入的結(jié)構(gòu)體.

for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {

遍歷該字段對應(yīng)的 ModelStruct 中的每個 StructFields 中的 StructField.

subField = subField.clone() 直接在副本上操作.

subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
  subField.DBName = prefix + subField.DBName
}

重新設(shè)置 NamesDBName.

if subField.IsPrimaryKey {
  if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
    modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
  } else {
    subField.IsPrimaryKey = false
  }
}

如果 subField 是主鍵, 且有 PRIMARY_KEY tag 選項, 添加到 modelStruct.PrimaryFields 上去.
否則, 重置 subField.IsPrimaryKey 為 false.

if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
  if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
    newJoinTableHandler := &JoinTableHandler{}
    newJoinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
    subField.Relationship.JoinTableHandler = newJoinTableHandler
  }
}

初始化了 subField 中的 JoinTableHandler.

modelStruct.StructFields = append(modelStruct.StructFields, subField)

最后將 subField 添加到了 modelStruct.StructFields 中.

最后, 使用 continue 開始新的 for 循環(huán). 因此, field.TagSettingsGet("EMBEDDED") 部分也結(jié)束了.

  • 判斷四

如果上面的三個判斷都不滿足, 就進入了最后的 else 判斷了.

而這里面又是個 switch 判斷, 真的是憂傷.

根據(jù) switch indirectType.Kind() { 的類型, 主要是切片和結(jié)構(gòu)體, 先看 default 部分:

default:
  field.IsNormal = true
}

case reflect.Slice:case reflect.Struct: 里都是一個 defer 函數(shù).

case reflect.Slice:
  defer func(field *StructField) {
    var (
      relationship           = &Relationship{}
      toScope                = scope.New(reflect.New(field.Struct.Type).Interface())
      foreignKeys            []string
      associationForeignKeys []string
      elemType               = field.Struct.Type
    )

    if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
      foreignKeys = strings.Split(foreignKey, ",")
    }

    if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
      associationForeignKeys = strings.Split(foreignKey, ",")
    } else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
      associationForeignKeys = strings.Split(foreignKey, ",")
    }

    for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
      elemType = elemType.Elem()
    }

    if elemType.Kind() == reflect.Struct {
      if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" {
        relationship.Kind = "many_to_many"

        { // Foreign Keys for Source
          joinTableDBNames := []string{}

          if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
            joinTableDBNames = strings.Split(foreignKey, ",")
          }

          // if no foreign keys defined with tag
          if len(foreignKeys) == 0 {
            for _, field := range modelStruct.PrimaryFields {
              foreignKeys = append(foreignKeys, field.DBName)
            }
          }

          for idx, foreignKey := range foreignKeys {
            if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
              // source foreign keys (db names)
              relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.DBName)

              // setup join table foreign keys for source
              if len(joinTableDBNames) > idx {
                // if defined join table's foreign key
                relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx])
              } else {
                defaultJointableForeignKey := ToColumnName(reflectType.Name()) + "_" + foreignField.DBName
                relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey)
              }
            }
          }
        }

        { // Foreign Keys for Association (Destination)
          associationJoinTableDBNames := []string{}

          if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" {
            associationJoinTableDBNames = strings.Split(foreignKey, ",")
          }

          // if no association foreign keys defined with tag
          if len(associationForeignKeys) == 0 {
            for _, field := range toScope.PrimaryFields() {
              associationForeignKeys = append(associationForeignKeys, field.DBName)
            }
          }

          for idx, name := range associationForeignKeys {
            if field, ok := toScope.FieldByName(name); ok {
              // association foreign keys (db names)
              relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)

              // setup join table foreign keys for association
              if len(associationJoinTableDBNames) > idx {
                relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx])
              } else {
                // join table foreign keys for association
                joinTableDBName := ToColumnName(elemType.Name()) + "_" + field.DBName
                relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
              }
            }
          }
        }

        joinTableHandler := JoinTableHandler{}
        joinTableHandler.Setup(relationship, many2many, reflectType, elemType)
        relationship.JoinTableHandler = &joinTableHandler
        field.Relationship = relationship
      } else {
        // User has many comments, associationType is User, comment use UserID as foreign key
        var associationType = reflectType.Name()
        var toFields = toScope.GetStructFields()
        relationship.Kind = "has_many"

        if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
          // Dog has many toys, tag polymorphic is Owner, then associationType is Owner
          // Toy use OwnerID, OwnerType ('dogs') as foreign key
          if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
            associationType = polymorphic
            relationship.PolymorphicType = polymorphicType.Name
            relationship.PolymorphicDBName = polymorphicType.DBName
            // if Dog has multiple set of toys set name of the set (instead of default 'dogs')
            if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
              relationship.PolymorphicValue = value
            } else {
              relationship.PolymorphicValue = scope.TableName()
            }
            polymorphicType.IsForeignKey = true
          }
        }

        // if no foreign keys defined with tag
        if len(foreignKeys) == 0 {
          // if no association foreign keys defined with tag
          if len(associationForeignKeys) == 0 {
            for _, field := range modelStruct.PrimaryFields {
              foreignKeys = append(foreignKeys, associationType+field.Name)
              associationForeignKeys = append(associationForeignKeys, field.Name)
            }
          } else {
            // generate foreign keys from defined association foreign keys
            for _, scopeFieldName := range associationForeignKeys {
              if foreignField := getForeignField(scopeFieldName, modelStruct.StructFields); foreignField != nil {
                foreignKeys = append(foreignKeys, associationType+foreignField.Name)
                associationForeignKeys = append(associationForeignKeys, foreignField.Name)
              }
            }
          }
        } else {
          // generate association foreign keys from foreign keys
          if len(associationForeignKeys) == 0 {
            for _, foreignKey := range foreignKeys {
              if strings.HasPrefix(foreignKey, associationType) {
                associationForeignKey := strings.TrimPrefix(foreignKey, associationType)
                if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
                  associationForeignKeys = append(associationForeignKeys, associationForeignKey)
                }
              }
            }
            if len(associationForeignKeys) == 0 && len(foreignKeys) == 1 {
              associationForeignKeys = []string{scope.PrimaryKey()}
            }
          } else if len(foreignKeys) != len(associationForeignKeys) {
            scope.Err(errors.New("invalid foreign keys, should have same length"))
            return
          }
        }

        for idx, foreignKey := range foreignKeys {
          if foreignField := getForeignField(foreignKey, toFields); foreignField != nil {
            if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
              // source foreign keys
              foreignField.IsForeignKey = true
              relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
              relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)

              // association foreign keys
              relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
              relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
            }
          }
        }

        if len(relationship.ForeignFieldNames) != 0 {
          field.Relationship = relationship
        }
      }
    } else {
      field.IsNormal = true
    }
  }(field)

主要是處理了關(guān)系類型的 tag.

這一部分先跳過吧, 等具體研究關(guān)系的實現(xiàn), 再繼續(xù)深入.

等這整個 if 判斷都結(jié)束后, 解析一下列名, 最后將 fields 添加到 modelStruct.StructFields:

// Even it is ignored, also possible to decode db value into the field
if value, ok := field.TagSettingsGet("COLUMN"); ok {
  field.DBName = value
} else {
  field.DBName = ToColumnName(fieldStruct.Name)
}

modelStruct.StructFields = append(modelStruct.StructFields, field)

小結(jié)

所以, 整個模型解析的過程就是如此. 最耗時的部分在于解析每個字段, 解析 tag 以及字段間的關(guān)系.

總結(jié)

定義模型并解析模型的過程已經(jīng)看完了, 但關(guān)于模型還有很多內(nèi)容, 比如將模型轉(zhuǎn)換為表插入數(shù)據(jù)庫等.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末足删,一起剝皮案震驚了整個濱河市钙蒙,隨后出現(xiàn)的幾起案子髓窜,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,248評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件幻馁,死亡現(xiàn)場離奇詭異脓钾,居然都是意外死亡售睹,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評論 2 381
  • 文/潘曉璐 我一進店門可训,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昌妹,“玉大人,你說我怎么就攤上這事握截》裳拢” “怎么了?”我有些...
    開封第一講書人閱讀 153,443評論 0 344
  • 文/不壞的土叔 我叫張陵谨胞,是天一觀的道長固歪。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么牢裳? 我笑而不...
    開封第一講書人閱讀 55,475評論 1 279
  • 正文 為了忘掉前任逢防,我火速辦了婚禮,結(jié)果婚禮上蒲讯,老公的妹妹穿的比我還像新娘忘朝。我一直安慰自己,他們只是感情好判帮,可當我...
    茶點故事閱讀 64,458評論 5 374
  • 文/花漫 我一把揭開白布局嘁。 她就那樣靜靜地躺著,像睡著了一般晦墙。 火紅的嫁衣襯著肌膚如雪悦昵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,185評論 1 284
  • 那天偎痛,我揣著相機與錄音旱捧,去河邊找鬼。 笑死踩麦,一個胖子當著我的面吹牛枚赡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谓谦,決...
    沈念sama閱讀 38,451評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼贫橙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了反粥?” 一聲冷哼從身側(cè)響起卢肃,我...
    開封第一講書人閱讀 37,112評論 0 261
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎才顿,沒想到半個月后莫湘,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,609評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡郑气,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,083評論 2 325
  • 正文 我和宋清朗相戀三年幅垮,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尾组。...
    茶點故事閱讀 38,163評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡忙芒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出讳侨,到底是詐尸還是另有隱情呵萨,我是刑警寧澤,帶...
    沈念sama閱讀 33,803評論 4 323
  • 正文 年R本政府宣布跨跨,位于F島的核電站潮峦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜跑杭,卻給世界環(huán)境...
    茶點故事閱讀 39,357評論 3 307
  • 文/蒙蒙 一铆帽、第九天 我趴在偏房一處隱蔽的房頂上張望咆耿。 院中可真熱鬧德谅,春花似錦、人聲如沸萨螺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,357評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽慰技。三九已至椭盏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吻商,已是汗流浹背掏颊。 一陣腳步聲響...
    開封第一講書人閱讀 31,590評論 1 261
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留艾帐,地道東北人乌叶。 一個月前我還...
    沈念sama閱讀 45,636評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像柒爸,于是被迫代替她去往敵國和親准浴。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,925評論 2 344

推薦閱讀更多精彩內(nèi)容

  • 參考 理解reflect 通過reflect獲取tag值 gorm外鍵使用 gorm指定外鍵和關(guān)聯(lián)外鍵 目的 分析...
    不存在的里皮閱讀 2,178評論 0 0
  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當在唯一索引所對應(yīng)的列上鍵入重復值時捎稚,會觸發(fā)此異常乐横。 O...
    我想起個好名字閱讀 5,190評論 0 9
  • 很實用的編程英語詞庫,共收錄一千五百余條詞匯今野。 第一部分: application 應(yīng)用程式 應(yīng)用葡公、應(yīng)用程序app...
    春天的蜜蜂閱讀 1,338評論 0 22
  • Java基礎(chǔ)常見英語詞匯(共70個)['?bd?ekt] ['?:rientid]導向的 ...
    今夜子辰閱讀 3,265評論 1 34
  • 當同事第一次在辦公室當著我的面咳嗽,打噴嚏的時候条霜,我并沒有在意催什。 直到晚上回家,才覺得嗓子干疼蛔外,也懶得喝水蛆楞,早上起...
    紫墨zm閱讀 644評論 9 28