From 04b7061e7c37199ad71ae836574d8335870a7878 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 26 Nov 2024 01:25:44 +0300 Subject: [PATCH 1/5] [req] Improvements --- CHANGELOG.md | 7 ++ mathutil/mathutil.go | 4 + req/query.go | 241 +++++++++++++++++++++++++++++++++++++++++++ req/req.go | 46 +-------- req/req_test.go | 208 +++++++++++++++++++++---------------- 5 files changed, 374 insertions(+), 132 deletions(-) create mode 100644 req/query.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 177cc10c..4731e82d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ ## Changelog +### [13.13.0](https://kaos.sh/ek/13.13.0) + +* `[req]` Added support for different types of slices to `Query` +* `[req]` Added support for `fmt.Stringer` interface to `Query` +* `[req]` Added interface for custom struct encoding for `Query` +* `[req]` Improved `Query`encoding + ### [13.12.0](https://kaos.sh/ek/13.12.0) * `[req]` Added custom timeout per request diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 21f6082c..4a3b2031 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -19,6 +19,10 @@ type Integer interface { ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 } +type Float interface { + ~float32 | ~float64 +} + type Numeric interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | diff --git a/req/query.go b/req/query.go new file mode 100644 index 00000000..2f77fd4c --- /dev/null +++ b/req/query.go @@ -0,0 +1,241 @@ +package req + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "bytes" + "fmt" + "net/url" + + "github.com/essentialkaos/ek/v13/mathutil" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +// Query is a map[string]any used for query +type Query map[string]any + +// ////////////////////////////////////////////////////////////////////////////////// // + +// QueryPayload is an interface for query payload with custom encoder +type QueryPayload interface { + // ToQuery encodes payload for using in query string + ToQuery(name string) string +} + +// ////////////////////////////////////////////////////////////////////////////////// // + +// Encode converts query struct to URL-encoded string +func (q Query) Encode() string { + var buf bytes.Buffer + + for k, v := range q { + if k == "" { + continue + } + + buf.WriteString(url.QueryEscape(k)) + + switch u := v.(type) { + case string: + if u != "" { + buf.WriteRune('=') + buf.WriteString(queryFormatString(u)) + } + + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + buf.WriteRune('=') + buf.WriteString(queryFormatNumber(v)) + + case float32, float64: + buf.WriteRune('=') + buf.WriteString(queryFormatFloat(v)) + + case nil: + // noop + + case fmt.Stringer: + vv := url.QueryEscape(u.String()) + + if vv != "" { + buf.WriteRune('=') + buf.WriteString(vv) + } + + case QueryPayload: + vv := url.QueryEscape(u.ToQuery(k)) + + if vv != "" { + buf.WriteRune('=') + buf.WriteString(vv) + } + + case []string: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatStringSlice(&buf, u) + } + + case []fmt.Stringer: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatStringerSlice(&buf, u) + } + + case []int: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []int8: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []int16: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []int32: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []int64: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []uint: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []uint8: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []uint16: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []uint32: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []uint64: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatNumSlice(&buf, u) + } + + case []float32: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatFloatSlice(&buf, u) + } + + case []float64: + if len(u) > 0 { + buf.WriteRune('=') + queryFormatFloatSlice(&buf, u) + } + + default: + buf.WriteRune('=') + buf.WriteString(url.QueryEscape(fmt.Sprintf("%v", v))) + } + + buf.WriteRune('&') + } + + if buf.Len() == 0 { + return "" + } + + buf.Truncate(buf.Len() - 1) + + return buf.String() +} + +// ////////////////////////////////////////////////////////////////////////////////// // + +func queryFormatString(v string) string { + return url.QueryEscape(v) +} + +func queryFormatNumber(v any) string { + return fmt.Sprintf("%d", v) +} + +func queryFormatFloat(v any) string { + return fmt.Sprintf("%f", v) +} + +func queryFormatStringSlice(buf *bytes.Buffer, v []string) { + l := buf.Len() + + for _, vv := range v { + if vv != "" { + buf.WriteString(queryFormatString(vv)) + buf.WriteRune(',') + } + } + + if l != buf.Len() { + buf.Truncate(buf.Len() - 1) + } +} + +func queryFormatStringerSlice(buf *bytes.Buffer, v []fmt.Stringer) { + l := buf.Len() + + for _, vv := range v { + vvv := vv.String() + + if vvv != "" { + buf.WriteString(queryFormatString(vvv)) + buf.WriteRune(',') + } + } + + if l != buf.Len() { + buf.Truncate(buf.Len() - 1) + } +} + +func queryFormatNumSlice[T mathutil.Integer](buf *bytes.Buffer, v []T) { + for _, vv := range v { + buf.WriteString(queryFormatNumber(vv)) + buf.WriteRune(',') + } + + buf.Truncate(buf.Len() - 1) +} + +func queryFormatFloatSlice[T mathutil.Float](buf *bytes.Buffer, v []T) { + for _, vv := range v { + buf.WriteString(queryFormatFloat(vv)) + buf.WriteRune(',') + } + + buf.Truncate(buf.Len() - 1) +} diff --git a/req/req.go b/req/req.go index b1a6c394..d30e9518 100644 --- a/req/req.go +++ b/req/req.go @@ -17,7 +17,6 @@ import ( "mime/multipart" "net" "net/http" - "net/url" "os" "path/filepath" "runtime" @@ -182,9 +181,6 @@ const USER_AGENT = "ek.go" // ////////////////////////////////////////////////////////////////////////////////// // -// Query is a map[string]any used for query -type Query map[string]any - // Headers is a map[string]string used for headers type Headers map[string]string @@ -541,44 +537,6 @@ func (e RequestError) Error() string { // ////////////////////////////////////////////////////////////////////////////////// // -// Encode converts query struct to URL-encoded string -func (q Query) Encode() string { - var result strings.Builder - - for k, v := range q { - switch u := v.(type) { - case string: - if v == "" { - result.WriteString(k) - } else { - result.WriteString(k) - result.WriteRune('=') - result.WriteString(url.QueryEscape(u)) - } - case nil: - result.WriteString(k) - default: - result.WriteString(k) - result.WriteRune('=') - result.WriteString(url.QueryEscape(fmt.Sprintf("%v", v))) - } - - result.WriteRune('&') - } - - if result.Len() == 0 { - return "" - } - - return result.String()[:result.Len()-1] -} - -// ////////////////////////////////////////////////////////////////////////////////// // - -// This method has a lot of actions to prepare request for executing, so it is ok to -// have so many conditions -// codebeat:disable[CYCLO,ABC] - func (e *Engine) doRequest(r Request, method string) (*Response, error) { // Lazy engine initialization if e != nil && !e.initialized { @@ -605,7 +563,7 @@ func (e *Engine) doRequest(r Request, method string) (*Response, error) { r.Method = GET } - if r.Query != nil && len(r.Query) != 0 { + if len(r.Query) != 0 { r.URL += "?" + r.Query.Encode() } @@ -648,8 +606,6 @@ func (e *Engine) doRequest(r Request, method string) (*Response, error) { return result, nil } -// codebeat:enable[CYCLO,ABC] - // ////////////////////////////////////////////////////////////////////////////////// // func checkRequest(r Request) error { diff --git a/req/req_test.go b/req/req_test.go index 0c5e9b7f..050c2872 100644 --- a/req/req_test.go +++ b/req/req_test.go @@ -10,9 +10,11 @@ package req import ( "bytes" "errors" + "fmt" "io" "net" "net/http" + "net/url" "os" "sort" "strings" @@ -56,6 +58,11 @@ const ( // ////////////////////////////////////////////////////////////////////////////////// // +type TestStringer struct{} +type TestPayload struct{} + +// ////////////////////////////////////////////////////////////////////////////////// // + func Test(t *testing.T) { TestingT(t) } type ReqSuite struct { @@ -263,35 +270,6 @@ func (s *ReqSuite) TestMethodDelete(c *C) { c.Assert(deleteResp.StatusCode, Equals, 200) } -func (s *ReqSuite) TestQuery(c *C) { - resp, err := Request{ - URL: s.url + _URL_QUERY, - Query: Query{ - "test01": "john", - "test02": 1398, - "test03": true, - "test04": false, - "test05": int(1), - "test06": int8(2), - "test07": int16(3), - "test08": int32(4), - "test09": int64(5), - "test10": uint(6), - "test11": uint8(7), - "test12": uint16(8), - "test13": uint32(9), - "test14": uint64(10), - "test15": float32(12.35), - "test16": float64(56.7895), - "test17": "", - "test18": nil, - }, - }.Do() - - c.Assert(err, IsNil) - c.Assert(resp.StatusCode, Equals, 200) -} - func (s *ReqSuite) TestHeaders(c *C) { resp, err := Request{ URL: s.url + _URL_HEADERS, @@ -682,6 +660,108 @@ func (s *ReqSuite) TestNil(c *C) { c.Assert(r.String(), Equals, "") } +func (s *ReqSuite) TestQuery(c *C) { + resp, err := Request{ + URL: s.url + _URL_QUERY, + Query: Query{ + "user": "john", + "id": 1000, + }, + }.Do() + + c.Assert(err, IsNil) + c.Assert(resp.StatusCode, Equals, 200) +} + +func (s *ReqSuite) TestQueryParsing(c *C) { + var q Query + c.Assert(q.Encode(), Equals, "") + + q = nil + c.Assert(q.Encode(), Equals, "") + + ts := &TestStringer{} + tp := &TestPayload{} + + q = Query{ + "": "test", + "test01": nil, + "test02": "Test 1234", + "test03": true, + "test04": false, + "test05": int(1), + "test06": int8(2), + "test07": int16(3), + "test08": int32(4), + "test09": int64(5), + "test10": uint(6), + "test11": uint8(7), + "test12": uint16(8), + "test13": uint32(9), + "test14": uint64(10), + "test15": float32(12.35), + "test16": float64(56.7895), + "test17": "", + "test18": ts, + "test19": tp, + "test20": []string{"abcd", "1234"}, + "test21": []fmt.Stringer{ts, ts}, + "test22": []int{0, 1, 2}, + "test23": []int8{0, 1, 2}, + "test24": []int16{0, 1, 2}, + "test25": []int32{0, 1, 2}, + "test26": []int64{0, 1, 2}, + "test27": []uint{0, 1, 2}, + "test28": []uint8{0, 1, 2}, + "test29": []uint16{0, 1, 2}, + "test30": []uint32{0, 1, 2}, + "test31": []uint64{0, 1, 2}, + "test32": []float32{0, 1, 2}, + "test33": []float64{0, 1, 2}, + } + + nq, err := url.ParseQuery(q.Encode()) + c.Assert(err, IsNil) + c.Assert(nq, NotNil) + + c.Assert(nq.Get(""), Equals, "") + c.Assert(nq.Has("test01"), Equals, true) + c.Assert(nq.Get("test01"), Equals, "") + c.Assert(nq.Get("test02"), Equals, "Test 1234") + c.Assert(nq.Get("test03"), Equals, "true") + c.Assert(nq.Get("test04"), Equals, "false") + c.Assert(nq.Get("test05"), Equals, "1") + c.Assert(nq.Get("test06"), Equals, "2") + c.Assert(nq.Get("test07"), Equals, "3") + c.Assert(nq.Get("test08"), Equals, "4") + c.Assert(nq.Get("test09"), Equals, "5") + c.Assert(nq.Get("test10"), Equals, "6") + c.Assert(nq.Get("test11"), Equals, "7") + c.Assert(nq.Get("test12"), Equals, "8") + c.Assert(nq.Get("test13"), Equals, "9") + c.Assert(nq.Get("test14"), Equals, "10") + c.Assert(nq.Get("test15"), Equals, "12.350000") + c.Assert(nq.Get("test16"), Equals, "56.789500") + c.Assert(nq.Has("test17"), Equals, true) + c.Assert(nq.Get("test17"), Equals, "") + c.Assert(nq.Get("test18"), Equals, "test") + c.Assert(nq.Get("test19"), Equals, "test") + c.Assert(nq.Get("test20"), Equals, "abcd,1234") + c.Assert(nq.Get("test21"), Equals, "test,test") + c.Assert(nq.Get("test22"), Equals, "0,1,2") + c.Assert(nq.Get("test23"), Equals, "0,1,2") + c.Assert(nq.Get("test24"), Equals, "0,1,2") + c.Assert(nq.Get("test25"), Equals, "0,1,2") + c.Assert(nq.Get("test26"), Equals, "0,1,2") + c.Assert(nq.Get("test27"), Equals, "0,1,2") + c.Assert(nq.Get("test28"), Equals, "0,1,2") + c.Assert(nq.Get("test29"), Equals, "0,1,2") + c.Assert(nq.Get("test30"), Equals, "0,1,2") + c.Assert(nq.Get("test31"), Equals, "0,1,2") + c.Assert(nq.Get("test32"), Equals, "0.000000,1.000000,2.000000") + c.Assert(nq.Get("test33"), Equals, "0.000000,1.000000,2.000000") +} + // ////////////////////////////////////////////////////////////////////////////////// // func (s *ReqSuite) BenchmarkQueryEncoding(c *C) { @@ -834,68 +914,12 @@ func queryRequestHandler(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() switch { - case query.Get("test01") != "john": + case query.Get("user") != "john": w.WriteHeader(901) return - case query.Get("test02") != "1398": + case query.Get("id") != "1000": w.WriteHeader(902) return - case query.Get("test03") != "true": - w.WriteHeader(903) - return - case query.Get("test04") != "false": - w.WriteHeader(904) - return - case query.Get("test05") != "1": - w.WriteHeader(905) - return - case query.Get("test06") != "2": - w.WriteHeader(906) - return - case query.Get("test07") != "3": - w.WriteHeader(907) - return - case query.Get("test08") != "4": - w.WriteHeader(908) - return - case query.Get("test09") != "5": - w.WriteHeader(909) - return - case query.Get("test10") != "6": - w.WriteHeader(910) - return - case query.Get("test11") != "7": - w.WriteHeader(911) - return - case query.Get("test12") != "8": - w.WriteHeader(912) - return - case query.Get("test13") != "9": - w.WriteHeader(913) - return - case query.Get("test14") != "10": - w.WriteHeader(914) - return - case query.Get("test15") != "12.35": - w.WriteHeader(915) - return - case query.Get("test16") != "56.7895": - w.WriteHeader(916) - return - } - - _, test17 := query["test17"] - - if !test17 { - w.WriteHeader(917) - return - } - - _, test18 := query["test18"] - - if !test18 { - w.WriteHeader(918) - return } w.WriteHeader(200) @@ -1029,3 +1053,13 @@ func timeoutRequestHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Write([]byte(`{}`)) } + +// ////////////////////////////////////////////////////////////////////////////////// // + +func (t *TestStringer) String() string { + return "test" +} + +func (t *TestPayload) ToQuery(name string) string { + return "test" +} From 64af50cbfabc728472126ec27d67a9b855f2d8ba Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 26 Nov 2024 01:26:25 +0300 Subject: [PATCH 2/5] Bump version --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index a4588fab..50dde6ee 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.12.0" +const VERSION = "13.13.0" From 4d39d9598d1b1892ddd1503fd08f0e47388f1bd6 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 26 Nov 2024 11:52:26 +0300 Subject: [PATCH 3/5] [req] Better floats encoding in 'Query' --- req/query.go | 3 ++- req/req_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/req/query.go b/req/query.go index 2f77fd4c..aeafe7ea 100644 --- a/req/query.go +++ b/req/query.go @@ -11,6 +11,7 @@ import ( "bytes" "fmt" "net/url" + "strings" "github.com/essentialkaos/ek/v13/mathutil" ) @@ -187,7 +188,7 @@ func queryFormatNumber(v any) string { } func queryFormatFloat(v any) string { - return fmt.Sprintf("%f", v) + return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", v), "0"), ".") } func queryFormatStringSlice(buf *bytes.Buffer, v []string) { diff --git a/req/req_test.go b/req/req_test.go index 050c2872..1dbd433d 100644 --- a/req/req_test.go +++ b/req/req_test.go @@ -716,8 +716,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) { "test29": []uint16{0, 1, 2}, "test30": []uint32{0, 1, 2}, "test31": []uint64{0, 1, 2}, - "test32": []float32{0, 1, 2}, - "test33": []float64{0, 1, 2}, + "test32": []float32{0.01, 1.0, 2.231213}, + "test33": []float64{0.01, 1.0, 2.231213}, } nq, err := url.ParseQuery(q.Encode()) @@ -740,8 +740,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) { c.Assert(nq.Get("test12"), Equals, "8") c.Assert(nq.Get("test13"), Equals, "9") c.Assert(nq.Get("test14"), Equals, "10") - c.Assert(nq.Get("test15"), Equals, "12.350000") - c.Assert(nq.Get("test16"), Equals, "56.789500") + c.Assert(nq.Get("test15"), Equals, "12.35") + c.Assert(nq.Get("test16"), Equals, "56.7895") c.Assert(nq.Has("test17"), Equals, true) c.Assert(nq.Get("test17"), Equals, "") c.Assert(nq.Get("test18"), Equals, "test") @@ -758,8 +758,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) { c.Assert(nq.Get("test29"), Equals, "0,1,2") c.Assert(nq.Get("test30"), Equals, "0,1,2") c.Assert(nq.Get("test31"), Equals, "0,1,2") - c.Assert(nq.Get("test32"), Equals, "0.000000,1.000000,2.000000") - c.Assert(nq.Get("test33"), Equals, "0.000000,1.000000,2.000000") + c.Assert(nq.Get("test32"), Equals, "0.01,1,2.231213") + c.Assert(nq.Get("test33"), Equals, "0.01,1,2.231213") } // ////////////////////////////////////////////////////////////////////////////////// // From e325accb363e787dfa54933b489dd5c8b2e48dba Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 26 Nov 2024 12:05:59 +0300 Subject: [PATCH 4/5] Improve README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ea321b2a..24bccf83 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ Currently we support Linux and macOS (_except some packages_). All packages have ### Projects with `EK` +_It's hard to show all the possibilities of all the subpackages. But you can take a look at these open source projects that use almost all the features of this package in real-world tasks._ + - [aligo](https://kaos.sh/aligo) — Utility for checking and viewing Golang struct alignment info - [artefactor](https://kaos.sh/artefactor) — Utility for downloading artefacts from GitHub - [atlassian-cloud-backuper](https://kaos.sh/atlassian-cloud-backuper) — Tool for backuping Atlassian cloud services (_Jira and Confluence_) From 39102ca0713ddc9b9d37ac13b8b42fcebf395300 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 26 Nov 2024 12:23:54 +0300 Subject: [PATCH 5/5] Remove codebeat tags --- cron/cron.go | 11 ----------- fmtc/fmtc.go | 4 ---- fsutil/list.go | 5 ----- options/options.go | 7 ------- signal/signal.go | 4 ---- spellcheck/spellcheck.go | 5 ----- system/info_cpu_linux.go | 9 --------- system/info_fs_linux.go | 4 ---- system/info_loadavg_linux.go | 4 ---- system/info_memory_linux.go | 4 ---- system/info_net_linux.go | 4 ---- system/process/process_cpu.go | 8 -------- system/process/process_memory.go | 4 ---- system/process/process_mount.go | 4 ---- timeutil/timeutil.go | 10 ---------- 15 files changed, 87 deletions(-) diff --git a/cron/cron.go b/cron/cron.go index 332070f5..f65608f6 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -88,8 +88,6 @@ var info = []exprInfo{ // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // Parse parse cron expression // https://en.wikipedia.org/wiki/Cron func Parse(expr string) (*Expr, error) { @@ -142,12 +140,8 @@ func Parse(expr string) (*Expr, error) { return result, nil } -// codebeat:enable[LOC,ABC] - // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC] - // IsDue check if current moment is match for expression func (e *Expr) IsDue(args ...time.Time) bool { if e == nil { @@ -185,9 +179,6 @@ func (e *Expr) IsDue(args ...time.Time) bool { return true } -// I don't have an idea how we can implement this without this conditions -// codebeat:disable[BLOCK_NESTING,LOC,CYCLO] - // Next get time of next matched moment func (e *Expr) Next(args ...time.Time) time.Time { if e == nil { @@ -308,8 +299,6 @@ func (e *Expr) Prev(args ...time.Time) time.Time { return time.Unix(0, 0) } -// codebeat:enable[BLOCK_NESTING,LOC,CYCLO] - // String return raw expression func (e *Expr) String() string { if e == nil { diff --git a/fmtc/fmtc.go b/fmtc/fmtc.go index 3779fc61..5eee0474 100644 --- a/fmtc/fmtc.go +++ b/fmtc/fmtc.go @@ -381,8 +381,6 @@ func IsTag(tag string) bool { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,BLOCK_NESTING] - func tag2ANSI(tag string, clean bool) string { switch { case clean: @@ -449,8 +447,6 @@ func tag2ANSI(tag string, clean bool) string { return fmt.Sprintf("\033[" + chars[:len(chars)-1] + "m") } -// codebeat:enable[LOC,BLOCK_NESTING] - func parseExtendedColor(tag string) string { if len(tag) == 7 { hex := strings.TrimLeft(tag, "#%") diff --git a/fsutil/list.go b/fsutil/list.go index 8d0963e2..95186af9 100644 --- a/fsutil/list.go +++ b/fsutil/list.go @@ -266,9 +266,6 @@ func readDirRecFiles(path, base string, ignoreHidden bool, filter ListingFilter) return result } -// It's ok to have long function with many conditions to filter some entities -// codebeat:disable[LOC,ABC,CYCLO] - func isMatch(name, fullPath string, filter ListingFilter) bool { var ( hasNotMatchPatterns = filter.hasNotMatchPatterns() @@ -379,8 +376,6 @@ func isMatch(name, fullPath string, filter ListingFilter) bool { return match } -// codebeat:enable[LOC,ABC,CYCLO] - func filterList(names []string, dir string, filter ListingFilter) []string { var filteredNames []string diff --git a/options/options.go b/options/options.go index 899edf5e..a88bd8ab 100644 --- a/options/options.go +++ b/options/options.go @@ -673,11 +673,6 @@ func (o optionName) String() string { // ////////////////////////////////////////////////////////////////////////////////// // -// I think it is okay to have such a long and complicated method for parsing data -// because it has a lot of logic which can't be separated into different methods -// without losing code readability -// codebeat:disable[LOC,BLOCK_NESTING,CYCLO] - func (o *Options) parseOptions(rawOpts []string) (Arguments, errors.Errors) { o.prepare() @@ -784,8 +779,6 @@ func (o *Options) parseOptions(rawOpts []string) (Arguments, errors.Errors) { return arguments, errs } -// codebeat:enable[LOC,BLOCK_NESTING,CYCLO] - func (o *Options) parseLongOption(opt string) (string, string, error) { if strings.Contains(opt, "=") { optName, optValue, ok := strings.Cut(opt, "=") diff --git a/signal/signal.go b/signal/signal.go index 210aadd3..b531cd29 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -113,8 +113,6 @@ func (h Handlers) TrackAsync() { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // GetByName returns signal with given name func GetByName(name string) (syscall.Signal, error) { switch strings.ToUpper(name) { @@ -245,6 +243,4 @@ func GetByCode(code int) (syscall.Signal, error) { return syscall.Signal(-1), fmt.Errorf("Unknown signal code %d", code) } -// codebeat:enable[LOC,ABC] - // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/spellcheck/spellcheck.go b/spellcheck/spellcheck.go index df483c41..63a133c9 100644 --- a/spellcheck/spellcheck.go +++ b/spellcheck/spellcheck.go @@ -122,9 +122,6 @@ func (m *Model) Suggest(word string, max int) []string { // ////////////////////////////////////////////////////////////////////////////////// // -// I don't have an idea how we could separate this method -// codebeat:disable[LOC,ABC,CYCLO] - // Damerau–Levenshtein distance algorithm and code func getDLDistance(source, target string) int { sl, tl := len(source), len(target) @@ -187,8 +184,6 @@ func getDLDistance(source, target string) int { return h[sl+1][tl+1] } -// codebeat:enable[LOC,ABC,CYCLO] - func getSuggestSlice(terms []string, word string) suggestItems { var result suggestItems diff --git a/system/info_cpu_linux.go b/system/info_cpu_linux.go index db6cb881..a511b8a2 100644 --- a/system/info_cpu_linux.go +++ b/system/info_cpu_linux.go @@ -55,9 +55,6 @@ func GetCPUUsage(duration time.Duration) (*CPUUsage, error) { return CalculateCPUUsage(c1, c2), nil } -// It's ok to have so complex method for calculation -// codebeat:disable[CYCLO] - // CalculateCPUUsage calculates CPU usage based on CPUStats func CalculateCPUUsage(c1, c2 *CPUStats) *CPUUsage { prevIdle := c1.Idle + c1.Wait @@ -84,8 +81,6 @@ func CalculateCPUUsage(c1, c2 *CPUStats) *CPUUsage { } } -// codebeat:enable[CYCLO] - // GetCPUStats returns basic CPU stats func GetCPUStats() (*CPUStats, error) { s, closer, err := getFileScanner(procStatFile) @@ -148,8 +143,6 @@ func GetCPUCount() (CPUCount, error) { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC,CYCLO] - // parseCPUStats parses cpu stats data func parseCPUStats(s *bufio.Scanner) (*CPUStats, error) { var err error @@ -288,8 +281,6 @@ func parseCPUInfo(s *bufio.Scanner) ([]*CPUInfo, error) { return info, nil } -// codebeat:enable[LOC,ABC,CYCLO] - // parseCPUCountInfo parses CPU count data func parseCPUCountInfo(data string) uint32 { startNum := strings.Trim(strutil.ReadField(data, 0, false, '-'), "\n\r") diff --git a/system/info_fs_linux.go b/system/info_fs_linux.go index 5eadc638..5431f261 100644 --- a/system/info_fs_linux.go +++ b/system/info_fs_linux.go @@ -105,8 +105,6 @@ func CalculateIOUtil(io1, io2 map[string]*IOStats, duration time.Duration) map[s // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC,CYCLO] - // parseIOStats parses IO stats data func parseIOStats(s *bufio.Scanner) (map[string]*IOStats, error) { var err error @@ -249,8 +247,6 @@ func parseFSInfo(s *bufio.Scanner, calculateStats bool) (map[string]*FSUsage, er return info, nil } -// enable:disable[LOC,ABC,CYCLO] - // getHZ returns number of processor clock ticks per second func getHZ() float64 { // CLK_TCK is a constant on Linux diff --git a/system/info_loadavg_linux.go b/system/info_loadavg_linux.go index 4976e37e..5583a860 100644 --- a/system/info_loadavg_linux.go +++ b/system/info_loadavg_linux.go @@ -46,8 +46,6 @@ func GetLA() (*LoadAvg, error) { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // parseLAInfo parses loadavg data func parseLAInfo(text string) (*LoadAvg, error) { var err error @@ -97,5 +95,3 @@ func parseLAInfo(text string) (*LoadAvg, error) { return la, nil } - -// codebeat:enable[LOC,ABC] diff --git a/system/info_memory_linux.go b/system/info_memory_linux.go index b2594225..96dbe0e9 100644 --- a/system/info_memory_linux.go +++ b/system/info_memory_linux.go @@ -36,8 +36,6 @@ func GetMemUsage() (*MemUsage, error) { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // parseMemUsage parses memory usage info func parseMemUsage(s *bufio.Scanner) (*MemUsage, error) { var err error @@ -91,5 +89,3 @@ func parseMemUsage(s *bufio.Scanner) (*MemUsage, error) { return mem, nil } - -// codebeat:enable[LOC,ABC] diff --git a/system/info_net_linux.go b/system/info_net_linux.go index 632fe8ca..a8d6802c 100644 --- a/system/info_net_linux.go +++ b/system/info_net_linux.go @@ -80,8 +80,6 @@ func CalculateNetworkSpeed(ii1, ii2 map[string]*InterfaceStats, duration time.Du // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // parseInterfacesStats parses interfaces stats data func parseInterfacesStats(s *bufio.Scanner) (map[string]*InterfaceStats, error) { var err error @@ -133,8 +131,6 @@ func parseInterfacesStats(s *bufio.Scanner) (map[string]*InterfaceStats, error) return stats, nil } -// codebeat:enable[LOC,ABC] - // getActiveInterfacesBytes calculate received and transmitted bytes on all interfaces func getActiveInterfacesBytes(is map[string]*InterfaceStats) (uint64, uint64) { var ( diff --git a/system/process/process_cpu.go b/system/process/process_cpu.go index 18ab05ab..3ac31ea2 100644 --- a/system/process/process_cpu.go +++ b/system/process/process_cpu.go @@ -88,8 +88,6 @@ func GetInfo(pid int) (*ProcInfo, error) { return parseStatData(text) } -// codebeat:disable[LOC,ABC] - // GetSample returns ProcSample for CPU usage calculation func GetSample(pid int) (ProcSample, error) { fd, err := os.OpenFile(procFS+"/"+strconv.Itoa(pid)+"/stat", os.O_RDONLY, 0) @@ -110,8 +108,6 @@ func GetSample(pid int) (ProcSample, error) { return parseSampleData(text) } -// codebeat:enable[LOC,ABC] - // CalculateCPUUsage calculates CPU usage func CalculateCPUUsage(s1, s2 ProcSample, duration time.Duration) float64 { total := float64(s2 - s1) @@ -122,8 +118,6 @@ func CalculateCPUUsage(s1, s2 ProcSample, duration time.Duration) float64 { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // parseStatData parses CPU stats data func parseStatData(text string) (*ProcInfo, error) { var err error @@ -197,8 +191,6 @@ func parseSampleData(text string) (ProcSample, error) { return ProcSample(utime + stime + cutime + cstime), nil } -// codebeat:enable[LOC,ABC] - // parseIntField parses int value of field func parseIntField(s string, field int) (int, error) { v, err := strconv.Atoi(s) diff --git a/system/process/process_memory.go b/system/process/process_memory.go index 02a9b1e5..7284a03a 100644 --- a/system/process/process_memory.go +++ b/system/process/process_memory.go @@ -39,8 +39,6 @@ type MemInfo struct { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // GetMemInfo returns info about process memory usage func GetMemInfo(pid int) (*MemInfo, error) { fd, err := os.OpenFile(procFS+"/"+strconv.Itoa(pid)+"/status", os.O_RDONLY, 0) @@ -102,8 +100,6 @@ func GetMemInfo(pid int) (*MemInfo, error) { return info, nil } -// codebeat:enable[LOC,ABC] - // ////////////////////////////////////////////////////////////////////////////////// // // parseSize parse size in kB diff --git a/system/process/process_mount.go b/system/process/process_mount.go index 38dac93e..ac18b10e 100644 --- a/system/process/process_mount.go +++ b/system/process/process_mount.go @@ -40,8 +40,6 @@ type MountInfo struct { // ////////////////////////////////////////////////////////////////////////////////// // -// codebeat:disable[LOC,ABC] - // GetMountInfo returns info about process mounts func GetMountInfo(pid int) ([]*MountInfo, error) { fd, err := os.OpenFile(procFS+"/"+strconv.Itoa(pid)+"/mountinfo", os.O_RDONLY, 0) @@ -127,8 +125,6 @@ func parseMountInfoLine(data string) (*MountInfo, error) { return info, nil } -// codebeat:enable[LOC,ABC] - // parseStDevValue parses st_dev major and minor values func parseStDevValue(data string) (uint16, uint16, error) { major, err := parseFieldUint16(strutil.ReadField(data, 0, false, ':'), "StDevMajor") diff --git a/timeutil/timeutil.go b/timeutil/timeutil.go index 029ed679..16240ebc 100644 --- a/timeutil/timeutil.go +++ b/timeutil/timeutil.go @@ -397,9 +397,6 @@ func FromISOWeek(week, year int, loc *time.Location) time.Time { // ////////////////////////////////////////////////////////////////////////////////// // -// It's ok to have so long method here -// codebeat:disable[LOC,ABC] - func convertDuration(d any) (time.Duration, bool) { switch u := d.(type) { case time.Duration: @@ -531,8 +528,6 @@ func replaceDateTag(d time.Time, input, output *bytes.Buffer) { } } -// codebeat:enable[LOC,ABC] - func getShortWeekday(d time.Weekday) string { long := getLongWeekday(d) @@ -706,9 +701,6 @@ func getShortDuration(dur time.Duration, highPrecision bool) string { return fmt.Sprintf("%d:%02d", m, d) } -// It's ok to have so nested blocks in this method -// codebeat:disable[BLOCK_NESTING] - func getPrettyLongDuration(dur time.Duration) string { d := int64(dur.Seconds()) @@ -767,8 +759,6 @@ func getPrettySimpleDuration(dur time.Duration) string { return "< 1 second" } -// codebeat:enable[BLOCK_NESTING] - func getPrettyShortDuration(d time.Duration, separator string) string { switch { case d >= 24*time.Hour: