diff options
Diffstat (limited to 'vendor/github.com/uptrace/bun/schema/table.go')
| -rw-r--r-- | vendor/github.com/uptrace/bun/schema/table.go | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/table.go b/vendor/github.com/uptrace/bun/schema/table.go index 13b989e4d..cf9f49197 100644 --- a/vendor/github.com/uptrace/bun/schema/table.go +++ b/vendor/github.com/uptrace/bun/schema/table.go @@ -62,8 +62,9 @@ type Table struct { FieldMap map[string]*Field StructMap map[string]*structField - Relations map[string]*Relation - Unique map[string][]*Field + IsM2MTable bool // If true, this table is the "junction table" of an m2m relation. + Relations map[string]*Relation + Unique map[string][]*Field SoftDeleteField *Field UpdateSoftDeleteField func(fv reflect.Value, tm time.Time) error @@ -122,6 +123,7 @@ func (t *Table) processFields(typ reflect.Type) { names := make(map[string]struct{}) embedded := make([]embeddedField, 0, 10) + ebdStructs := make(map[string]*structField, 0) for i, n := 0, typ.NumField(); i < n; i++ { sf := typ.Field(i) @@ -163,6 +165,17 @@ func (t *Table) processFields(typ reflect.Type) { subfield: subfield, }) } + if len(subtable.StructMap) > 0 { + for k, v := range subtable.StructMap { + // NOTE: conflict Struct name + if _, ok := ebdStructs[k]; !ok { + ebdStructs[k] = &structField{ + Index: makeIndex(sf.Index, v.Index), + Table: subtable, + } + } + } + } if tagstr != "" { tag := tagparser.Parse(tagstr) @@ -197,6 +210,18 @@ func (t *Table) processFields(typ reflect.Type) { subfield: subfield, }) } + if len(subtable.StructMap) > 0 { + for k, v := range subtable.StructMap { + // NOTE: conflict Struct name + k = prefix + k + if _, ok := ebdStructs[k]; !ok { + ebdStructs[k] = &structField{ + Index: makeIndex(sf.Index, v.Index), + Table: subtable, + } + } + } + } continue } @@ -252,6 +277,15 @@ func (t *Table) processFields(typ reflect.Type) { } } + if len(ebdStructs) > 0 && t.StructMap == nil { + t.StructMap = make(map[string]*structField) + } + for name, sfield := range ebdStructs { + if _, ok := t.StructMap[name]; !ok { + t.StructMap[name] = sfield + } + } + if len(embedded) > 0 { // https://github.com/uptrace/bun/issues/1095 // < v1.2, all fields follow the order corresponding to the struct @@ -483,6 +517,7 @@ func (t *Table) newField(sf reflect.StructField, tag tagparser.Tag) *Field { } field := &Field{ + Table: t, StructField: sf, IsPtr: sf.Type.Kind() == reflect.Ptr, @@ -862,6 +897,7 @@ func (t *Table) m2mRelation(field *Field) *Relation { JoinTable: joinTable, M2MTable: m2mTable, } + m2mTable.markM2M() if field.Tag.HasOption("join_on") { rel.Condition = field.Tag.Options["join_on"] @@ -907,6 +943,10 @@ func (t *Table) m2mRelation(field *Field) *Relation { return rel } +func (t *Table) markM2M() { + t.IsM2MTable = true +} + //------------------------------------------------------------------------------ func (t *Table) Dialect() Dialect { return t.dialect } |
