diff --git a/.golangci.yml b/.golangci.yml index 4129e7e5..126264a6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,6 +21,7 @@ linters: - ireturn - lll - musttag + - modernize - nestif - nlreturn - nonamedreturns diff --git a/cmdutils/cmd_utils.go b/cmdutils/cmd_utils.go index e95bf8aa..6c7bbb26 100644 --- a/cmdutils/cmd_utils.go +++ b/cmdutils/cmd_utils.go @@ -9,5 +9,5 @@ package cmdutils type CommandLineOptionsGroup struct { ShortDescription string LongDescription string - Options interface{} + Options any } diff --git a/conv/convert_types_test.go b/conv/convert_types_test.go index 8667ec94..e1a1222a 100644 --- a/conv/convert_types_test.go +++ b/conv/convert_types_test.go @@ -162,7 +162,7 @@ func assertSingleValue(t *testing.T, inElem, elem reflect.Value, expectPointer b } // assertValues checks equivalent representation pointer vs values for single var, slices and maps -func assertValues(t *testing.T, in, out interface{}, expectPointer bool, idx int) { +func assertValues(t *testing.T, in, out any, expectPointer bool, idx int) { vin := reflect.ValueOf(in) vout := reflect.ValueOf(out) diff --git a/fileutils/file_test.go b/fileutils/file_test.go index c4ec8f24..c993fecf 100644 --- a/fileutils/file_test.go +++ b/fileutils/file_test.go @@ -11,13 +11,13 @@ import ( ) func TestFileImplementsIOReader(t *testing.T) { - var file interface{} = &File{} + var file any = &File{} expected := "that File implements io.Reader" assert.Implements(t, new(io.Reader), file, expected) } func TestFileImplementsIOReadCloser(t *testing.T) { - var file interface{} = &File{} + var file any = &File{} expected := "that File implements io.ReadCloser" assert.Implements(t, new(io.ReadCloser), file, expected) } diff --git a/jsonname/name_provider.go b/jsonname/name_provider.go index bd0cd40b..8eaf1bec 100644 --- a/jsonname/name_provider.go +++ b/jsonname/name_provider.go @@ -79,7 +79,7 @@ func newNameIndex(tpe reflect.Type) nameIndex { } // GetJSONNames gets all the json property names for a type -func (n *NameProvider) GetJSONNames(subject interface{}) []string { +func (n *NameProvider) GetJSONNames(subject any) []string { n.lock.Lock() defer n.lock.Unlock() tpe := reflect.Indirect(reflect.ValueOf(subject)).Type() @@ -96,7 +96,7 @@ func (n *NameProvider) GetJSONNames(subject interface{}) []string { } // GetJSONName gets the json name for a go property name -func (n *NameProvider) GetJSONName(subject interface{}, name string) (string, bool) { +func (n *NameProvider) GetJSONName(subject any, name string) (string, bool) { tpe := reflect.Indirect(reflect.ValueOf(subject)).Type() return n.GetJSONNameForType(tpe, name) } @@ -114,7 +114,7 @@ func (n *NameProvider) GetJSONNameForType(tpe reflect.Type, name string) (string } // GetGoName gets the go name for a json property name -func (n *NameProvider) GetGoName(subject interface{}, name string) (string, bool) { +func (n *NameProvider) GetGoName(subject any, name string) (string, bool) { tpe := reflect.Indirect(reflect.ValueOf(subject)).Type() return n.GetGoNameForType(tpe, name) } diff --git a/jsonutils_iface.go b/jsonutils_iface.go index 78110bc3..7bd4105f 100644 --- a/jsonutils_iface.go +++ b/jsonutils_iface.go @@ -22,17 +22,17 @@ type JSONMapItem = jsonutils.JSONMapItem // WriteJSON writes json data. // // Deprecated: use [jsonutils.WriteJSON] instead. -func WriteJSON(data interface{}) ([]byte, error) { return jsonutils.WriteJSON(data) } +func WriteJSON(data any) ([]byte, error) { return jsonutils.WriteJSON(data) } // ReadJSON reads json data. // // Deprecated: use [jsonutils.ReadJSON] instead. -func ReadJSON(data []byte, value interface{}) error { return jsonutils.ReadJSON(data, value) } +func ReadJSON(data []byte, value any) error { return jsonutils.ReadJSON(data, value) } // DynamicJSONToStruct converts an untyped JSON structure into a target data type. // // Deprecated: use [jsonutils.FromDynamicJSON] instead. -func DynamicJSONToStruct(data interface{}, target interface{}) error { +func DynamicJSONToStruct(data any, target any) error { return jsonutils.FromDynamicJSON(data, target) } @@ -46,8 +46,8 @@ func ConcatJSON(blobs ...[]byte) []byte { return jsonutils.ConcatJSON(blobs...) // It is the same as [FromDynamicJSON], but doesn't check for errors. // // Deprecated: this function is a misnomer and is unsafe. Use [jsonutils.FromDynamicJSON] instead. -func ToDynamicJSON(value interface{}) interface{} { - var res interface{} +func ToDynamicJSON(value any) any { + var res any if err := FromDynamicJSON(value, &res); err != nil { log.Println(err) } @@ -57,9 +57,9 @@ func ToDynamicJSON(value interface{}) interface{} { // FromDynamicJSON turns a go value into a properly JSON typed structure. // -// "Dynamic JSON" refers to what you get when unmarshaling JSON into an untyped interface{}, -// i.e. objects are represented by map[string]interface{}, arrays by []interface{}, and all -// scalar values are interface{}. +// "Dynamic JSON" refers to what you get when unmarshaling JSON into an untyped any, +// i.e. objects are represented by map[string]any, arrays by []any, and all +// scalar values are any. // // Deprecated: use [jsonutils.FromDynamicJSON] instead. -func FromDynamicJSON(data, target interface{}) error { return jsonutils.FromDynamicJSON(data, target) } +func FromDynamicJSON(data, target any) error { return jsonutils.FromDynamicJSON(data, target) } diff --git a/loading/yaml.go b/loading/yaml.go index eebb29da..3ebb5366 100644 --- a/loading/yaml.go +++ b/loading/yaml.go @@ -27,7 +27,7 @@ func YAMLDoc(path string, opts ...Option) (json.RawMessage, error) { } // YAMLData loads a yaml document from either http or a file. -func YAMLData(path string, opts ...Option) (interface{}, error) { +func YAMLData(path string, opts ...Option) (any, error) { data, err := LoadFromFileOrHTTP(path, opts...) if err != nil { return nil, err diff --git a/loading_iface.go b/loading_iface.go index 7154d36c..27ec3fb8 100644 --- a/loading_iface.go +++ b/loading_iface.go @@ -73,7 +73,7 @@ func YAMLDoc(path string) (json.RawMessage, error) { // YAMLData loads a yaml document from either http or a file. // // Deprecated: use [loading.YAMLData] instead. -func YAMLData(path string) (interface{}, error) { +func YAMLData(path string) (any, error) { return loading.YAMLData(path) } diff --git a/loading_iface_test.go b/loading_iface_test.go index 532868a9..344cdaad 100644 --- a/loading_iface_test.go +++ b/loading_iface_test.go @@ -7,6 +7,7 @@ import ( "context" "net/http" "net/http/httptest" + "slices" "testing" "time" @@ -62,14 +63,7 @@ func TestLoadFromHTTP(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { myHeaders := r.Header[sharedHeaderKey] - ok := false - for _, v := range myHeaders { - if v == sharedHeaderValue { - ok = true - break - } - } - if ok { + if slices.Contains(myHeaders, sharedHeaderValue) { rw.WriteHeader(http.StatusOK) } else { rw.WriteHeader(http.StatusForbidden) diff --git a/typeutils_iface.go b/typeutils_iface.go index 60a6a91a..b63813ea 100644 --- a/typeutils_iface.go +++ b/typeutils_iface.go @@ -9,4 +9,4 @@ import "github.com/go-openapi/swag/typeutils" // This allows for safer checking of interface values. // // Deprecated: use [typeutils.IsZero] instead. -func IsZero(data interface{}) bool { return typeutils.IsZero(data) } +func IsZero(data any) bool { return typeutils.IsZero(data) } diff --git a/yamlutils_iface.go b/yamlutils_iface.go index 4ac42982..57767efc 100644 --- a/yamlutils_iface.go +++ b/yamlutils_iface.go @@ -12,9 +12,9 @@ import ( // YAMLToJSON converts YAML unmarshaled data into json compatible data // // Deprecated: use [yamlutils.YAMLToJSON] instead. -func YAMLToJSON(data interface{}) (json.RawMessage, error) { return yamlutils.YAMLToJSON(data) } +func YAMLToJSON(data any) (json.RawMessage, error) { return yamlutils.YAMLToJSON(data) } // BytesToYAMLDoc converts a byte slice into a YAML document // // Deprecated: use [yamlutils.BytesToYAMLDoc] instead. -func BytesToYAMLDoc(data []byte) (interface{}, error) { return yamlutils.BytesToYAMLDoc(data) } +func BytesToYAMLDoc(data []byte) (any, error) { return yamlutils.BytesToYAMLDoc(data) }