Skip to content

Commit 07de408

Browse files
author
maksim.konovalov
committed
Added logic for working with Tarantool schema via Box
- Implemented the `box.Schema()` method that returns a `Schema` object for schema-related operations
1 parent 7eae014 commit 07de408

10 files changed

+407
-55
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1010

1111
### Added
1212

13+
- Implemented box.schema.user operations requests and sugar interface.
14+
1315
### Changed
1416

17+
- Box Info method now requires context and implements CallRequest type instead custom baseRequest.
18+
1519
### Fixed
1620

1721
## [v2.2.1] - 2024-12-17

box/box.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package box
22

33
import (
4+
"context"
5+
46
"github.com/tarantool/go-tarantool/v2"
57
)
68

@@ -17,13 +19,19 @@ func New(conn tarantool.Doer) *Box {
1719
}
1820
}
1921

22+
// Schema returns a new Schema instance, providing access to schema-related operations.
23+
// It uses the connection from the Box instance to communicate with Tarantool.
24+
func (b *Box) Schema() *Schema {
25+
return NewSchema(b.conn)
26+
}
27+
2028
// Info retrieves the current information of the Tarantool instance.
2129
// It calls the "box.info" function and parses the result into the Info structure.
22-
func (b *Box) Info() (Info, error) {
30+
func (b *Box) Info(ctx context.Context) (Info, error) {
2331
var infoResp InfoResponse
2432

2533
// Call "box.info" to get instance information from Tarantool.
26-
fut := b.conn.Do(NewInfoRequest())
34+
fut := b.conn.Do(NewInfoRequest().Context(ctx))
2735

2836
// Parse the result into the Info structure.
2937
err := fut.GetTyped(&infoResp)

box/box_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package box_test
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -24,6 +25,6 @@ func TestNew(t *testing.T) {
2425

2526
// Calling Info on a box with a nil connection will result in a panic, since the underlying
2627
// connection (Doer) cannot perform the requested action (it's nil).
27-
_, _ = b.Info()
28+
_, _ = b.Info(context.TODO())
2829
})
2930
}

box/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Example() {
4646

4747
b := box.New(client)
4848

49-
info, err := b.Info()
49+
info, err := b.Info(ctx)
5050
if err != nil {
5151
log.Fatalf("Failed get box info: %s", err)
5252
}

box/info.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,14 @@ func (ir *InfoResponse) DecodeMsgpack(d *msgpack.Decoder) error {
5959
// InfoRequest represents a request to retrieve information about the Tarantool instance.
6060
// It implements the tarantool.Request interface.
6161
type InfoRequest struct {
62-
baseRequest
63-
}
64-
65-
// Body method is used to serialize the request's body.
66-
// It is part of the tarantool.Request interface implementation.
67-
func (i InfoRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
68-
return i.impl.Body(res, enc)
62+
*tarantool.CallRequest // Underlying Tarantool call request.
6963
}
7064

7165
// NewInfoRequest returns a new empty info request.
7266
func NewInfoRequest() InfoRequest {
73-
req := InfoRequest{}
74-
req.impl = newCall("box.info")
75-
return req
67+
callReq := tarantool.NewCallRequest("box.info")
68+
69+
return InfoRequest{
70+
callReq,
71+
}
7672
}

box/request.go

-38
This file was deleted.

box/schema.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package box
2+
3+
import "github.com/tarantool/go-tarantool/v2"
4+
5+
// Schema represents the schema-related operations in Tarantool.
6+
// It holds a connection to interact with the Tarantool instance.
7+
type Schema struct {
8+
conn tarantool.Doer // Connection interface for interacting with Tarantool.
9+
}
10+
11+
func NewSchema(conn tarantool.Doer) *Schema {
12+
return &Schema{conn: conn} // Pass the connection to the Schema.
13+
}
14+
15+
// User returns a new SchemaUser instance, allowing schema-related user operations.
16+
func (s *Schema) User() *SchemaUser {
17+
return NewSchemaUser(s.conn)
18+
}

box/schema_user.go

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package box
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/tarantool/go-tarantool/v2"
8+
"github.com/vmihailenco/msgpack/v5"
9+
)
10+
11+
// SchemaUser provides methods to interact with schema-related user operations in Tarantool.
12+
type SchemaUser struct {
13+
conn tarantool.Doer // Connection interface for interacting with Tarantool.
14+
}
15+
16+
func NewSchemaUser(conn tarantool.Doer) *SchemaUser {
17+
return &SchemaUser{conn: conn}
18+
}
19+
20+
// UserExistsRequest represents a request to check if a user exists in Tarantool.
21+
type UserExistsRequest struct {
22+
*tarantool.CallRequest // Underlying Tarantool call request.
23+
}
24+
25+
// UserExistsResponse represents the response to a user existence check.
26+
type UserExistsResponse struct {
27+
Exists bool // True if the user exists, false otherwise.
28+
}
29+
30+
// DecodeMsgpack decodes the response from a Msgpack-encoded byte slice.
31+
func (uer *UserExistsResponse) DecodeMsgpack(d *msgpack.Decoder) error {
32+
arrayLen, err := d.DecodeArrayLen()
33+
if err != nil {
34+
return err
35+
}
36+
37+
// Ensure that the response array contains exactly 1 element (the "Exists" field).
38+
if arrayLen != 1 {
39+
return fmt.Errorf("protocol violation; expected 1 array entry, got %d", arrayLen)
40+
}
41+
42+
// Decode the boolean value indicating whether the user exists.
43+
uer.Exists, err = d.DecodeBool()
44+
45+
return err
46+
}
47+
48+
// NewUserExistsRequest creates a new request to check if a user exists.
49+
func NewUserExistsRequest(username string) UserExistsRequest {
50+
callReq := tarantool.NewCallRequest("box.schema.user.exists").Args([]interface{}{username})
51+
52+
return UserExistsRequest{
53+
callReq,
54+
}
55+
}
56+
57+
// Exists checks if the specified user exists in Tarantool.
58+
func (u *SchemaUser) Exists(ctx context.Context, username string) (bool, error) {
59+
// Create a request and send it to Tarantool.
60+
req := NewUserExistsRequest(username).Context(ctx)
61+
resp := &UserExistsResponse{}
62+
63+
// Execute the request and parse the response.
64+
err := u.conn.Do(req).GetTyped(resp)
65+
66+
return resp.Exists, err
67+
}
68+
69+
// UserCreateOptions represents options for creating a user in Tarantool.
70+
type UserCreateOptions struct {
71+
// IfNotExists - if true, prevents an error if the user already exists.
72+
IfNotExists bool `msgpack:"if_not_exists"`
73+
// Password for the new user.
74+
Password string `msgpack:"password"`
75+
}
76+
77+
// UserCreateRequest represents a request to create a new user in Tarantool.
78+
type UserCreateRequest struct {
79+
*tarantool.CallRequest // Underlying Tarantool call request.
80+
}
81+
82+
// NewUserCreateRequest creates a new request to create a user with specified options.
83+
func NewUserCreateRequest(username string, options UserCreateOptions) UserCreateRequest {
84+
callReq := tarantool.NewCallRequest("box.schema.user.create").
85+
Args([]interface{}{username, options})
86+
87+
return UserCreateRequest{
88+
callReq,
89+
}
90+
}
91+
92+
// UserCreateResponse represents the response to a user creation request.
93+
type UserCreateResponse struct {
94+
}
95+
96+
// DecodeMsgpack decodes the response for a user creation request.
97+
// In this case, the response does not contain any data.
98+
func (uer *UserCreateResponse) DecodeMsgpack(_ *msgpack.Decoder) error {
99+
return nil
100+
}
101+
102+
// Create creates a new user in Tarantool with the given username and options.
103+
func (u *SchemaUser) Create(ctx context.Context, username string, options UserCreateOptions) error {
104+
// Create a request and send it to Tarantool.
105+
req := NewUserCreateRequest(username, options).Context(ctx)
106+
resp := &UserCreateResponse{}
107+
108+
// Execute the request and handle the response.
109+
fut := u.conn.Do(req)
110+
111+
err := fut.GetTyped(resp)
112+
if err != nil {
113+
return err
114+
}
115+
116+
return nil
117+
}
118+
119+
// UserDropOptions represents options for dropping a user in Tarantool.
120+
type UserDropOptions struct {
121+
IfExists bool `msgpack:"if_exists"` // If true, prevents an error if the user does not exist.
122+
}
123+
124+
// UserDropRequest represents a request to drop a user from Tarantool.
125+
type UserDropRequest struct {
126+
*tarantool.CallRequest // Underlying Tarantool call request.
127+
}
128+
129+
// NewUserDropRequest creates a new request to drop a user with specified options.
130+
func NewUserDropRequest(username string, options UserDropOptions) UserDropRequest {
131+
callReq := tarantool.NewCallRequest("box.schema.user.drop").
132+
Args([]interface{}{username, options})
133+
134+
return UserDropRequest{
135+
callReq,
136+
}
137+
}
138+
139+
// UserDropResponse represents the response to a user drop request.
140+
type UserDropResponse struct{}
141+
142+
// Drop drops the specified user from Tarantool, with optional conditions.
143+
func (u *SchemaUser) Drop(ctx context.Context, username string, options UserDropOptions) error {
144+
// Create a request and send it to Tarantool.
145+
req := NewUserDropRequest(username, options).Context(ctx)
146+
resp := &UserCreateResponse{}
147+
148+
// Execute the request and handle the response.
149+
fut := u.conn.Do(req)
150+
151+
err := fut.GetTyped(resp)
152+
if err != nil {
153+
return err
154+
}
155+
156+
return nil
157+
}
158+
159+
// UserPasswordRequest represents a request to retrieve a user's password from Tarantool.
160+
type UserPasswordRequest struct {
161+
*tarantool.CallRequest // Underlying Tarantool call request.
162+
}
163+
164+
// NewUserPasswordRequest creates a new request to fetch the user's password.
165+
// It takes the username and constructs the request to Tarantool.
166+
func NewUserPasswordRequest(username string) UserPasswordRequest {
167+
// Create a request to get the user's password.
168+
callReq := tarantool.NewCallRequest("box.schema.user.password").Args([]interface{}{username})
169+
170+
return UserPasswordRequest{
171+
callReq,
172+
}
173+
}
174+
175+
// UserPasswordResponse represents the response to the user password request.
176+
// It contains the password hash.
177+
type UserPasswordResponse struct {
178+
Hash string // The password hash of the user.
179+
}
180+
181+
// DecodeMsgpack decodes the response from Tarantool in Msgpack format.
182+
// It expects the response to be an array of length 1, containing the password hash string.
183+
func (upr *UserPasswordResponse) DecodeMsgpack(d *msgpack.Decoder) error {
184+
// Decode the array length.
185+
arrayLen, err := d.DecodeArrayLen()
186+
if err != nil {
187+
return err
188+
}
189+
190+
// Ensure the array contains exactly 1 element (the password hash).
191+
if arrayLen != 1 {
192+
return fmt.Errorf("protocol violation; expected 1 array entry, got %d", arrayLen)
193+
}
194+
195+
// Decode the string containing the password hash.
196+
upr.Hash, err = d.DecodeString()
197+
198+
return err
199+
}
200+
201+
// Password sends a request to retrieve the user's password from Tarantool.
202+
// It returns the password hash as a string or an error if the request fails.
203+
func (u *SchemaUser) Password(ctx context.Context, username string) (string, error) {
204+
// Create the request and send it to Tarantool.
205+
req := NewUserPasswordRequest(username).Context(ctx)
206+
resp := &UserPasswordResponse{}
207+
208+
// Execute the request and handle the response.
209+
fut := u.conn.Do(req)
210+
211+
// Get the decoded response.
212+
err := fut.GetTyped(resp)
213+
if err != nil {
214+
return "", err
215+
}
216+
217+
// Return the password hash.
218+
return resp.Hash, nil
219+
}

0 commit comments

Comments
 (0)