forked from mirror/gorm
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 {
|
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 {
|
for _, ef := range field.EmbeddedbSchema.Fields {
|
||||||
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...)
|
ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...)
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,11 @@ type Schema struct {
|
||||||
FieldsByName map[string]*Field
|
FieldsByName map[string]*Field
|
||||||
FieldsByDBName map[string]*Field
|
FieldsByDBName map[string]*Field
|
||||||
Relationships Relationships
|
Relationships Relationships
|
||||||
|
namer Namer
|
||||||
}
|
}
|
||||||
|
|
||||||
// get data type from dialector
|
// 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()
|
modelType := reflect.ValueOf(dest).Type()
|
||||||
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr {
|
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr {
|
||||||
modelType = modelType.Elem()
|
modelType = modelType.Elem()
|
||||||
|
@ -35,6 +36,7 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema {
|
||||||
|
|
||||||
schema := &Schema{
|
schema := &Schema{
|
||||||
ModelType: modelType,
|
ModelType: modelType,
|
||||||
|
Table: namer.TableName(modelType.Name()),
|
||||||
FieldsByName: map[string]*Field{},
|
FieldsByName: map[string]*Field{},
|
||||||
FieldsByDBName: map[string]*Field{},
|
FieldsByDBName: map[string]*Field{},
|
||||||
}
|
}
|
||||||
|
@ -45,14 +47,23 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
schema.Fields = append(schema.Fields, schema.ParseField(fieldStruct))
|
field := schema.ParseField(fieldStruct)
|
||||||
// db namer
|
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 {
|
for _, field := range schema.Fields {
|
||||||
|
if field.DBName == "" {
|
||||||
|
field.DBName = namer.ColumnName(field.Name)
|
||||||
|
}
|
||||||
|
|
||||||
if field.DBName != "" {
|
if field.DBName != "" {
|
||||||
// nonexistence or shortest path or first appear prioritized
|
// nonexistence or shortest path or first appear prioritized if has permission
|
||||||
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || len(field.BindNames) < len(v.BindNames) {
|
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || (field.Creatable && len(field.BindNames) < len(v.BindNames)) {
|
||||||
schema.FieldsByDBName[field.DBName] = field
|
schema.FieldsByDBName[field.DBName] = field
|
||||||
schema.FieldsByName[field.Name] = field
|
schema.FieldsByName[field.Name] = field
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue