Skip to content

Commit

Permalink
Add mapping for remaining tags
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMacies committed Jul 12, 2024
1 parent 82bbce3 commit 4ef407c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (commonDialect) Quote(key string) string {
func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool {
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
return strings.ToLower(value) != "false"
} else if value, ok := field.TagSettingsGet("AUTOINCREMENT"); ok {
return strings.ToLower(value) != "false"
}
return field.IsPrimaryKey
}
Expand Down
25 changes: 24 additions & 1 deletion model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
associationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
associationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("REFERENCES"); foreignKey != "" {
associationForeignKeys = strings.Split(foreignKey, ",")
}

for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
Expand All @@ -318,8 +320,11 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
{ // Foreign Keys for Source
joinTableDBNames := []string{}

// Compatibility with GORM v2
if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
joinTableDBNames = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("JOINFOREIGNKEY"); foreignKey != "" {
joinTableDBNames = strings.Split(foreignKey, ",")
}

// if no foreign keys defined with tag
Expand Down Expand Up @@ -351,6 +356,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *

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

// if no association foreign keys defined with tag
Expand Down Expand Up @@ -395,7 +402,21 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
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 {

// Compatibility with GORM v2

var value string
var ok bool

if v1, ok1 := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok1 {
value = v1
ok = ok1
} else if v2, ok2 := field.TagSettingsGet("POLYMORPHICVALUE"); ok2 {
value = v2
ok = ok2
}

if ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
Expand Down Expand Up @@ -489,6 +510,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("REFERENCES"); foreignKey != "" {
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
}

if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
Expand Down

0 comments on commit 4ef407c

Please sign in to comment.