Skip to content

Commit

Permalink
Remove error from constructor if never error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jun 13, 2024
1 parent 3b74b36 commit b3e3a1f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 133 deletions.
10 changes: 2 additions & 8 deletions pkg/datastore/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ func Initdb(log *slog.Logger, db *sqlx.DB, healthServer *health.Server, dir stri
return err
}

ts, err := New(log, db, &v1.Tenant{})
if err != nil {
return err
}
ts := New(log, db, &v1.Tenant{})
tbs := &bootstrap[*v1.Tenant]{
log: log,
ds: ts,
}

ps, err := New(log, db, &v1.Project{})
if err != nil {
return err
}
ps := New(log, db, &v1.Project{})
pbs := &bootstrap[*v1.Project]{
log: log,
ds: ps,
Expand Down
5 changes: 1 addition & 4 deletions pkg/datastore/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ func MigrateDB(log *slog.Logger, db *sqlx.DB, healthServer *health.Server) error
&migrator.Migration{
Name: "Sample Migration for Tenant",
Func: func(tx *sql.Tx) error {
ts, err := New(log, db, &v1.Tenant{})
if err != nil {
return err
}
ts := New(log, db, &v1.Tenant{})

tenants, _, err := ts.Find(context.Background(), nil, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/datastore/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewPostgresDB(logger *slog.Logger, host, port, user, password, dbname, sslm
}

// New creates a new Storage which uses the given database abstraction.
func New[E Entity](logger *slog.Logger, db *sqlx.DB, e E) (Storage[E], error) {
func New[E Entity](logger *slog.Logger, db *sqlx.DB, e E) Storage[E] {
ds := &datastore[E]{
log: logger,
db: db,
Expand All @@ -91,7 +91,7 @@ func New[E Entity](logger *slog.Logger, db *sqlx.DB, e E) (Storage[E], error) {
tableName: e.TableName(),
historyTableName: fmt.Sprintf("%s_history", e.TableName()),
}
return ds, nil
return ds
}

// Create a entity
Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/postgres_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

func init() {
_, db, _ = createPostgresConnection()
ds, _ = New(slog.Default(), db, &v1.Tenant{})
ds = New(slog.Default(), db, &v1.Tenant{})
}

func BenchmarkGetTenant(b *testing.B) {
Expand Down
63 changes: 25 additions & 38 deletions pkg/datastore/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func TestMain(m *testing.M) {
}

func TestCRUD(t *testing.T) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()
tcr := &v1.Tenant{
Expand All @@ -63,7 +62,7 @@ func TestCRUD(t *testing.T) {
Description: "A very important Tenant",
}

err = tenantDS.Create(ctx, tcr)
err := tenantDS.Create(ctx, tcr)
require.NoError(t, err)
assert.NotNil(t, tcr)
// specified id is persisted
Expand Down Expand Up @@ -134,8 +133,7 @@ func TestCRUD(t *testing.T) {
}

func TestUpdateOptimisticLock(t *testing.T) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()
tcr := &v1.Tenant{
Expand All @@ -144,7 +142,7 @@ func TestUpdateOptimisticLock(t *testing.T) {
Description: "A very important Tenant",
}

err = tenantDS.Create(ctx, tcr)
err := tenantDS.Create(ctx, tcr)
require.NoError(t, err)
assert.NotNil(t, tcr)
assert.Equal(t, int64(0), tcr.Meta.Version)
Expand Down Expand Up @@ -180,8 +178,7 @@ func TestUpdateOptimisticLock(t *testing.T) {

func TestCreate(t *testing.T) {
const t1 = "t1"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

Expand All @@ -191,7 +188,7 @@ func TestCreate(t *testing.T) {
}

// meta is nil
err = tenantDS.Create(ctx, tcr1)
err := tenantDS.Create(ctx, tcr1)
require.Error(t, err)
require.EqualError(t, err, "create of type:tenant failed, meta is nil")

Expand Down Expand Up @@ -271,8 +268,7 @@ func TestCreate(t *testing.T) {

func TestUpdate(t *testing.T) {
const t3 = "t3"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

Expand All @@ -281,7 +277,7 @@ func TestUpdate(t *testing.T) {
Name: "ctenant",
Description: "C Tenant",
}
err = tenantDS.Update(ctx, tcr1)
err := tenantDS.Update(ctx, tcr1)
require.Error(t, err)
require.EqualError(t, err, "update of type:tenant failed, meta is nil")

Expand Down Expand Up @@ -344,33 +340,30 @@ func TestUpdate(t *testing.T) {

//nolint:unparam
func checkHistoryCreated(ctx context.Context, t *testing.T, id string, name string, desc string) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
var tgrhc v1.Tenant
err = tenantDS.GetHistoryCreated(ctx, id, &tgrhc)
err := tenantDS.GetHistoryCreated(ctx, id, &tgrhc)
require.NoError(t, err)
assert.Equal(t, name, tgrhc.Name)
assert.Equal(t, desc, tgrhc.GetDescription())
}

func checkHistory(ctx context.Context, t *testing.T, id string, tm time.Time, name string, desc string) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
var tgrh v1.Tenant
err = tenantDS.GetHistory(ctx, id, tm, &tgrh)
err := tenantDS.GetHistory(ctx, id, tm, &tgrh)
require.NoError(t, err)
assert.Equal(t, name, tgrh.Name)
assert.Equal(t, desc, tgrh.GetDescription())
}

func TestGet(t *testing.T) {
const t4 = "t4"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()
// unknown id
_, err = tenantDS.Get(ctx, "unknown-id")
_, err := tenantDS.Get(ctx, "unknown-id")
require.Error(t, err)
require.EqualError(t, err, "tenant with id:unknown-id not found sql: no rows in result set")

Expand All @@ -396,16 +389,15 @@ func TestGet(t *testing.T) {

func TestGetHistory(t *testing.T) {
const t5 = "t5"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

tsNow := time.Date(2020, 4, 30, 18, 0, 0, 0, time.UTC)

// unknown id
var tgr1 v1.Tenant
err = tenantDS.GetHistory(ctx, "unknown-id", tsNow, &tgr1)
err := tenantDS.GetHistory(ctx, "unknown-id", tsNow, &tgr1)
require.Error(t, err)
require.EqualError(t, err, "entity of type:tenant with predicate:[map[id:unknown-id] map[created_at:2020-04-30 18:00:00 +0000 UTC]] not found")

Expand Down Expand Up @@ -487,8 +479,7 @@ func TestGetHistory(t *testing.T) {

func TestFind(t *testing.T) {
const t6 = "t6"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

Expand All @@ -498,7 +489,7 @@ func TestFind(t *testing.T) {
Name: "ftenant",
Description: "F Tenant",
}
err = tenantDS.Create(ctx, tcr1)
err := tenantDS.Create(ctx, tcr1)
require.NoError(t, err)
assert.Equal(t, t6, tcr1.GetMeta().GetId())
assert.Equal(t, "ftenant", tcr1.GetName())
Expand Down Expand Up @@ -554,10 +545,9 @@ func TestFind(t *testing.T) {
}

func TestFindWithPaging(t *testing.T) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
tenantDS := New(slog.Default(), db, &v1.Tenant{})
// prevent side effects
db.MustExec("DELETE from tenants")
require.NoError(t, err)
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

Expand Down Expand Up @@ -595,16 +585,15 @@ func TestFindWithPaging(t *testing.T) {

func TestDelete(t *testing.T) {
const t9 = "t9"
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

// unknown id
tdr1 := &v1.Tenant{
Meta: &v1.Meta{Id: "unknown-id"},
}
err = tenantDS.Delete(ctx, tdr1.Meta.Id)
err := tenantDS.Delete(ctx, tdr1.Meta.Id)
require.Error(t, err)
require.EqualError(t, err, "tenant with id:unknown-id not found sql: no rows in result set")

Expand Down Expand Up @@ -642,16 +631,15 @@ func TestDeleteAll(t *testing.T) {
t11 = "t11"
t10 = "t10"
)
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()

// unknown id
tdr1 := &v1.Tenant{
Meta: &v1.Meta{Id: "unknown-id"},
}
err = tenantDS.DeleteAll(ctx, tdr1.Meta.Id)
err := tenantDS.DeleteAll(ctx, tdr1.Meta.Id)
require.Error(t, err)
require.EqualError(t, err, "tenant with id:unknown-id not found sql: no rows in result set")

Expand Down Expand Up @@ -702,8 +690,7 @@ func TestDeleteAll(t *testing.T) {
}

func TestAnnotationsAndLabels(t *testing.T) {
tenantDS, err := New(slog.Default(), db, &v1.Tenant{})
require.NoError(t, err)
tenantDS := New(slog.Default(), db, &v1.Tenant{})
assert.NotNil(t, tenantDS, "Datastore must not be nil")
ctx := context.Background()
tcr := &v1.Tenant{
Expand All @@ -721,7 +708,7 @@ func TestAnnotationsAndLabels(t *testing.T) {
Description: "A very important Tenant",
}

err = tenantDS.Create(ctx, tcr)
err := tenantDS.Create(ctx, tcr)
require.NoError(t, err)
assert.NotNil(t, tcr)
assert.Equal(t, int64(0), tcr.Meta.Version)
Expand Down
19 changes: 5 additions & 14 deletions pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,16 @@ type projectService struct {
log *slog.Logger
}

func NewProjectService(db *sqlx.DB, l *slog.Logger) (*projectService, error) {
ps, err := datastore.New(l, db, &v1.Project{})
if err != nil {
return nil, err
}
ts, err := datastore.New(l, db, &v1.Tenant{})
if err != nil {
return nil, err
}
pms, err := datastore.New(l, db, &v1.ProjectMember{})
if err != nil {
return nil, err
}
func NewProjectService(db *sqlx.DB, l *slog.Logger) *projectService {
ps := datastore.New(l, db, &v1.Project{})
ts := datastore.New(l, db, &v1.Tenant{})
pms := datastore.New(l, db, &v1.ProjectMember{})
return &projectService{
projectStore: NewStorageStatusWrapper(ps),
projectMemberStore: NewStorageStatusWrapper(pms),
tenantStore: NewStorageStatusWrapper(ts),
log: l,
}, nil
}
}

func (s *projectService) Create(ctx context.Context, req *v1.ProjectCreateRequest) (*v1.ProjectResponse, error) {
Expand Down
19 changes: 5 additions & 14 deletions pkg/service/projectmember.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,16 @@ type projectMemberService struct {
log *slog.Logger
}

func NewProjectMemberService(db *sqlx.DB, l *slog.Logger) (*projectMemberService, error) {
pms, err := datastore.New(l, db, &v1.ProjectMember{})
if err != nil {
return nil, err
}
ts, err := datastore.New(l, db, &v1.Tenant{})
if err != nil {
return nil, err
}
ps, err := datastore.New(l, db, &v1.Project{})
if err != nil {
return nil, err
}
func NewProjectMemberService(db *sqlx.DB, l *slog.Logger) *projectMemberService {
pms := datastore.New(l, db, &v1.ProjectMember{})
ts := datastore.New(l, db, &v1.Tenant{})
ps := datastore.New(l, db, &v1.Project{})
return &projectMemberService{
projectMemberStore: NewStorageStatusWrapper(pms),
tenantStore: NewStorageStatusWrapper(ts),
projectStore: NewStorageStatusWrapper(ps),
log: l,
}, nil
}
}

func (s *projectMemberService) Create(ctx context.Context, req *v1.ProjectMemberCreateRequest) (*v1.ProjectMemberResponse, error) {
Expand Down
14 changes: 4 additions & 10 deletions pkg/service/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,15 @@ var (
tenants = datastore.Entity(&v1.Tenant{})
)

func NewTenantService(db *sqlx.DB, l *slog.Logger) (*tenantService, error) {
ts, err := datastore.New(l, db, &v1.Tenant{})
if err != nil {
return nil, err
}
tms, err := datastore.New(l, db, &v1.TenantMember{})
if err != nil {
return nil, err
}
func NewTenantService(db *sqlx.DB, l *slog.Logger) *tenantService {
ts := datastore.New(l, db, &v1.Tenant{})
tms := datastore.New(l, db, &v1.TenantMember{})
return &tenantService{
db: db,
tenantStore: NewStorageStatusWrapper(ts),
tenantMemberStore: NewStorageStatusWrapper(tms),
log: l,
}, nil
}
}

func (s *tenantService) Create(ctx context.Context, req *v1.TenantCreateRequest) (*v1.TenantResponse, error) {
Expand Down
Loading

0 comments on commit b3e3a1f

Please sign in to comment.