Skip to content

Commit

Permalink
fix: removed di register functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Jan 20, 2025
1 parent 00644ca commit 98a12c3
Show file tree
Hide file tree
Showing 40 changed files with 171 additions and 393 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
args: ["--autofix", "--no-ensure-ascii", "--no-sort-keys"]
- id: trailing-whitespace
- repo: https://github.com/crate-ci/typos
rev: v1.28.1
rev: v1.29.4
hooks:
- id: typos
verbose: true
Expand All @@ -41,13 +41,13 @@ repos:
go.mod
)$
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.6.0
rev: v4.0.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: []
- repo: https://github.com/golangci/golangci-lint
rev: v1.62.2
rev: v1.63.4
hooks:
- id: golangci-lint
- repo: local
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pre-commit 3.8.0
golang 1.23.4
golang 1.23.5
21 changes: 13 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ init: dep-tools
command -v protoc-gen-go-grpc >/dev/null || go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
command -v stringer >/dev/null || go install golang.org/x/tools/cmd/stringer@latest

.PHONY: dev-samplesvc
dev-samplesvc:
.PHONY: sample-dev
sample-dev:
air --build.bin "./tmp/samplesvc" --build.cmd "go build -o ./tmp/samplesvc ./cmd/samplesvc/"

.PHONY: run-samplesvc
run-samplesvc:
.PHONY: sample-run
sample-run:
go run ./cmd/samplesvc/

.PHONY: migrate
Expand All @@ -60,7 +60,7 @@ test:
.PHONY: test-cov
test-cov:
go test -failfast -race -count 1 -coverpkg=./... -coverprofile=${TMPDIR}cov_profile.out ./...
# `go env GOPATH`/bin/gcov2lcov -infile ${TMPDIR}cov_profile.out -outfile ./cov_profile.lcov
# gcov2lcov -infile ${TMPDIR}cov_profile.out -outfile ./cov_profile.lcov

.PHONY: test-view-html
test-view-html:
Expand All @@ -85,12 +85,17 @@ test-ci: test-cov

.PHONY: lint
lint:
`go env GOPATH`/bin/golangci-lint run ./...
golangci-lint run ./...

.PHONY: check
check:
`go env GOPATH`/bin/govulncheck ./...
`go env GOPATH`/bin/betteralign ./...
govulncheck ./...
betteralign ./...

.PHONY: fix
fix:
betteralign -apply ./...
go fmt ./...

.PHONY: postgres-start
postgres-start:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/eser/go-service

go 1.23.4
go 1.23.5

