Skip to content

Commit 097f4ea

Browse files
Fix: support tag bun for comments building
1 parent 8180aca commit 097f4ea

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

database/comment.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
3131
for i := 0; i < modelType.NumField(); i++ {
3232
fieldType := modelType.Field(i)
3333

34-
if fieldType.Name == "tableName" {
34+
if fieldType.Name == "tableName" || fieldType.Name == "BaseModel" {
3535
var ok bool
36-
tableName, ok = getPgName(fieldType)
36+
tableName, ok = getDatabaseTagName(fieldType)
3737
if !ok {
3838
tableName = hasura.ToSnakeCase(modelType.Name())
3939
}
@@ -95,7 +95,7 @@ func makeFieldComment(ctx context.Context, sc SchemeCommenter, tableName string,
9595
return nil
9696
}
9797

98-
columnName, ok := getPgName(fieldType)
98+
columnName, ok := getDatabaseTagName(fieldType)
9999

100100
if columnName == "-" {
101101
return nil
@@ -112,13 +112,26 @@ func makeFieldComment(ctx context.Context, sc SchemeCommenter, tableName string,
112112
return nil
113113
}
114114

115-
func getPgName(fieldType reflect.StructField) (name string, ok bool) {
116-
pgTag, ok := fieldType.Tag.Lookup("pg")
117-
if !ok {
115+
func getDatabaseTagName(fieldType reflect.StructField) (name string, ok bool) {
116+
pgTag, pgOk := fieldType.Tag.Lookup("pg")
117+
bunTag, bunOk := fieldType.Tag.Lookup("bun")
118+
ok = pgOk || bunOk
119+
120+
var tag string
121+
switch {
122+
case !pgOk && !bunOk:
118123
return "", false
124+
case pgOk && pgTag != "-":
125+
tag = pgTag
126+
case bunOk && bunTag != "-":
127+
tag = strings.TrimPrefix(bunTag, "table:")
128+
case pgOk:
129+
tag = pgTag
130+
case bunOk:
131+
tag = bunTag
119132
}
120133

121-
tags := strings.Split(pgTag, ",")
134+
tags := strings.Split(tag, ",")
122135

123136
if tags[0] != "" {
124137
name = tags[0]

database/comment_test.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package database
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/dipdup-net/go-lib/mocks"
68
"github.com/golang/mock/gomock"
7-
"github.com/stretchr/testify/assert"
8-
"testing"
9+
"github.com/stretchr/testify/require"
10+
"github.com/uptrace/bun"
911
)
1012

1113
func initMocks(t *testing.T) (*gomock.Controller, *mocks.MockSchemeCommenter, context.Context) {
@@ -39,7 +41,7 @@ func TestMakeCommentsWithTableName(t *testing.T) {
3941
err := MakeComments(ctx, mockSC, model)
4042

4143
// Assert
42-
assert.Empty(t, err)
44+
require.NoError(t, err)
4345
}
4446

4547
func TestMakeCommentsWithoutPgComment(t *testing.T) {
@@ -69,7 +71,7 @@ func TestMakeCommentsWithoutPgComment(t *testing.T) {
6971
err := MakeComments(ctx, mockSC, model)
7072

7173
// Assert
72-
assert.Empty(t, err)
74+
require.NoError(t, err)
7375
}
7476

7577
func TestMakeCommentsFieldWithPgComment(t *testing.T) {
@@ -95,7 +97,7 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
9597
err := MakeComments(ctx, mockSC, model)
9698

9799
// Assert
98-
assert.Empty(t, err)
100+
require.NoError(t, err)
99101
}
100102

101103
func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
@@ -142,7 +144,7 @@ func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
142144
err := MakeComments(ctx, mockSC, model)
143145

144146
// Assert
145-
assert.Empty(t, err)
147+
require.NoError(t, err)
146148
}
147149

148150
func TestMakeCommentsWithMultipleModels(t *testing.T) {
@@ -176,7 +178,7 @@ func TestMakeCommentsWithMultipleModels(t *testing.T) {
176178
err := MakeComments(ctx, mockSC, models...)
177179

178180
// Assert
179-
assert.Empty(t, err)
181+
require.NoError(t, err)
180182
}
181183

182184
func TestMakeCommentsWithMultipleModelsByPointers(t *testing.T) {
@@ -210,7 +212,7 @@ func TestMakeCommentsWithMultipleModelsByPointers(t *testing.T) {
210212
err := MakeComments(ctx, mockSC, models...)
211213

212214
// Assert
213-
assert.Empty(t, err)
215+
require.NoError(t, err)
214216
}
215217

216218
func TestMakeCommentsIgnoreFieldWithPgHyphen(t *testing.T) {
@@ -235,7 +237,7 @@ func TestMakeCommentsIgnoreFieldWithPgHyphen(t *testing.T) {
235237
err := MakeComments(ctx, mockSC, model)
236238

237239
// Assert
238-
assert.Empty(t, err)
240+
require.NoError(t, err)
239241
}
240242

241243
func TestMakeCommentsIgnoreFieldsWithEmptyComment(t *testing.T) {
@@ -260,7 +262,7 @@ func TestMakeCommentsIgnoreFieldsWithEmptyComment(t *testing.T) {
260262
err := MakeComments(ctx, mockSC, model)
261263

262264
// Assert
263-
assert.Empty(t, err)
265+
require.NoError(t, err)
264266
}
265267

266268
func TestMakeCommentsIgnoreNilModel(t *testing.T) {
@@ -277,7 +279,7 @@ func TestMakeCommentsIgnoreNilModel(t *testing.T) {
277279
err := MakeComments(ctx, mockSC, nil)
278280

279281
// Assert
280-
assert.Empty(t, err)
282+
require.NoError(t, err)
281283
}
282284

283285
func TestMakeCommentsIgnoreNoModels(t *testing.T) {
@@ -294,7 +296,7 @@ func TestMakeCommentsIgnoreNoModels(t *testing.T) {
294296
err := MakeComments(ctx, mockSC)
295297

296298
// Assert
297-
assert.Empty(t, err)
299+
require.NoError(t, err)
298300
}
299301

300302
func TestMakeCommentsWithStructCompositionAndCorrectOrder(t *testing.T) {
@@ -355,7 +357,7 @@ func TestMakeCommentsWithStructCompositionAndCorrectOrder(t *testing.T) {
355357
err := MakeComments(ctx, mockSC, model)
356358

357359
// Assert
358-
assert.Empty(t, err)
360+
require.NoError(t, err)
359361
}
360362

361363
func TestMakeCommentsWithDeepStructComposition(t *testing.T) {
@@ -424,7 +426,7 @@ func TestMakeCommentsWithDeepStructComposition(t *testing.T) {
424426
err := MakeComments(ctx, mockSC, model)
425427

426428
// Assert
427-
assert.Empty(t, err)
429+
require.NoError(t, err)
428430
}
429431

430432
func TestMakeCommentsWithStructCompositionErrorOnEmbeddedTableName(t *testing.T) {
@@ -459,5 +461,51 @@ func TestMakeCommentsWithStructCompositionErrorOnEmbeddedTableName(t *testing.T)
459461
err := MakeComments(ctx, mockSC, model)
460462

461463
// Assert
462-
assert.Error(t, err, "Embedded type must not have tableName field.")
464+
require.Error(t, err, "Embedded type must not have tableName field.")
465+
}
466+
467+
func TestMakeCommentsWithBunBaseModel(t *testing.T) {
468+
type Operation struct {
469+
bun.BaseModel `pg:"-" bun:"table:operation" comment:"This is bun comment."`
470+
CreatedAt int64 `json:"-" comment:"Date of creation in seconds since UNIX epoch."`
471+
UpdatedAt int64 `json:"-" comment:"Date of last update in seconds since UNIX epoch."`
472+
Network string `json:"network" bun:",pk" comment:"Identifies belonging network."`
473+
}
474+
475+
mockCtrl, mockSC, ctx := initMocks(t)
476+
defer mockCtrl.Finish()
477+
478+
// Assert prepare
479+
tableNameCall := mockSC.
480+
EXPECT().
481+
MakeTableComment(ctx, "operation", "This is bun comment.").
482+
Times(1).
483+
Return(nil)
484+
485+
createdAtCall := mockSC.
486+
EXPECT().
487+
MakeColumnComment(ctx, "operation", "created_at", "Date of creation in seconds since UNIX epoch.").
488+
After(tableNameCall).
489+
Times(1).
490+
Return(nil)
491+
492+
updatedAtCall := mockSC.
493+
EXPECT().
494+
MakeColumnComment(ctx, "operation", "updated_at", "Date of last update in seconds since UNIX epoch.").
495+
After(createdAtCall).
496+
Times(1).
497+
Return(nil)
498+
499+
mockSC.
500+
EXPECT().
501+
MakeColumnComment(ctx, "operation", "network", "Identifies belonging network.").
502+
After(updatedAtCall).
503+
Times(1).
504+
Return(nil)
505+
506+
// Act
507+
err := MakeComments(ctx, mockSC, Operation{})
508+
509+
// Assert
510+
require.NoError(t, err, "Bun model comments was failed")
463511
}

database/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
// State -
1111
type State struct {
1212
//nolint
13-
tableName struct{} `gorm:"-" pg:"dipdup_state" json:"-" comment:"Indexer state table"`
14-
bun.BaseModel `bun:"table:dipdup_state"`
13+
tableName struct{} `gorm:"-" bun:"-" pg:"dipdup_state" json:"-" comment:"Indexer state table"`
14+
bun.BaseModel `gorm:"-" pg:"-" bun:"table:dipdup_state" json:"-" comment:"Indexer state table"`
1515

1616
IndexName string `gorm:"primaryKey" pg:",pk" json:"index_name" comment:"Index name"`
1717
IndexType string `json:"index_type" comment:"Index type"`

0 commit comments

Comments
 (0)