diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 607ccf5..c218644 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -33,27 +33,11 @@ jobs: - name: Check out repository code uses: actions/checkout@v4 - - name: Decide if tests should run - id: set_run_tests - run: | - if [[ ("$GITHUB_EVENT_NAME" == "push" && ( "$GITHUB_HEAD_COMMIT_MESSAGE" == fix:* || "$GITHUB_HEAD_COMMIT_MESSAGE" == feat:* || "$GITHUB_HEAD_COMMIT_MESSAGE" == refactor:* || "$GITHUB_HEAD_COMMIT_MESSAGE" == perf:* )) || \ - ("$GITHUB_EVENT_NAME" == "pull_request" && ( "$GITHUB_PULL_REQUEST_TITLE" == fix:* || "$GITHUB_PULL_REQUEST_TITLE" == feat:* || "$GITHUB_PULL_REQUEST_TITLE" == refactor:* || "$GITHUB_PULL_REQUEST_TITLE" == perf:* )) ]]; then - echo "should_run_tests=true" >> $GITHUB_OUTPUT - else - echo "should_run_tests=false" >> $GITHUB_OUTPUT - fi - env: - GITHUB_HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }} - GITHUB_PULL_REQUEST_TITLE: ${{ github.event.pull_request.title }} - GITHUB_EVENT_NAME: ${{ github.event_name }} - - name: Test with coverage - if: steps.set_run_tests.outputs.should_run_tests == 'true' run: | go test -cover -coverprofile=coverage.txt ./... - name: Upload coverage reports to Codecov - if: steps.set_run_tests.outputs.should_run_tests == 'true' uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/module.go b/module.go index 9e7971f..101790b 100644 --- a/module.go +++ b/module.go @@ -3,6 +3,7 @@ package sqlorm import ( "fmt" "time" + "reflect" "github.com/tinh-tinh/tinhtinh/v2/common" "github.com/tinh-tinh/tinhtinh/v2/core" @@ -68,7 +69,16 @@ func Inject(ref core.RefProvider) *gorm.DB { func InjectRepository[M any](ref core.RefProvider) *Repository[M] { var model M - modelName := core.Provide(fmt.Sprintf("%sRepo", common.GetStructName(model))) + + ctModel := reflect.ValueOf(&model).Elem() + fnc := ctModel.MethodByName("RepositoryName") + var name string + if fnc.IsValid() { + name = fnc.Call(nil)[0].String() + } else { + name = common.GetStructName(model) + } + modelName := core.Provide(GetRepoName(name)) data, ok := ref.Ref(modelName).(*Repository[M]) if !ok { return nil diff --git a/module_test.go b/module_test.go index b568020..18f1918 100644 --- a/module_test.go +++ b/module_test.go @@ -246,6 +246,29 @@ func Test_Nil(t *testing.T) { require.Nil(t, sqlorm.InjectRepository[User](core.NewModule(core.NewModuleOptions{}))) } +func Test_RefName(t *testing.T) { + require.NotPanics(t, func() { + createDatabaseForTest("test") + }) + dsn := "host=localhost user=postgres password=postgres dbname=test port=5432 sslmode=disable TimeZone=Asia/Shanghai" + + appModule := func() core.Module { + module := core.NewModule(core.NewModuleOptions{ + Imports: []core.Modules{ + sqlorm.ForRoot(sqlorm.Config{ + Dialect: postgres.Open(dsn), + Models: []interface{}{&Abc{}}, + }), + sqlorm.ForFeature(sqlorm.NewRepo(Abc{})), + }, + }) + + return module + } + abcRepo := sqlorm.InjectRepository[Abc](appModule()) + require.NotNil(t, abcRepo) +} + func createDatabaseForTest(dbName string) { connStr := "host=localhost user=postgres password=postgres port=5432 dbname=postgres sslmode=disable TimeZone=Asia/Shanghai" diff --git a/repository.go b/repository.go index 9b6e17d..8fc51b2 100644 --- a/repository.go +++ b/repository.go @@ -23,7 +23,16 @@ type Repository[M any] struct { func (r *Repository[M]) GetName() string { var model M - return common.GetStructName(model) + + ctModel := reflect.ValueOf(&model).Elem() + fnc := ctModel.MethodByName("RepositoryName") + var name string + if fnc.IsValid() { + name = fnc.Call(nil)[0].String() + } else { + name = common.GetStructName(model) + } + return name } func (r *Repository[M]) SetDB(db *gorm.DB) { diff --git a/repository_test.go b/repository_test.go index 81c37c9..67be688 100644 --- a/repository_test.go +++ b/repository_test.go @@ -69,3 +69,17 @@ func Test_Map(t *testing.T) { list4 := sqlorm.MapMany[Todo](nil) require.Equal(t, 0, len(list4)) } + +type Abc struct { + sqlorm.Model `gorm:"embedded"` + Name string `gorm:"type:varchar(255);not null"` +} + +func (Abc) RepositoryName() string { + return "service" +} + +func TestNameRepo(t *testing.T) { + repo := sqlorm.NewRepo(Abc{}) + require.Equal(t, "service", repo.GetName()) +}