Skip to content

Commit cd8ab3d

Browse files
committed
api: unexport Stream fields, add Id() accessor
Make Stream.Id and Stream.Conn unexported to enforce encapsulation and prevent unsafe direct mutation. The stream identifier is now exposed via a Stream.Id() method; the underlying connection is no longer reachable from outside the package — callers should hold their own *Connection reference if they need it. - Rename Stream.Id to Stream.id, add Stream.Id() accessor. - Rename Stream.Conn to Stream.conn, drop public access. - Add SetStreamIdForTesting helper in export_test.go so external tests can still exercise wire-encoding for arbitrary stream ids. - Document the breaking change in CHANGELOG.md and MIGRATION.md. Closes #471
1 parent 52d167d commit cd8ab3d

6 files changed

Lines changed: 38 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
9393
Use `Opts.Logger *slog.Logger` instead. Pool `Opts.Logger *slog.Logger`
9494
replaces direct `log.Printf` calls that were not customizable.
9595
By default, logs are discarded (silent). See MIGRATION.md for details.
96+
* `Stream` struct fields `Id` and `Conn` are now unexported. Use the new
97+
`Stream.Id()` accessor for the stream identifier; the underlying
98+
connection is no longer reachable from outside the package (#471).
9699

97100
### Removed
98101

MIGRATION.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ TODO
166166
`tarantool.pool` group. When a pool logger is set and a connection
167167
does not have its own logger, the pool passes its logger to each
168168
connection, which then applies its own `WithGroup("tarantool")`.
169+
* `Stream` struct fields are unexported. The exported `Stream.Id` field
170+
is now an `Id()` method, and `Stream.Conn` is no longer accessible —
171+
the underlying connection is an internal detail and callers should
172+
hold their own `*Connection` reference if they need it.
173+
174+
Before:
175+
```go
176+
stream, _ := conn.NewStream()
177+
log.Printf("opened stream %d on %v", stream.Id, stream.Conn)
178+
```
179+
180+
After:
181+
```go
182+
stream, _ := conn.NewStream()
183+
log.Printf("opened stream %d on %v", stream.Id(), conn)
184+
```
169185

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

connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,8 @@ func (conn *Connection) NewPrepared(expr string) (*Prepared, error) {
12961296
func (conn *Connection) NewStream() (*Stream, error) {
12971297
next := conn.lastStreamId.Add(1)
12981298
return &Stream{
1299-
Id: next,
1300-
Conn: conn,
1299+
id: next,
1300+
conn: conn,
13011301
}, nil
13021302
}
13031303

export_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package tarantool
2+
3+
// SetStreamIdForTesting overrides a Stream's identifier for tests that need
4+
// to exercise wire-encoding of arbitrary stream id values.
5+
func SetStreamIdForTesting(s *Stream, id uint64) {
6+
s.id = id
7+
}

stream.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ var (
3131
)
3232

3333
type Stream struct {
34-
Id uint64
35-
Conn *Connection
34+
id uint64
35+
conn *Connection
36+
}
37+
38+
// Id returns the stream identifier assigned by the connection.
39+
func (s *Stream) Id() uint64 {
40+
return s.id
3641
}
3742

3843
// BeginRequest helps you to create a begin request object for execution
@@ -241,9 +246,9 @@ func (req *RollbackRequest) Context(ctx context.Context) *RollbackRequest {
241246
// create the future.
242247
func (s *Stream) Do(req Request) Future {
243248
if connectedReq, ok := req.(ConnectedRequest); ok {
244-
if connectedReq.Conn() != s.Conn {
249+
if connectedReq.Conn() != s.conn {
245250
return NewFutureWithErr(req, errUnknownStreamRequest)
246251
}
247252
}
248-
return s.Conn.send(req, s.Id)
253+
return s.conn.send(req, s.id)
249254
}

tarantool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ func TestStream_IdValues(t *testing.T) {
21122112

21132113
for _, id := range cases {
21142114
t.Run(fmt.Sprintf("%d", id), func(t *testing.T) {
2115-
stream.Id = id
2115+
SetStreamIdForTesting(stream, id)
21162116
_, err := stream.Do(req).Get()
21172117
require.NoError(t, err, "Failed to Ping")
21182118
})

0 commit comments

Comments
 (0)