func loadTableSetting(mType reflect.Type) *TableInformation { tInfo := &TableInformation{ TableName: string551.SnakeCase(mType.Name()), PrimaryKey: "id", Fields: []string{}, DeleteTable: false, DeletedAtField: "", } if name := loadTableName(mType); name != "" { tInfo.TableName = name } if primaryKey := loadPrimaryKey(mType); primaryKey != "" { tInfo.PrimaryKey = primaryKey } if del, name := loadDeleteAt(mType); del { tInfo.DeleteTable = true tInfo.DeletedAtField = name } tInfo.Fields = loadFields(mType) return tInfo }
func loadPrimaryKey(mType reflect.Type) string { for i := 0; i < mType.NumField(); i++ { sField := mType.Field(i) pk, err := strconv.ParseBool(sField.Tag.Get("db_pk")) if err == nil && pk { db := sField.Tag.Get("db") if db == "" { return string551.SnakeCase(sField.Name) } else { return string551.SnakeCase(db) } } } return "" }
func loadFields(mType reflect.Type) []string { fields := make([]string, 0) for i := 0; i < mType.NumField(); i++ { sField := mType.Field(i) db := sField.Tag.Get("db") del, err := strconv.ParseBool(sField.Tag.Get("db_delete")) if err != nil || !del { if db == "" { fields = append(fields, string551.SnakeCase(sField.Name)) } else if db != "-" { fields = append(fields, string551.SnakeCase(db)) } } } return fields }
func loadDeleteAt(mType reflect.Type) (bool, string) { for i := 0; i < mType.NumField(); i++ { sField := mType.Field(i) del, err := strconv.ParseBool(sField.Tag.Get("db_delete")) if err == nil && del { return true, string551.SnakeCase(sField.Name) } } return false, "" }