Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
### Added

* New types for MessagePack extensions compatible with go-option (#459).
* Added `box.MustNew` wrapper for `box.New` without an error (#448).

### Changed

* Required Go version is `1.24` now (#456).
* `box.New` returns an error instead of panic (#448).

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ TODO
### <a id="major-changes-v3">Major changes</a>

* Required Go version is `1.24` now.
* `box.New` returns an error instead of panic
* Added `box.MustNew` wrapper for `box.New` without an error

## Migration from v1.x.x to v2.x.x

Expand Down
18 changes: 14 additions & 4 deletions box/box.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package box

import (
"errors"

"github.com/tarantool/go-tarantool/v3"
)

Expand All @@ -11,16 +13,24 @@ type Box struct {
}

// New returns a new instance of the box structure, which implements the Box interface.
func New(conn tarantool.Doer) *Box {
func New(conn tarantool.Doer) (*Box, error) {
if conn == nil {
// Check if the provided Tarantool connection is nil, and if it is, panic with an error
// message. panic early helps to catch and fix nil pointer issues in the code.
panic("tarantool connection cannot be nil")
return nil, errors.New("tarantool connection cannot be nil")
}

return &Box{
conn: conn, // Assigns the provided Tarantool connection.
}, nil
}

// MustNew returns a new instance of the box structure, which implements the Box interface.
// It panics if conn == nil.
func MustNew(conn tarantool.Doer) *Box {
b, err := New(conn)
if err != nil {
panic(err)
}
return b
}

// Schema returns a new Schema instance, providing access to schema-related operations.
Expand Down
31 changes: 27 additions & 4 deletions box/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,31 @@ import (
func TestNew(t *testing.T) {
t.Parallel()

_, err := box.New(nil)
require.Error(t, err)
}

func TestMustNew(t *testing.T) {
t.Parallel()

// Create a box instance with a nil connection. This should lead to a panic.
require.Panics(t, func() { box.New(nil) })
require.Panics(t, func() { box.MustNew(nil) })
}

func TestMocked_BoxNew(t *testing.T) {
t.Parallel()

mock := test_helpers.NewMockDoer(t,
test_helpers.NewMockResponse(t, "valid"),
)

b, err := box.New(&mock)
require.NoError(t, err)
require.NotNil(t, b)

assert.Len(t, mock.Requests, 0)
b.Schema().User().Exists(box.NewInfoRequest().Ctx(), "")
require.Len(t, mock.Requests, 1)
}

func TestMocked_BoxInfo(t *testing.T) {
Expand All @@ -37,7 +60,7 @@ func TestMocked_BoxInfo(t *testing.T) {
mock := test_helpers.NewMockDoer(t,
test_helpers.NewMockResponse(t, data),
)
b := box.New(&mock)
b := box.MustNew(&mock)

info, err := b.Info()
require.NoError(t, err)
Expand All @@ -57,7 +80,7 @@ func TestMocked_BoxSchemaUserInfo(t *testing.T) {
mock := test_helpers.NewMockDoer(t,
test_helpers.NewMockResponse(t, data),
)
b := box.New(&mock)
b := box.MustNew(&mock)

privs, err := b.Schema().User().Info(context.Background(), "username")
require.NoError(t, err)
Expand All @@ -82,7 +105,7 @@ func TestMocked_BoxSessionSu(t *testing.T) {
test_helpers.NewMockResponse(t, []interface{}{}),
errors.New("user not found or supplied credentials are invalid"),
)
b := box.New(&mock)
b := box.MustNew(&mock)

err := b.Session().Su(context.Background(), "admin")
require.NoError(t, err)
Expand Down
13 changes: 7 additions & 6 deletions box/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ExampleBox_Info() {

// Or use simple Box implementation.

b := box.New(client)
b := box.MustNew(client)

info, err := b.Info()
if err != nil {
Expand Down Expand Up @@ -88,7 +88,8 @@ func ExampleSchemaUser_Exists() {
}

// Or use simple User implementation.
b := box.New(client)
b := box.MustNew(client)

exists, err := b.Schema().User().Exists(ctx, "user")
if err != nil {
log.Fatalf("Failed get box schema user exists with error: %s", err)
Expand Down Expand Up @@ -120,7 +121,7 @@ func ExampleSchemaUser_Create() {
}

// Create SchemaUser.
schemaUser := box.New(client).Schema().User()
schemaUser := box.MustNew(client).Schema().User()

// Create a new user.
username := "new_user"
Expand Down Expand Up @@ -153,7 +154,7 @@ func ExampleSchemaUser_Drop() {
}

// Create SchemaUser.
schemaUser := box.New(client).Schema().User()
schemaUser := box.MustNew(client).Schema().User()

// Drop an existing user.
username := "new_user"
Expand Down Expand Up @@ -192,7 +193,7 @@ func ExampleSchemaUser_Password() {
}

// Create SchemaUser.
schemaUser := box.New(client).Schema().User()
schemaUser := box.MustNew(client).Schema().User()

// Get the password hash.
password := "my-password"
Expand Down Expand Up @@ -221,7 +222,7 @@ func ExampleSchemaUser_Info() {
}

// Create SchemaUser.
schemaUser := box.New(client).Schema().User()
schemaUser := box.MustNew(client).Schema().User()

info, err := schemaUser.Info(ctx, "test")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion box/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestBox_Session(t *testing.T) {
b := box.New(th.Ptr(th.NewMockDoer(t)))
b := box.MustNew(th.Ptr(th.NewMockDoer(t)))
require.NotNil(t, b.Session())
}

Expand Down
50 changes: 27 additions & 23 deletions box/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func TestBox_Sugar_Info(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

info, err := box.New(conn).Info()
b := box.MustNew(conn)

info, err := b.Info()
require.NoError(t, err)

validateInfo(t, info)
Expand Down Expand Up @@ -83,7 +85,7 @@ func TestBox_Sugar_Schema_UserCreate_NoError(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user.
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand All @@ -103,7 +105,7 @@ func TestBox_Sugar_Schema_UserCreate_CanConnectWithNewCred(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user.
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand Down Expand Up @@ -137,7 +139,7 @@ func TestBox_Sugar_Schema_UserCreate_AlreadyExists(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user.
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand Down Expand Up @@ -167,7 +169,7 @@ func TestBox_Sugar_Schema_UserCreate_ExistsTrue(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user.
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand All @@ -193,7 +195,7 @@ func TestBox_Sugar_Schema_UserCreate_IfNotExistsNoErr(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user.
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand All @@ -217,7 +219,7 @@ func TestBox_Sugar_Schema_UserPassword(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Require password hash.
hash, err := b.Schema().User().Password(ctx, password)
Expand All @@ -236,7 +238,7 @@ func TestBox_Sugar_Schema_UserDrop_AfterCreate(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand All @@ -258,7 +260,7 @@ func TestBox_Sugar_Schema_UserDrop_DoubleDrop(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Create new user
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
Expand Down Expand Up @@ -286,7 +288,7 @@ func TestBox_Sugar_Schema_UserDrop_UnknownUser(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// Require error cause user not exists
err = b.Schema().User().Drop(ctx, "some_strange_not_existing_name", box.UserDropOptions{})
Expand All @@ -305,7 +307,7 @@ func TestSchemaUser_Passwd_NotFound(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Passwd(ctx, "not-exists-passwd", "new_password")
require.Error(t, err)
Expand All @@ -329,7 +331,7 @@ func TestSchemaUser_Passwd_Ok(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

// New user change password and connect

Expand Down Expand Up @@ -367,7 +369,7 @@ func TestSchemaUser_Passwd_WithoutGrants(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username,
box.UserCreateOptions{Password: startPassword, IfNotExists: true})
Expand All @@ -381,7 +383,8 @@ func TestSchemaUser_Passwd_WithoutGrants(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, conn2Fail)

bFail := box.New(conn2Fail)
bFail := box.MustNew(conn2Fail)

// can't change self user password without grants
err = bFail.Schema().User().Passwd(ctx, endPassword)
require.Error(t, err)
Expand All @@ -401,7 +404,7 @@ func TestSchemaUser_Info_TestUserCorrect(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

privileges, err := b.Schema().User().Info(ctx, dialer.User)
require.NoError(t, err)
Expand All @@ -418,7 +421,7 @@ func TestSchemaUser_Info_NonExistsUser(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

privileges, err := b.Schema().User().Info(ctx, "non-existing")
require.Error(t, err)
Expand All @@ -438,7 +441,7 @@ func TestBox_Sugar_Schema_UserGrant_NoSu(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)
Expand Down Expand Up @@ -471,7 +474,7 @@ func TestBox_Sugar_Schema_UserGrant_WithSu(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)
Expand Down Expand Up @@ -521,7 +524,7 @@ func TestSchemaUser_Revoke_WithoutSu(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)
Expand Down Expand Up @@ -555,7 +558,7 @@ func TestSchemaUser_Revoke_WithSu(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)
Expand Down Expand Up @@ -603,7 +606,7 @@ func TestSchemaUser_Revoke_NonExistsPermission(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)
Expand Down Expand Up @@ -639,7 +642,7 @@ func TestSession_Su_AdminPermissions(t *testing.T) {
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)
b := box.MustNew(conn)

err = b.Session().Su(ctx, "admin")
require.NoError(t, err)
Expand All @@ -652,7 +655,8 @@ func cleanupUser(username string) {
log.Fatal(err)
}

b := box.New(conn)
b := box.MustNew(conn)

err = b.Schema().User().Drop(ctx, username, box.UserDropOptions{})
if err != nil {
log.Fatal(err)
Expand Down
Loading