Skip to content

Commit 1b62c35

Browse files
authored
refactor: add explicit ProtocolVersion type (#124)
### Changes are visible to end-users: no ### Test plan - Covered by existing test cases
1 parent 7dea6df commit 1b62c35

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

runner/pkg/ibp/protocol.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import (
1111
"github.com/fatih/color"
1212
)
1313

14-
const PROTOCOL_VERSION = 0
14+
type ProtocolVersion int
15+
16+
const (
17+
PROTOCOL_VERSION ProtocolVersion = 0
18+
)
19+
1520
const PROTOCOL_SOCKET_ENV = "ABAZEL_WATCH_SOCKET_FILE"
1621

1722
type IncrementalBazel interface {
@@ -25,7 +30,7 @@ type IncrementalBazel interface {
2530
Close() error
2631
HasConnection() bool
2732

28-
WaitForConnection() <-chan int
33+
WaitForConnection() <-chan ProtocolVersion
2934

3035
// The path/address a client can connect to.
3136
Address() string
@@ -40,11 +45,11 @@ type Message struct {
4045

4146
type negotiateMessage struct {
4247
Message
43-
Versions []int `json:"versions"`
48+
Versions []ProtocolVersion `json:"versions"`
4449
}
4550
type negotiateResponseMessage struct {
4651
Message
47-
Version int `json:"version"`
52+
Version ProtocolVersion `json:"version"`
4853
}
4954

5055
type capMessage struct {
@@ -77,15 +82,15 @@ type CycleSourcesMessage struct {
7782
}
7883

7984
// The versions supported by this host implementation of the protocol.
80-
var abazelSupportedProtocolVersions = []int{PROTOCOL_VERSION}
85+
var abazelSupportedProtocolVersions = []ProtocolVersion{PROTOCOL_VERSION}
8186

8287
type aspectBazelSocket = socket.Server[interface{}, map[string]any]
8388

8489
type aspectBazelProtocol struct {
8590
socket aspectBazelSocket
8691
socketPath string
8792

88-
connectedCh chan int
93+
connectedCh chan ProtocolVersion
8994

9095
// cycle_id is used to track the current cycle number.
9196
cycle_id atomic.Int32
@@ -99,11 +104,11 @@ func NewServer() IncrementalBazel {
99104
socketPath: socketPath,
100105
socket: socket.NewJsonServer[interface{}, map[string]interface{}](),
101106

102-
connectedCh: make(chan int, 1),
107+
connectedCh: make(chan ProtocolVersion, 1),
103108
}
104109
}
105110

106-
func (p *aspectBazelProtocol) WaitForConnection() <-chan int {
111+
func (p *aspectBazelProtocol) WaitForConnection() <-chan ProtocolVersion {
107112
return p.connectedCh
108113
}
109114

@@ -172,11 +177,11 @@ func (p *aspectBazelProtocol) acceptNegotiation() error {
172177
if negResp["version"] == nil {
173178
return fmt.Errorf("Received NEGOTIATE_RESPONSE without version: %v", negResp)
174179
}
175-
if negResp["version"].(float64) != PROTOCOL_VERSION {
176-
return fmt.Errorf("Received NEGOTIATE_RESPONSE with unsupported version %v, expected %d", negResp["version"], PROTOCOL_VERSION)
180+
if ProtocolVersion(negResp["version"].(float64)) != PROTOCOL_VERSION {
181+
return fmt.Errorf("Received NEGOTIATE_RESPONSE with unsupported version %v, expected %v", negResp["version"], PROTOCOL_VERSION)
177182
}
178183

179-
p.connectedCh <- int(negResp["version"].(float64))
184+
p.connectedCh <- ProtocolVersion(negResp["version"].(float64))
180185

181186
return nil
182187
}

0 commit comments

Comments
 (0)