Skip to content

Commit

Permalink
generics model support
Browse files Browse the repository at this point in the history
  • Loading branch information
cubatic45 committed Apr 28, 2024
1 parent 7767207 commit 9d04526
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ type Table struct {
func newTable(typ reflect.Type) *Table {
t := new(Table)
t.Type = typ
name := t.Type.Name()
index := strings.Index(name, "[")
if index > 0 {
name = name[:index]
}
t.zeroStruct = reflect.New(t.Type).Elem()
t.TypeName = internal.ToExported(t.Type.Name())
t.ModelName = internal.Underscore(t.Type.Name())
t.TypeName = internal.ToExported(name)
t.ModelName = internal.Underscore(name)
tableName := tableNameInflector(t.ModelName)
t.setName(quoteIdent(tableName))
t.Alias = quoteIdent(t.ModelName)
Expand Down Expand Up @@ -292,7 +297,7 @@ func (t *Table) addFields(typ reflect.Type, baseIndex []int) {
}
}

//nolint
// nolint
func (t *Table) newField(f reflect.StructField, index []int) *Field {
pgTag := tagparser.Parse(f.Tag.Get("pg"))

Expand Down Expand Up @@ -944,7 +949,7 @@ func (t *Table) mustM2MRelation(field *Field, pgTag *tagparser.Tag) bool {
return true
}

//nolint
// nolint
func (t *Table) tryRelationSlice(field *Field, pgTag *tagparser.Tag) bool {
if t.tryM2MRelation(field, pgTag) {
internal.Deprecated.Printf(
Expand Down
17 changes: 17 additions & 0 deletions orm/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ var _ = Describe("embedding with ignored field", func() {
})
})

type P[T any] struct {
Data T
}

var _ = Describe("generics model", func() {
var table *orm.Table

BeforeEach(func() {
strct := reflect.ValueOf(P[string]{})
table = orm.GetTable(strct.Type())
})
It("TypeName and ModelName", func() {
Expect(table.TypeName).To(BeEquivalentTo("P"))
Expect(table.ModelName).To(BeEquivalentTo("p"))
})
})

type Nameless struct {
tableName struct{} `pg:"_"`

Expand Down

0 comments on commit 9d04526

Please sign in to comment.