require (
github.com/getkin/kin-openapi v0.128.0
Expand Down
19 changes: 0 additions & 19 deletions pkg/bliss/configfx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@ package is designed to work seamlessly with the `bliss/di` package.
The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed
information, refer to the source code and tests.

## Bliss DI

The `configfx` package provides a `RegisterDependencies` function that can be used to integrate with the `bliss/di`
package.

```go
import (
...
"github.com/eser/go-service/pkg/bliss/di"
"github.com/eser/go-service/pkg/bliss/configfx"
...
)

err := di.RegisterFn(
configfx.RegisterDependencies, // registers configfx.ConfigLoader
...
)
```

## API

### ConfigLoader interface
Expand Down
11 changes: 0 additions & 11 deletions pkg/bliss/configfx/configfx.go

This file was deleted.

23 changes: 0 additions & 23 deletions pkg/bliss/datafx/datafx.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/bliss/datafx/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type SqlExecutor interface {

type SqlDataSource interface {
GetConnection() SqlExecutor
UseUnitOfWork(ctx context.Context) (UnitOfWork, error)
UseUnitOfWork(ctx context.Context) (*UnitOfWork, error)
}

type Registry struct {
Expand Down
6 changes: 2 additions & 4 deletions pkg/bliss/datafx/sqldatasource-std.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ type SqlDataSourceStd struct {
connection *sql.DB
}

var _ SqlDataSource = (*SqlDataSourceStd)(nil)

func NewSqlDataSourceStd(ctx context.Context, dialect Dialect, dsn string) (*SqlDataSourceStd, error) {
connection, err := sql.Open(string(dialect), dsn)
if err != nil {
Expand All @@ -31,10 +29,10 @@ func (dataSource *SqlDataSourceStd) GetConnection() SqlExecutor { //nolint:iretu
return dataSource.connection
}

func (dataSource *SqlDataSourceStd) UseUnitOfWork(ctx context.Context) (UnitOfWork, error) { //nolint:ireturn
func (dataSource *SqlDataSourceStd) UseUnitOfWork(ctx context.Context) (*UnitOfWork, error) {
uow, err := UseUnitOfWork(ctx, dataSource.connection)
if err != nil {
return nil, err
return &UnitOfWork{}, err
}

return uow, err
Expand Down
34 changes: 12 additions & 22 deletions pkg/bliss/datafx/unit-of-work.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,26 @@ type TransactionFinalizer interface {
Commit() error
}

type UnitOfWork interface {
TxScope() TransactionFinalizer
Context() context.Context
Bind(context context.Context, txScope TransactionFinalizer)
Commit() error
Close() error
}

type UnitOfWorkImpl struct {
type UnitOfWork struct {
context context.Context //nolint:containedctx
txScope TransactionFinalizer
}

var _ UnitOfWork = (*UnitOfWorkImpl)(nil)

// func NewUnitOfWork() *UnitOfWorkImpl {
// return &UnitOfWorkImpl{}
// func NewUnitOfWork() *UnitOfWork {
// return &UnitOfWork{}
// }

type TransactionStarter interface {
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

func UseUnitOfWork(ctx context.Context, transactionStarter TransactionStarter) (UnitOfWork, error) { //nolint:ireturn,varnamelen,lll
uow, ok := ctx.Value(ContextKeyUnitOfWork).(UnitOfWork)
func UseUnitOfWork(ctx context.Context, transactionStarter TransactionStarter) (*UnitOfWork, error) {
uow, ok := ctx.Value(ContextKeyUnitOfWork).(*UnitOfWork)
if ok {
return uow, nil
}

uow = &UnitOfWorkImpl{} //nolint:exhaustruct
uow = &UnitOfWork{} //nolint:exhaustruct
newCtx := context.WithValue(ctx, ContextKeyUnitOfWork, uow)

transaction, err := transactionStarter.BeginTx(newCtx, nil)
Expand All @@ -59,27 +49,27 @@ func UseUnitOfWork(ctx context.Context, transactionStarter TransactionStarter) (
return uow, nil
}

func (uow *UnitOfWorkImpl) TxScope() TransactionFinalizer { //nolint:ireturn
func (uow *UnitOfWork) TxScope() TransactionFinalizer { //nolint:ireturn
return uow.txScope
}

func (uow *UnitOfWorkImpl) Context() context.Context {
func (uow *UnitOfWork) Context() context.Context {
return uow.context
}

func (uow *UnitOfWorkImpl) Bind(context context.Context, txScope TransactionFinalizer) {
func (uow *UnitOfWork) Bind(context context.Context, txScope TransactionFinalizer) {
uow.context = context
uow.txScope = txScope
}

func (uow *UnitOfWorkImpl) Commit() error {
func (uow *UnitOfWork) Commit() error {
return uow.txScope.Commit() //nolint:wrapcheck
}

func (uow *UnitOfWorkImpl) Close() error {
func (uow *UnitOfWork) Close() error {
return uow.txScope.Rollback() //nolint:wrapcheck
}

func (uow *UnitOfWorkImpl) Use(fn func(TransactionFinalizer) any) {
func (uow *UnitOfWork) Use(fn func(TransactionFinalizer) any) {
fn(uow.txScope)
}
Loading

0 comments on commit 98a12c3

Please sign in to comment.