mirror of https://github.com/go-gorm/gorm.git
Add namer when generate schema
This commit is contained in:
parent
bc68fde6aa
commit
010dc7e6dd
|
@ -177,7 +177,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
|||
}
|
||||
|
||||
if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||
field.EmbeddedbSchema = Parse(fieldValue, sync.Map{})
|
||||
field.EmbeddedbSchema = Parse(fieldValue, sync.Map{}, schema.namer)
|
||||
for _, ef := range field.EmbeddedbSchema.Fields {
|
||||
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...)
|
||||
|
||||
|
|
|
@ -16,10 +16,11 @@ type Schema struct {
|
|||
FieldsByName map[string]*Field
|
||||
FieldsByDBName map[string]*Field
|
||||
Relationships Relationships
|
||||
namer Namer
|
||||
}
|
||||
|
||||
// get data type from dialector
|
||||
func Parse(dest interface{}, cacheStore sync.Map) *Schema {
|
||||
func Parse(dest interface{}, cacheStore sync.Map, namer Namer) *Schema {
|
||||
modelType := reflect.ValueOf(dest).Type()
|
||||
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr {
|
||||
modelType = modelType.Elem()
|
||||
|
@ -35,6 +36,7 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema {
|
|||
|
||||
schema := &Schema{
|
||||
ModelType: modelType,
|
||||
Table: namer.TableName(modelType.Name()),
|
||||
FieldsByName: map[string]*Field{},
|
||||
FieldsByDBName: map[string]*Field{},
|
||||
}
|
||||
|
@ -45,14 +47,23 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema {
|
|||
continue
|
||||
}
|
||||
|
||||
schema.Fields = append(schema.Fields, schema.ParseField(fieldStruct))
|
||||
// db namer
|
||||
field := schema.ParseField(fieldStruct)
|
||||
schema.Fields = append(schema.Fields, field)
|
||||
if field.EmbeddedbSchema != nil {
|
||||
for _, f := range field.EmbeddedbSchema.Fields {
|
||||
schema.Fields = append(schema.Fields, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, field := range schema.Fields {
|
||||
if field.DBName == "" {
|
||||
field.DBName = namer.ColumnName(field.Name)
|
||||
}
|
||||
|
||||
if field.DBName != "" {
|
||||
// nonexistence or shortest path or first appear prioritized
|
||||
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || len(field.BindNames) < len(v.BindNames) {
|
||||
// nonexistence or shortest path or first appear prioritized if has permission
|
||||
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || (field.Creatable && len(field.BindNames) < len(v.BindNames)) {
|
||||
schema.FieldsByDBName[field.DBName] = field
|
||||
schema.FieldsByName[field.Name] = field
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue