Skip to content

Commit 42cbf81

Browse files
committed
request: switch all request types to value receivers and value returns
All New*Request() constructors now return values instead of pointers. All methods on request types use value receivers and return values, enabling immutable builder-style chaining. Renamed all value constructors from Make* to New* for naming consistency across the connector: crud.Make*Request → crud.New*Request, datetime.MakeDatetime → datetime.NewDatetime, decimal.MakeDecimal / MakeDecimalFromString / MustMakeDecimal → decimal.NewDecimal / NewDecimalFromString / MustNewDecimal, arrow.MakeArrow → arrow.NewArrow, crud.MakeResult → crud.NewResult. Removed intermediate spaceRequest, spaceIndexRequest types from the tarantool package and spaceRequest from the crud package — space and index fields are inlined directly into each request struct. Converted all constructors to composite literal style. Applied the same pattern to arrow/ and settings/ packages. In the box/ subpackage, request types no longer embed tarantool.CallRequest. They store it as a private field via baseCallRequest and implement their own Context() method returning the wrapper type. Part of #238
1 parent f8937c2 commit 42cbf81

55 files changed

Lines changed: 1060 additions & 907 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
4545

4646
### Changed
4747

48+
* All top-level `New*Request()` constructors now return values instead of
49+
pointers. All methods on request types use value receivers and return
50+
values, enabling immutable builder-style chaining.
51+
* Renamed value constructors `Make*` to `New*` for naming consistency across
52+
the connector. Affects `crud.Make*Request` (now `crud.New*Request`),
53+
`datetime.MakeDatetime` (now `datetime.NewDatetime`), `decimal.MakeDecimal`
54+
and `decimal.MakeDecimalFromString` (now `decimal.NewDecimal` and
55+
`decimal.NewDecimalFromString`), `decimal.MustMakeDecimal` (now
56+
`decimal.MustNewDecimal`), `arrow.MakeArrow` (now `arrow.NewArrow`), and
57+
`crud.MakeResult` (now `crud.NewResult`).
58+
* Removed intermediate `spaceRequest`, `spaceIndexRequest` types — `space`
59+
and `index` fields are now inlined directly into each request struct.
60+
The same flattening was applied to `crud.spaceRequest`.
4861
* Required Go version is `1.24` now (#456).
4962
* `test_helpers.MockDoer` is now an interface instead of a struct. The
5063
`Requests` field became a method `Requests()`. The `NewMockDoer()`

MIGRATION.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,51 @@ TODO
182182
stream, _ := conn.NewStream()
183183
log.Printf("opened stream on %v", conn)
184184
```
185+
* All request types (`PingRequest`, `SelectRequest`, `CallRequest`, etc.) now
186+
use value receivers and constructors return values instead of pointers.
187+
Builder methods return modified copies instead of mutating in place.
188+
189+
**Breaking**: calling a builder method without using its return value silently
190+
discards the change:
191+
```Go
192+
// Before (pointer receivers, mutation in place):
193+
req := NewSelectRequest("space")
194+
req.Index("idx") // mutated req directly
195+
conn.Do(req)
196+
197+
// After (value receivers, must use return value):
198+
req := NewSelectRequest("space")
199+
req = req.Index("idx") // must reassign
200+
conn.Do(req)
201+
202+
// Or chain directly:
203+
conn.Do(NewSelectRequest("space").Index("idx"))
204+
```
205+
206+
The same value-receiver pattern was applied to `arrow.InsertRequest` and
207+
`settings.SetRequest`/`settings.GetRequest`.
208+
209+
In the `box` subpackage, request types (`InfoRequest`, `UserExistsRequest`,
210+
`UserCreateRequest`, `SessionSuRequest`, etc.) no longer embed
211+
`tarantool.CallRequest`. They store it as a private field and implement
212+
their own `Context()` method returning the wrapper type. Usage stays clean:
213+
```Go
214+
req := box.NewUserExistsRequest(username).Context(ctx)
215+
```
216+
* Renamed value constructors from `Make*` to `New*` for naming consistency
217+
across the connector. Previously, the main package used `New*Request` while
218+
the `crud` package and value-type constructors used `Make*`. They now all
219+
use the `New*` prefix.
220+
221+
| Before | After |
222+
|---|---|
223+
| `datetime.MakeDatetime` | `datetime.NewDatetime` |
224+
| `decimal.MakeDecimal` | `decimal.NewDecimal` |
225+
| `decimal.MakeDecimalFromString` | `decimal.NewDecimalFromString` |
226+
| `decimal.MustMakeDecimal` | `decimal.MustNewDecimal` |
227+
| `arrow.MakeArrow` | `arrow.NewArrow` |
228+
| `crud.MakeResult` | `crud.NewResult` |
229+
| `crud.Make*Request` (all of them) | `crud.New*Request` |
185230

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

@@ -440,12 +485,13 @@ is supported.
440485

441486
Now you need to use objects of the Datetime type instead of pointers to it. A
442487
new constructor `MakeDatetime` returns an object. `NewDatetime` has been
443-
removed.
488+
removed. (`MakeDatetime` was later renamed back to `NewDatetime` in v3.)
444489

445490
### decimal package
446491

447492
Now you need to use objects of the Decimal type instead of pointers to it. A
448493
new constructor `MakeDecimal` returns an object. `NewDecimal` has been removed.
494+
(`MakeDecimal` was later renamed back to `NewDecimal` in v3.)
449495

450496
### multi package
451497

arrow/arrow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ type Arrow struct {
1717
data []byte
1818
}
1919

20-
// MakeArrow returns a new arrow.Arrow object that contains
20+
// NewArrow returns a new arrow.Arrow object that contains
2121
// wrapped a raw arrow data buffer.
22-
func MakeArrow(arrow []byte) (Arrow, error) {
22+
func NewArrow(arrow []byte) (Arrow, error) {
2323
return Arrow{arrow}, nil
2424
}
2525

arrow/arrow_gen_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestSomeOptionalArrow(t *testing.T) {
13-
val, err := MakeArrow([]byte{1, 2, 3})
13+
val, err := NewArrow([]byte{1, 2, 3})
1414
require.NoError(t, err)
1515
opt := SomeOptionalArrow(val)
1616

@@ -33,7 +33,7 @@ func TestNoneOptionalArrow(t *testing.T) {
3333
}
3434

3535
func TestOptionalArrow_MustGet(t *testing.T) {
36-
val, err := MakeArrow([]byte{1, 2, 3})
36+
val, err := NewArrow([]byte{1, 2, 3})
3737
require.NoError(t, err)
3838
optSome := SomeOptionalArrow(val)
3939
optNone := NoneOptionalArrow()
@@ -43,7 +43,7 @@ func TestOptionalArrow_MustGet(t *testing.T) {
4343
}
4444

4545
func TestOptionalArrow_Unwrap(t *testing.T) {
46-
val, err := MakeArrow([]byte{1, 2, 3})
46+
val, err := NewArrow([]byte{1, 2, 3})
4747
require.NoError(t, err)
4848
optSome := SomeOptionalArrow(val)
4949
optNone := NoneOptionalArrow()
@@ -53,9 +53,9 @@ func TestOptionalArrow_Unwrap(t *testing.T) {
5353
}
5454

5555
func TestOptionalArrow_UnwrapOr(t *testing.T) {
56-
val, err := MakeArrow([]byte{1, 2, 3})
56+
val, err := NewArrow([]byte{1, 2, 3})
5757
require.NoError(t, err)
58-
def, err := MakeArrow([]byte{4, 5, 6})
58+
def, err := NewArrow([]byte{4, 5, 6})
5959
require.NoError(t, err)
6060
optSome := SomeOptionalArrow(val)
6161
optNone := NoneOptionalArrow()
@@ -65,9 +65,9 @@ func TestOptionalArrow_UnwrapOr(t *testing.T) {
6565
}
6666

6767
func TestOptionalArrow_UnwrapOrElse(t *testing.T) {
68-
val, err := MakeArrow([]byte{1, 2, 3})
68+
val, err := NewArrow([]byte{1, 2, 3})
6969
require.NoError(t, err)
70-
def, err := MakeArrow([]byte{4, 5, 6})
70+
def, err := NewArrow([]byte{4, 5, 6})
7171
require.NoError(t, err)
7272
optSome := SomeOptionalArrow(val)
7373
optNone := NoneOptionalArrow()
@@ -77,7 +77,7 @@ func TestOptionalArrow_UnwrapOrElse(t *testing.T) {
7777
}
7878

7979
func TestOptionalArrow_EncodeDecodeMsgpack_Some(t *testing.T) {
80-
val, err := MakeArrow([]byte{1, 2, 3})
80+
val, err := NewArrow([]byte{1, 2, 3})
8181
require.NoError(t, err)
8282
some := SomeOptionalArrow(val)
8383

arrow/arrow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestEncodeArrow(t *testing.T) {
7272
buf := bytes.NewBuffer([]byte{})
7373
enc := msgpack.NewEncoder(buf)
7474

75-
arr, err := arrow.MakeArrow(tt.arr)
75+
arr, err := arrow.NewArrow(tt.arr)
7676
require.NoError(t, err)
7777

7878
err = enc.Encode(arr)

arrow/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Example() {
4242
log.Fatalf("Failed to connect: %s", err)
4343
}
4444

45-
arr, err := arrow.MakeArrow(arrowBinData)
45+
arr, err := arrow.NewArrow(arrowBinData)
4646
if err != nil {
4747
log.Fatalf("Failed prepare Arrow data: %s", err)
4848
}

arrow/request.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ type InsertRequest struct {
1919
}
2020

2121
// NewInsertRequest returns a new InsertRequest.
22-
func NewInsertRequest(space any, arrow Arrow) *InsertRequest {
23-
return &InsertRequest{
22+
func NewInsertRequest(space any, arrow Arrow) InsertRequest {
23+
return InsertRequest{
2424
space: space,
2525
arrow: arrow,
2626
}
2727
}
2828

2929
// Type returns a IPROTO_INSERT_ARROW type for the request.
30-
func (r *InsertRequest) Type() iproto.Type {
30+
func (r InsertRequest) Type() iproto.Type {
3131
return iproto.IPROTO_INSERT_ARROW
3232
}
3333

3434
// Async returns false to the request return a response.
35-
func (r *InsertRequest) Async() bool {
35+
func (r InsertRequest) Async() bool {
3636
return false
3737
}
3838

3939
// Ctx returns a context of the request.
40-
func (r *InsertRequest) Ctx() context.Context {
40+
func (r InsertRequest) Ctx() context.Context {
4141
return r.ctx
4242
}
4343

@@ -47,20 +47,20 @@ func (r *InsertRequest) Ctx() context.Context {
4747
// the timeout option for Connection does not affect the lifetime
4848
// of the request. For those purposes use context.WithTimeout() as
4949
// the root context.
50-
func (r *InsertRequest) Context(ctx context.Context) *InsertRequest {
50+
func (r InsertRequest) Context(ctx context.Context) InsertRequest {
5151
r.ctx = ctx
5252
return r
5353
}
5454

5555
// Arrow sets the arrow for insertion the insert arrow request.
5656
// Note: default value is nil.
57-
func (r *InsertRequest) Arrow(arrow Arrow) *InsertRequest {
57+
func (r InsertRequest) Arrow(arrow Arrow) InsertRequest {
5858
r.arrow = arrow
5959
return r
6060
}
6161

6262
// Body fills an msgpack.Encoder with the insert arrow request body.
63-
func (r *InsertRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
63+
func (r InsertRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
6464
if err := enc.EncodeMapLen(2); err != nil {
6565
return err
6666
}
@@ -74,7 +74,7 @@ func (r *InsertRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder)
7474
}
7575

7676
// Response creates a response for the InsertRequest.
77-
func (r *InsertRequest) Response(
77+
func (r InsertRequest) Response(
7878
header tarantool.Header,
7979
body io.Reader,
8080
) (tarantool.Response, error) {

arrow/request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestInsertRequestSetters(t *testing.T) {
129129
buf := bytes.NewBuffer([]byte{})
130130
enc := msgpack.NewEncoder(buf)
131131

132-
arr, err := arrow.MakeArrow([]byte{'a', 'b', 'c'})
132+
arr, err := arrow.NewArrow([]byte{'a', 'b', 'c'})
133133
require.NoError(t, err)
134134

135135
resolver := stubSchemeResolver{validSpace}

arrow/tarantool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestInsert_invalid(t *testing.T) {
7171
data, err := hex.DecodeString(a.arrow)
7272
require.NoError(t, err)
7373

74-
arr, err := arrow.MakeArrow(data)
74+
arr, err := arrow.NewArrow(data)
7575
require.NoError(t, err)
7676

7777
req := arrow.NewInsertRequest(space, arr)

box/info.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package box
22

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

67
"github.com/vmihailenco/msgpack/v5"
78

89
"github.com/tarantool/go-tarantool/v3"
910
)
1011

11-
var _ tarantool.Request = (*InfoRequest)(nil)
12+
var _ tarantool.Request = InfoRequest{}
1213

1314
// Info represents detailed information about the Tarantool instance.
1415
// It includes version, node ID, read-only status, process ID, cluster information, and more.
@@ -112,14 +113,20 @@ func (ir *InfoResponse) DecodeMsgpack(d *msgpack.Decoder) error {
112113
// InfoRequest represents a request to retrieve information about the Tarantool instance.
113114
// It implements the tarantool.Request interface.
114115
type InfoRequest struct {
115-
*tarantool.CallRequest // Underlying Tarantool call request.
116+
baseCallRequest
116117
}
117118

118119
// NewInfoRequest returns a new empty info request.
119120
func NewInfoRequest() InfoRequest {
120-
callReq := tarantool.NewCallRequest("box.info")
121-
122121
return InfoRequest{
123-
callReq,
122+
baseCallRequest: baseCallRequest{
123+
call: tarantool.NewCallRequest("box.info"),
124+
},
124125
}
125126
}
127+
128+
// Context sets a passed context to the request.
129+
func (req InfoRequest) Context(ctx context.Context) InfoRequest {
130+
req.call = req.call.Context(ctx)
131+
return req
132+
}

0 commit comments

Comments
 (0)