modernise: Run new gopls modernise tool against the codebase and fix minor issues (#1826)

* modernise: Run new gopls modernise tool against codebase

* Address shazbert's nits

* apichecker, gctcli: Simplify HTML scraping functions and improve depth limit handling

* refactor: Create minSyncInterval const and update order book limit handling for binance and binanceUS

* refactor: Various slice usage improvements and rename TODO

* tranches: Revert deleteByID changes due to performance decrease

Shazbert was a F1 driver in a past lifetime 🏎️

* tranches: Simply retrieve copy

Thanks to shazbert

* documentation: Sort contributors list by contributions

* tranches: Remove deadcode in deleteByID
This commit is contained in:
Adrian Gallagher
2025-03-21 09:17:10 +11:00
committed by GitHub
parent d857d704e3
commit 4651af5767
223 changed files with 1504 additions and 1752 deletions

View File

@@ -46,7 +46,7 @@ func DeriveURLValsFromJSONMap(payload []byte) (url.Values, error) {
if len(payload) == 0 {
return vals, nil
}
intermediary := make(map[string]interface{})
intermediary := make(map[string]any)
err := json.Unmarshal(payload, &intermediary)
if err != nil {
return vals, err
@@ -60,7 +60,7 @@ func DeriveURLValsFromJSONMap(payload []byte) (url.Values, error) {
vals.Add(k, strconv.FormatBool(val))
case float64:
vals.Add(k, strconv.FormatFloat(val, 'f', -1, 64))
case map[string]interface{}, []interface{}, nil:
case map[string]any, []any, nil:
vals.Add(k, fmt.Sprintf("%v", val))
default:
log.Println(reflect.TypeOf(val))

View File

@@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"sync"
@@ -153,7 +154,7 @@ func HTTPRecord(res *http.Response, service string, respContents []byte) error {
}
if MatchURLVals(mockQuery, res.Request.URL.Query()) {
mockResponses = append(mockResponses[:i], mockResponses[i+1:]...) // Delete Old
mockResponses = slices.Delete(mockResponses, i, i+1)
break
}
}
@@ -179,7 +180,7 @@ func HTTPRecord(res *http.Response, service string, respContents []byte) error {
if MatchURLVals(respQueryVals, mockRespVals) {
// if found will delete instance and overwrite with new
// data
mockResponses = append(mockResponses[:i], mockResponses[i+1:]...)
mockResponses = slices.Delete(mockResponses, i, i+1)
found = true
}
@@ -197,7 +198,7 @@ func HTTPRecord(res *http.Response, service string, respContents []byte) error {
if MatchURLVals(reqVals, mockVals) {
// if found will delete instance and overwrite with new
// data
mockResponses = append(mockResponses[:i], mockResponses[i+1:]...)
mockResponses = slices.Delete(mockResponses, i, i+1)
found = true
}
case "":
@@ -210,7 +211,7 @@ func HTTPRecord(res *http.Response, service string, respContents []byte) error {
if MatchURLVals(mockQuery, res.Request.URL.Query()) {
// if found will delete instance and overwrite with new data
mockResponses = append(mockResponses[:i], mockResponses[i+1:]...)
mockResponses = slices.Delete(mockResponses, i, i+1)
found = true
}
@@ -285,7 +286,7 @@ func CheckResponsePayload(data []byte) ([]byte, error) {
return nil, err
}
var intermediary interface{}
var intermediary any
err = json.Unmarshal(data, &intermediary)
if err != nil {
return nil, err
@@ -310,13 +311,13 @@ const (
)
// CheckJSON recursively parses json data to retract keywords, quite intensive.
func CheckJSON(data interface{}, excluded *Exclusion) (interface{}, error) {
if d, ok := data.([]interface{}); ok {
var sData []interface{}
func CheckJSON(data any, excluded *Exclusion) (any, error) {
if d, ok := data.([]any); ok {
var sData []any
for i := range d {
v := d[i]
switch v.(type) {
case map[string]interface{}, []interface{}:
case map[string]any, []any:
checkedData, err := CheckJSON(v, excluded)
if err != nil {
return nil, err
@@ -336,7 +337,7 @@ func CheckJSON(data interface{}, excluded *Exclusion) (interface{}, error) {
return nil, err
}
var context map[string]interface{}
var context map[string]any
err = json.Unmarshal(conv, &context)
if err != nil {
return nil, err
@@ -362,16 +363,16 @@ func CheckJSON(data interface{}, excluded *Exclusion) (interface{}, error) {
context[key] = 0.0 // Zero val float
}
case Slice:
slice, ok := val.([]interface{})
slice, ok := val.([]any)
if !ok {
return nil, common.GetTypeAssertError("[]interface{}", val)
return nil, common.GetTypeAssertError("[]any", val)
}
if len(slice) < 1 {
// Empty slice found
context[key] = slice
} else {
if _, ok := slice[0].(map[string]interface{}); ok {
var cleanSlice []interface{}
if _, ok := slice[0].(map[string]any); ok {
var cleanSlice []any
for i := range slice {
cleanMap, sErr := CheckJSON(slice[i], excluded)
if sErr != nil {

View File

@@ -69,7 +69,7 @@ type TestStructLevel0 struct {
FloatVal float64 `json:"floatVal"`
IntVal int64 `json:"intVal"`
StructVal TestStructLevel1 `json:"structVal"`
MixedSlice []interface{} `json:"mixedSlice"`
MixedSlice []any `json:"mixedSlice"`
}
type TestStructLevel1 struct {
@@ -119,9 +119,9 @@ func TestCheckJSON(t *testing.T) {
OtherData: level2,
}
sliceOfPrimitives := []interface{}{
[]interface{}{float64(1586994000000), "6615.23000000"},
[]interface{}{float64(1586994300000), "6624.74000000"},
sliceOfPrimitives := []any{
[]any{float64(1586994000000), "6615.23000000"},
[]any{float64(1586994300000), "6624.74000000"},
}
testVal := TestStructLevel0{
@@ -187,7 +187,7 @@ func TestCheckJSON(t *testing.T) {
t.Fatal("json marshal error", err)
}
var newSlice []interface{}
var newSlice []any
err = json.Unmarshal(payload, &newSlice)
if err != nil {
t.Fatal("Unmarshal error", err)

View File

@@ -222,7 +222,7 @@ func RegisterHandler(pattern string, mock map[string][]HTTPResponse, mux *http.S
}
// MessageWriteJSON writes JSON to a connection
func MessageWriteJSON(w http.ResponseWriter, status int, data interface{}) {
func MessageWriteJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set(contentType, applicationJSON)
w.WriteHeader(status)
if data != nil {
@@ -252,7 +252,7 @@ func MatchAndGetResponse(mockData []HTTPResponse, requestVals url.Values, isQuer
mockVals := url.Values{}
var err error
if json.Valid([]byte(data)) {
something := make(map[string]interface{})
something := make(map[string]any)
err = json.Unmarshal([]byte(data), &something)
if err != nil {
return nil, err
@@ -266,7 +266,7 @@ func MatchAndGetResponse(mockData []HTTPResponse, requestVals url.Values, isQuer
mockVals.Add(k, strconv.FormatBool(val))
case float64:
mockVals.Add(k, strconv.FormatFloat(val, 'f', -1, 64))
case map[string]interface{}, []interface{}, nil:
case map[string]any, []any, nil:
mockVals.Add(k, fmt.Sprintf("%v", val))
default:
log.Println(reflect.TypeOf(val))