From 5f0b2a8491def9660d5c49028d4ef015a0c78d93 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 3 Sep 2024 23:23:17 +0100 Subject: [PATCH] request: do not append '/j' in RawRequest() 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. --- request.go | 19 ++++++++++++------- request_test.go | 10 +++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/request.go b/request.go index dbcb321..3f2cf68 100644 --- a/request.go +++ b/request.go @@ -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) @@ -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 { @@ -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 { @@ -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 @@ -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 } @@ -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", diff --git a/request_test.go b/request_test.go index ccba442..4ea46ac 100644 --- a/request_test.go +++ b/request_test.go @@ -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) { @@ -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) {