Skip to content

Commit

Permalink
request: do not append '/j' in RawRequest()
Browse files Browse the repository at this point in the history
This change moves the prefix of '/j' of the `RawRequest()` to
`prepareRequests()` function instead. The prefix is used in Hyprland to
indicate that the request needs to return JSON format, and while we
always wants JSON format as response, someone using `RawRequest()`
directly may not want to do so.

While this is a breaking change for users of `RawRequest()`, the impact
should be minimal and will allow for more flexibility in future.

This also fixes a bug in batch mode, where we need to append '/j' in
every command, not just the first one. It didn't affect the e.g.
`Dispatch()` since it doesn't have a JSON output anyway, but it could
affect something in the future.
  • Loading branch information
thiagokokada committed Sep 3, 2024
1 parent 482f29b commit 5f0b2a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
19 changes: 12 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var reqHeader = []byte{'j', '/'}
var reqSep = []byte{' ', ';'}

func prepareRequest(buf *bytes.Buffer, command string, param string) int {
buf.Write(reqHeader)
buf.WriteString(command)
buf.WriteByte(reqSep[0])
buf.WriteString(param)
Expand All @@ -39,6 +40,10 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
// misuse since this function is internal
panic("empty command")
}

// Buffer that will store the temporary prepared request
buf := bytes.NewBuffer(nil)

switch len(params) {
case 0:
if len(command)+len(reqHeader) > bufSize {
Expand All @@ -49,7 +54,8 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
command,
)
}
requests = append(requests, []byte(command))
buf.Write(reqHeader)
buf.WriteString(command)
case 1:
request := command + " " + params[0]
if len(request)+len(reqHeader) > bufSize {
Expand All @@ -60,10 +66,9 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
request,
)
}
requests = append(requests, []byte(request))
buf.Write(reqHeader)
buf.WriteString(request)
default:
buf := bytes.NewBuffer(nil)

// Add [[BATCH]] to the buffer
buf.WriteString(batch)
// Initialise current length of buffer
Expand Down Expand Up @@ -102,9 +107,10 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
// Add the contents of the request to the buffer
curLen = prepareRequest(buf, command, param)
}
// Append any remaining buffer content to requests array
requests = append(requests, buf.Bytes())
}
// Append any remaining buffer content to requests array
requests = append(requests, buf.Bytes())

return requests, nil
}

Expand Down Expand Up @@ -253,7 +259,6 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
}

// Send the request to the socket
request = append(reqHeader, request...)
if len(request) > bufSize {
return nil, fmt.Errorf(
"request too big (%d>%d): %s",
Expand Down
10 changes: 5 additions & 5 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func TestPrepareRequests(t *testing.T) {
params []string
expected []string
}{
{"command", nil, []string{"command"}},
{"command", []string{"param0"}, []string{"command param0"}},
{"command", []string{"param0", "param1"}, []string{"[[BATCH]]command param0;command param1;"}},
{"command", nil, []string{"j/command"}},
{"command", []string{"param0"}, []string{"j/command param0"}},
{"command", []string{"param0", "param1"}, []string{"[[BATCH]]j/command param0;j/command param1;"}},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("tests_%s-%s", tt.command, tt.params), func(t *testing.T) {
Expand All @@ -102,8 +102,8 @@ func TestPrepareRequestsMass(t *testing.T) {
{"command", genParams("very big param list", 100), 1},
{"command", genParams("very big param list", 500), 2},
{"command", genParams("very big param list", 1000), 4},
{"command", genParams("very big param list", 5000), 18},
{"command", genParams("very big param list", 10000), 35},
{"command", genParams("very big param list", 5000), 19},
{"command", genParams("very big param list", 10000), 37},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("mass_tests_%s-%d", tt.command, len(tt.params)), func(t *testing.T) {
Expand Down

0 comments on commit 5f0b2a8

Please sign in to comment.