diff options
| author | Shulhan <ms@kilabit.info> | 2023-09-10 21:27:22 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-09-11 02:16:52 +0700 |
| commit | 310ca99dd0b47e15428089028546fcfa1e0e02e7 (patch) | |
| tree | 1aa5ec46b562e8f183937347bc6252fbe9a48185 /lib/http | |
| parent | df58e21e4a8fa4fa308ec1b791867968972f57f3 (diff) | |
| download | pakakeh.go-310ca99dd0b47e15428089028546fcfa1e0e02e7.tar.xz | |
lib/http: realign struct for better size allocation
The realignment reduce the cost of the following struct,
* EndpointResponse: from 72 to 56 bytes (-16 bytes)
* RangePosition: from 48 to 24 bytes (-24 bytes)
* testCase in TestParseContentRange: from 24 to 16 bytes (-8 bytes)
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/endpoint_response.go | 3 | ||||
| -rw-r--r-- | lib/http/endpoint_response_example_test.go | 6 | ||||
| -rw-r--r-- | lib/http/range_position.go | 4 | ||||
| -rw-r--r-- | lib/http/range_position_test.go | 2 | ||||
| -rw-r--r-- | lib/http/server_test.go | 12 |
5 files changed, 14 insertions, 13 deletions
diff --git a/lib/http/endpoint_response.go b/lib/http/endpoint_response.go index ff16d6b9..6bf115d8 100644 --- a/lib/http/endpoint_response.go +++ b/lib/http/endpoint_response.go @@ -17,9 +17,10 @@ import liberrors "github.com/shuLhan/share/lib/errors" // // See the example below on how to use it with Endpoint.Call handler. type EndpointResponse struct { - liberrors.E Data interface{} `json:"data,omitempty"` + liberrors.E + // The Limit field contains the maximum number of records per page. Limit int64 `json:"limit,omitempty"` diff --git a/lib/http/endpoint_response_example_test.go b/lib/http/endpoint_response_example_test.go index 91829346..de34a11e 100644 --- a/lib/http/endpoint_response_example_test.go +++ b/lib/http/endpoint_response_example_test.go @@ -98,7 +98,7 @@ func ExampleEndpointResponse() { fmt.Printf("GET /?id=1000 => %s\n", resBody) // Output: - // GET / => {"code":400,"message":"empty parameter id"} - // GET /?id=0 => {"code":500,"message":"id value 0 cause internal server error"} - // GET /?id=1000 => {"code":200,"data":{"ID":"1000"}} + // GET / => {"message":"empty parameter id","code":400} + // GET /?id=0 => {"message":"id value 0 cause internal server error","code":500} + // GET /?id=1000 => {"data":{"ID":"1000"},"code":200} } diff --git a/lib/http/range_position.go b/lib/http/range_position.go index eaac5f91..279cdcb2 100644 --- a/lib/http/range_position.go +++ b/lib/http/range_position.go @@ -11,13 +11,13 @@ import ( type RangePosition struct { unit string + content []byte + Start int64 End int64 // Length of zero means read until the end. Length int64 - - content []byte } // ParseContentRange parse Content-Range value, the following format, diff --git a/lib/http/range_position_test.go b/lib/http/range_position_test.go index a6ca7ceb..1147b875 100644 --- a/lib/http/range_position_test.go +++ b/lib/http/range_position_test.go @@ -8,8 +8,8 @@ import ( func TestParseContentRange(t *testing.T) { type testCase struct { - v string exp *RangePosition + v string } var cases = []testCase{{ diff --git a/lib/http/server_test.go b/lib/http/server_test.go index bea01131..fd7330d6 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -757,32 +757,32 @@ func TestStatusError(t *testing.T) { desc: "With registered error no body", reqURL: testServerUrl + "/error/no-body?k=v", expStatusCode: http.StatusLengthRequired, - expBody: `{"code":411,"message":"Length required"}`, + expBody: `{"message":"Length required","code":411}`, }, { desc: "With registered error binary", reqURL: testServerUrl + "/error/binary?k=v", expStatusCode: http.StatusLengthRequired, - expBody: `{"code":411,"message":"Length required"}`, + expBody: `{"message":"Length required","code":411}`, }, { desc: "With registered error plain", reqURL: testServerUrl + "/error/plain?k=v", expStatusCode: http.StatusLengthRequired, - expBody: `{"code":411,"message":"Length required"}`, + expBody: `{"message":"Length required","code":411}`, }, { desc: "With registered error plain", reqURL: testServerUrl + "/error/json?k=v", expStatusCode: http.StatusLengthRequired, - expBody: `{"code":411,"message":"Length required"}`, + expBody: `{"message":"Length required","code":411}`, }, { desc: "With registered error plain", reqURL: testServerUrl + "/error/no-code?k=v", expStatusCode: http.StatusInternalServerError, - expBody: `{"code":500,"message":"internal server error","name":"ERR_INTERNAL"}`, + expBody: `{"message":"internal server error","name":"ERR_INTERNAL","code":500}`, }, { desc: "With registered error plain", reqURL: testServerUrl + "/error/custom?k=v", expStatusCode: http.StatusInternalServerError, - expBody: `{"code":500,"message":"internal server error","name":"ERR_INTERNAL"}`, + expBody: `{"message":"internal server error","name":"ERR_INTERNAL","code":500}`, }} for _, c := range cases { |
