Skip to content

Commit

Permalink
Merge pull request #36 from thiagokokada/refactor-prepare-requests
Browse files Browse the repository at this point in the history
request: refactor prepareRequests()
  • Loading branch information
thiagokokada authored Sep 4, 2024
2 parents cb10aac + 465fb90 commit 2636df5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func processEvent(ev EventHandler, msg ReceivedData, events []EventType) {
WorkspaceName: WorkspaceName(raw[1]),
})
case EventActiveWindow:
// e.g. jetbrains-goland,hyprland-ipc-ipc – main.go
// e.g. nvim,nvim event/event.go
ev.ActiveWindow(ActiveWindow{
Name: raw[0],
Title: raw[1],
Expand Down
51 changes: 28 additions & 23 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const (
bufSize = 8192
)

var reqHeader = []byte{'j', '/'}
var jsonReqHeader = []byte{'j', '/'}
var reqSep = []byte{' ', ';'}

func prepareRequest(buf *bytes.Buffer, command string, param string, jsonResp bool) int {
if jsonResp {
buf.Write(reqHeader)
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
buf.WriteByte(reqSep[0])
Expand All @@ -36,7 +36,6 @@ func prepareRequest(buf *bytes.Buffer, command string, param string, jsonResp bo
return buf.Len()
}

// TODO: needs refactor, the logic is all over the place
func prepareRequests(command string, params []string, jsonResp bool) (requests []RawRequest, err error) {
if command == "" {
// Panic since this is not supposed to happen, i.e.: only by
Expand All @@ -46,25 +45,35 @@ func prepareRequests(command string, params []string, jsonResp bool) (requests [

// Buffer that will store the temporary prepared request
buf := bytes.NewBuffer(nil)
bufErr := func() error {
return fmt.Errorf(
"command is too long (%d>=%d): %s",
buf.Len(),
bufSize,
buf.String(),
)
}

switch len(params) {
case 0, 1:
case 0:
if jsonResp {
buf.Write(reqHeader)
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
if len(params) == 1 {
buf.WriteByte(reqSep[0])
buf.WriteString(params[0])

if buf.Len() > bufSize {
return nil, bufErr()
}
case 1:
if jsonResp {
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
buf.WriteByte(reqSep[0])
buf.WriteString(params[0])

if buf.Len() > bufSize {
return nil, fmt.Errorf(
"command is too long (%d>=%d): %s",
buf.Len(),
bufSize,
buf.String(),
)
return nil, bufErr()
}
default:
// Add [[BATCH]] to the buffer
Expand All @@ -77,20 +86,15 @@ func prepareRequests(command string, params []string, jsonResp bool) (requests [
// header and separators
cmdLen := len(command) + len(param) + len(reqSep)
if jsonResp {
cmdLen += len(reqHeader)
cmdLen += len(jsonReqHeader)
}

// If batch + command length is bigger than bufSize,
// return an error since it will not fit the socket
if len(batch)+cmdLen > bufSize {
return nil, fmt.Errorf(
"command is too long (%d>=%d): %s%s %s;",
len(batch)+cmdLen,
bufSize,
batch,
command,
param,
)
// Call prepare request for error
prepareRequest(buf, command, param, jsonResp)
return nil, bufErr()
}

// If the current length of the buffer + command +
Expand All @@ -105,6 +109,7 @@ func prepareRequests(command string, params []string, jsonResp bool) (requests [
buf.Reset()
buf.WriteString(batch)
}

// Add the contents of the request to the buffer
curLen = prepareRequest(buf, command, param, jsonResp)
}
Expand Down
12 changes: 6 additions & 6 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func TestPrepareRequestsError(t *testing.T) {
paramsLen int
jsonResp bool
}{
{bufSize - len(reqHeader), 0, true}, // '/j<command>'
{bufSize - len(reqHeader) - 2, 1, true}, // '/j<command> p'
{bufSize - len(reqHeader) - len(batch) - len(reqSep) - 1, 2, true}, // '[[BATCH]]/j<command> p;'
{bufSize, 0, false}, // '<command>'
{bufSize - 2, 1, false}, // '<command> p'
{bufSize - len(batch) - len(reqSep) - 1, 2, false}, // '[[BATCH]]<command> p;'
{bufSize - len(jsonReqHeader), 0, true}, // '/j<command>'
{bufSize - len(jsonReqHeader) - 2, 1, true}, // '/j<command> p'
{bufSize - len(jsonReqHeader) - len(batch) - len(reqSep) - 1, 2, true}, // '[[BATCH]]/j<command> p;'
{bufSize, 0, false}, // '<command>'
{bufSize - 2, 1, false}, // '<command> p'
{bufSize - len(batch) - len(reqSep) - 1, 2, false}, // '[[BATCH]]<command> p;'
}
for _, tt := range tests {
t.Run(fmt.Sprintf("tests_%d-%d-%v", tt.lastSafeLen, tt.paramsLen, tt.jsonResp), func(t *testing.T) {
Expand Down

0 comments on commit 2636df5

Please sign in to comment.