all: gofmt -w -r 'interface{} -> any' .

Updates #9687
This commit is contained in:
Bjørn Erik Pedersen
2022-03-17 22:03:27 +01:00
parent 423594e03a
commit b80853de90
342 changed files with 2118 additions and 2102 deletions

View File

@@ -32,7 +32,7 @@ const (
tomlDelimLf = "+++\n"
)
func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer) error {
func InterfaceToConfig(in any, format metadecoders.Format, w io.Writer) error {
if in == nil {
return errors.New("input was nil")
}
@@ -77,7 +77,7 @@ func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer)
}
}
func InterfaceToFrontMatter(in interface{}, format metadecoders.Format, w io.Writer) error {
func InterfaceToFrontMatter(in any, format metadecoders.Format, w io.Writer) error {
if in == nil {
return errors.New("input was nil")
}

View File

@@ -23,33 +23,33 @@ import (
func TestInterfaceToConfig(t *testing.T) {
cases := []struct {
input interface{}
input any
format metadecoders.Format
want []byte
isErr bool
}{
// TOML
{map[string]interface{}{}, metadecoders.TOML, nil, false},
{map[string]any{}, metadecoders.TOML, nil, false},
{
map[string]interface{}{"title": "test' 1"},
map[string]any{"title": "test' 1"},
metadecoders.TOML,
[]byte("title = \"test' 1\"\n"),
false,
},
// YAML
{map[string]interface{}{}, metadecoders.YAML, []byte("{}\n"), false},
{map[string]any{}, metadecoders.YAML, []byte("{}\n"), false},
{
map[string]interface{}{"title": "test 1"},
map[string]any{"title": "test 1"},
metadecoders.YAML,
[]byte("title: test 1\n"),
false,
},
// JSON
{map[string]interface{}{}, metadecoders.JSON, []byte("{}\n"), false},
{map[string]any{}, metadecoders.JSON, []byte("{}\n"), false},
{
map[string]interface{}{"title": "test 1"},
map[string]any{"title": "test 1"},
metadecoders.JSON,
[]byte("{\n \"title\": \"test 1\"\n}\n"),
false,
@@ -57,7 +57,7 @@ func TestInterfaceToConfig(t *testing.T) {
// Errors
{nil, metadecoders.TOML, nil, true},
{map[string]interface{}{}, "foo", nil, true},
{map[string]any{}, "foo", nil, true},
}
for i, c := range cases {

View File

@@ -29,7 +29,7 @@ var (
// Code adapted from https://gist.github.com/piersy/b9934790a8892db1a603820c0c23e4a7
type LowerCaseCamelJSONMarshaller struct {
Value interface{}
Value any
}
func (c LowerCaseCamelJSONMarshaller) MarshalJSON() ([]byte, error) {

View File

@@ -58,8 +58,8 @@ var Default = Decoder{
// UnmarshalToMap will unmarshall data in format f into a new map. This is
// what's needed for Hugo's front matter decoding.
func (d Decoder) UnmarshalToMap(data []byte, f Format) (map[string]interface{}, error) {
m := make(map[string]interface{})
func (d Decoder) UnmarshalToMap(data []byte, f Format) (map[string]any, error) {
m := make(map[string]any)
if data == nil {
return m, nil
}
@@ -71,7 +71,7 @@ func (d Decoder) UnmarshalToMap(data []byte, f Format) (map[string]interface{},
// UnmarshalFileToMap is the same as UnmarshalToMap, but reads the data from
// the given filename.
func (d Decoder) UnmarshalFileToMap(fs afero.Fs, filename string) (map[string]interface{}, error) {
func (d Decoder) UnmarshalFileToMap(fs afero.Fs, filename string) (map[string]any, error) {
format := FormatFromString(filename)
if format == "" {
return nil, errors.Errorf("%q is not a valid configuration format", filename)
@@ -85,16 +85,16 @@ func (d Decoder) UnmarshalFileToMap(fs afero.Fs, filename string) (map[string]in
}
// UnmarshalStringTo tries to unmarshal data to a new instance of type typ.
func (d Decoder) UnmarshalStringTo(data string, typ interface{}) (interface{}, error) {
func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) {
data = strings.TrimSpace(data)
// We only check for the possible types in YAML, JSON and TOML.
switch typ.(type) {
case string:
return data, nil
case map[string]interface{}:
case map[string]any:
format := d.FormatFromContentString(data)
return d.UnmarshalToMap([]byte(data), format)
case []interface{}:
case []any:
// A standalone slice. Let YAML handle it.
return d.Unmarshal([]byte(data), YAML)
case bool:
@@ -112,23 +112,23 @@ func (d Decoder) UnmarshalStringTo(data string, typ interface{}) (interface{}, e
// Unmarshal will unmarshall data in format f into an interface{}.
// This is what's needed for Hugo's /data handling.
func (d Decoder) Unmarshal(data []byte, f Format) (interface{}, error) {
func (d Decoder) Unmarshal(data []byte, f Format) (any, error) {
if data == nil {
switch f {
case CSV:
return make([][]string, 0), nil
default:
return make(map[string]interface{}), nil
return make(map[string]any), nil
}
}
var v interface{}
var v any
err := d.UnmarshalTo(data, f, &v)
return v, err
}
// UnmarshalTo unmarshals data in format f into v.
func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error {
var err error
switch f {
@@ -140,19 +140,19 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
var xmlRoot xml.Map
xmlRoot, err = xml.NewMapXml(data)
var xmlValue map[string]interface{}
var xmlValue map[string]any
if err == nil {
xmlRootName, err := xmlRoot.Root()
if err != nil {
return toFileError(f, errors.Wrap(err, "failed to unmarshal XML"))
}
xmlValue = xmlRoot[xmlRootName].(map[string]interface{})
xmlValue = xmlRoot[xmlRootName].(map[string]any)
}
switch v := v.(type) {
case *map[string]interface{}:
case *map[string]any:
*v = xmlValue
case *interface{}:
case *any:
*v = xmlValue
}
case TOML:
@@ -167,12 +167,12 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
// map[interface{}]interface{}. Here we recurse through the result
// and change all maps to map[string]interface{} like we would've
// gotten from `json`.
var ptr interface{}
var ptr any
switch v.(type) {
case *map[string]interface{}:
ptr = *v.(*map[string]interface{})
case *interface{}:
ptr = *v.(*interface{})
case *map[string]any:
ptr = *v.(*map[string]any)
case *any:
ptr = *v.(*any)
default:
// Not a map.
}
@@ -180,10 +180,10 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
if ptr != nil {
if mm, changed := stringifyMapKeys(ptr); changed {
switch v.(type) {
case *map[string]interface{}:
*v.(*map[string]interface{}) = mm.(map[string]interface{})
case *interface{}:
*v.(*interface{}) = mm
case *map[string]any:
*v.(*map[string]any) = mm.(map[string]any)
case *any:
*v.(*any) = mm
}
}
}
@@ -201,7 +201,7 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
return toFileError(f, errors.Wrap(err, "unmarshal failed"))
}
func (d Decoder) unmarshalCSV(data []byte, v interface{}) error {
func (d Decoder) unmarshalCSV(data []byte, v any) error {
r := csv.NewReader(bytes.NewReader(data))
r.Comma = d.Delimiter
r.Comment = d.Comment
@@ -212,8 +212,8 @@ func (d Decoder) unmarshalCSV(data []byte, v interface{}) error {
}
switch v.(type) {
case *interface{}:
*v.(*interface{}) = records
case *any:
*v.(*any) = records
default:
return errors.Errorf("CSV cannot be unmarshaled into %T", v)
@@ -230,14 +230,14 @@ func parseORGDate(s string) string {
return s
}
func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
func (d Decoder) unmarshalORG(data []byte, v any) error {
config := org.New()
config.Log = jww.WARN
document := config.Parse(bytes.NewReader(data), "")
if document.Error != nil {
return document.Error
}
frontMatter := make(map[string]interface{}, len(document.BufferSettings))
frontMatter := make(map[string]any, len(document.BufferSettings))
for k, v := range document.BufferSettings {
k = strings.ToLower(k)
if strings.HasSuffix(k, "[]") {
@@ -252,10 +252,10 @@ func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
}
}
switch v.(type) {
case *map[string]interface{}:
*v.(*map[string]interface{}) = frontMatter
case *map[string]any:
*v.(*map[string]any) = frontMatter
default:
*v.(*interface{}) = frontMatter
*v.(*any) = frontMatter
}
return nil
}
@@ -270,22 +270,22 @@ func toFileError(f Format, err error) error {
// described here: https://github.com/go-yaml/yaml/issues/139
//
// Inspired by https://github.com/stripe/stripe-mock, MIT licensed
func stringifyMapKeys(in interface{}) (interface{}, bool) {
func stringifyMapKeys(in any) (any, bool) {
switch in := in.(type) {
case []interface{}:
case []any:
for i, v := range in {
if vv, replaced := stringifyMapKeys(v); replaced {
in[i] = vv
}
}
case map[string]interface{}:
case map[string]any:
for k, v := range in {
if vv, changed := stringifyMapKeys(v); changed {
in[k] = vv
}
}
case map[interface{}]interface{}:
res := make(map[string]interface{})
case map[any]any:
res := make(map[string]any)
var (
ok bool
err error

View File

@@ -45,13 +45,13 @@ func TestUnmarshalXML(t *testing.T) {
</channel>
</rss>`
expect := map[string]interface{}{
expect := map[string]any{
"-atom": "http://www.w3.org/2005/Atom", "-version": "2.0",
"channel": map[string]interface{}{
"channel": map[string]any{
"copyright": "Example",
"description": "Example feed",
"generator": "Hugo -- gohugo.io",
"item": map[string]interface{}{
"item": map[string]any{
"description": "Example description",
"guid": "https://example.com/2021/11/30/example-title/",
"link": "https://example.com/2021/11/30/example-title/",
@@ -59,7 +59,7 @@ func TestUnmarshalXML(t *testing.T) {
"title": "Example title"},
"language": "en-us",
"lastBuildDate": "Fri, 08 Jan 2021 14:44:10 +0000",
"link": []interface{}{"https://example.com/", map[string]interface{}{
"link": []any{"https://example.com/", map[string]any{
"-href": "https://example.com/feed.xml",
"-rel": "self",
"-type": "application/rss+xml"}},
@@ -76,20 +76,20 @@ func TestUnmarshalXML(t *testing.T) {
func TestUnmarshalToMap(t *testing.T) {
c := qt.New(t)
expect := map[string]interface{}{"a": "b"}
expect := map[string]any{"a": "b"}
d := Default
for i, test := range []struct {
data string
format Format
expect interface{}
expect any
}{
{`a = "b"`, TOML, expect},
{`a: "b"`, YAML, expect},
// Make sure we get all string keys, even for YAML
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}},
{"a:\n true: 1\n false: 2", YAML, map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}},
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
{"a:\n true: 1\n false: 2", YAML, map[string]any{"a": map[string]any{"true": 1, "false": 2}}},
{`{ "a": "b" }`, JSON, expect},
{`<root><a>b</a></root>`, XML, expect},
{`#+a: b`, ORG, expect},
@@ -111,24 +111,24 @@ func TestUnmarshalToMap(t *testing.T) {
func TestUnmarshalToInterface(t *testing.T) {
c := qt.New(t)
expect := map[string]interface{}{"a": "b"}
expect := map[string]any{"a": "b"}
d := Default
for i, test := range []struct {
data string
format Format
expect interface{}
expect any
}{
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}},
{`{ "a": "b" }`, JSON, expect},
{`#+a: b`, ORG, expect},
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]interface{}{"date": "2020-06-26"}},
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}},
{`a = "b"`, TOML, expect},
{`a: "b"`, YAML, expect},
{`<root><a>b</a></root>`, XML, expect},
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}},
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
// errors
{`a = "`, TOML, false},
} {
@@ -149,20 +149,20 @@ func TestUnmarshalStringTo(t *testing.T) {
d := Default
expectMap := map[string]interface{}{"a": "b"}
expectMap := map[string]any{"a": "b"}
for i, test := range []struct {
data string
to interface{}
expect interface{}
to any
expect any
}{
{"a string", "string", "a string"},
{`{ "a": "b" }`, make(map[string]interface{}), expectMap},
{`{ "a": "b" }`, make(map[string]any), expectMap},
{"32", int64(1234), int64(32)},
{"32", int(1234), int(32)},
{"3.14159", float64(1), float64(3.14159)},
{"[3,7,9]", []interface{}{}, []interface{}{3, 7, 9}},
{"[3.1,7.2,9.3]", []interface{}{}, []interface{}{3.1, 7.2, 9.3}},
{"[3,7,9]", []any{}, []any{3, 7, 9}},
{"[3.1,7.2,9.3]", []any{}, []any{3.1, 7.2, 9.3}},
} {
msg := qt.Commentf("%d: %T", i, test.to)
m, err := d.UnmarshalStringTo(test.data, test.to)
@@ -178,43 +178,43 @@ func TestUnmarshalStringTo(t *testing.T) {
func TestStringifyYAMLMapKeys(t *testing.T) {
cases := []struct {
input interface{}
want interface{}
input any
want any
replaced bool
}{
{
map[interface{}]interface{}{"a": 1, "b": 2},
map[string]interface{}{"a": 1, "b": 2},
map[any]any{"a": 1, "b": 2},
map[string]any{"a": 1, "b": 2},
true,
},
{
map[interface{}]interface{}{"a": []interface{}{1, map[interface{}]interface{}{"b": 2}}},
map[string]interface{}{"a": []interface{}{1, map[string]interface{}{"b": 2}}},
map[any]any{"a": []any{1, map[any]any{"b": 2}}},
map[string]any{"a": []any{1, map[string]any{"b": 2}}},
true,
},
{
map[interface{}]interface{}{true: 1, "b": false},
map[string]interface{}{"true": 1, "b": false},
map[any]any{true: 1, "b": false},
map[string]any{"true": 1, "b": false},
true,
},
{
map[interface{}]interface{}{1: "a", 2: "b"},
map[string]interface{}{"1": "a", "2": "b"},
map[any]any{1: "a", 2: "b"},
map[string]any{"1": "a", "2": "b"},
true,
},
{
map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": 1}},
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
map[any]any{"a": map[any]any{"b": 1}},
map[string]any{"a": map[string]any{"b": 1}},
true,
},
{
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
map[string]interface{}{"a": map[string]interface{}{"b": 1}},
map[string]any{"a": map[string]any{"b": 1}},
map[string]any{"a": map[string]any{"b": 1}},
false,
},
{
[]interface{}{map[interface{}]interface{}{1: "a", 2: "b"}},
[]interface{}{map[string]interface{}{"1": "a", "2": "b"}},
[]any{map[any]any{1: "a", 2: "b"}},
[]any{map[string]any{"1": "a", "2": "b"}},
false,
},
}
@@ -235,18 +235,18 @@ func TestStringifyYAMLMapKeys(t *testing.T) {
}
func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) {
maps := make([]map[interface{}]interface{}, b.N)
maps := make([]map[any]any, b.N)
for i := 0; i < b.N; i++ {
maps[i] = map[interface{}]interface{}{
"a": map[interface{}]interface{}{
maps[i] = map[any]any{
"a": map[any]any{
"b": 32,
"c": 43,
"d": map[interface{}]interface{}{
"d": map[any]any{
"b": 32,
"c": 43,
},
},
"b": []interface{}{"a", "b"},
"b": []any{"a", "b"},
"c": "d",
}
}
@@ -257,16 +257,16 @@ func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) {
}
func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
m := map[string]interface{}{
"a": map[string]interface{}{
m := map[string]any{
"a": map[string]any{
"b": 32,
"c": 43,
"d": map[string]interface{}{
"d": map[string]any{
"b": 32,
"c": 43,
},
},
"b": []interface{}{"a", "b"},
"b": []any{"a", "b"},
"c": "d",
}
@@ -277,18 +277,18 @@ func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
}
func BenchmarkStringifyMapKeysIntegers(b *testing.B) {
maps := make([]map[interface{}]interface{}, b.N)
maps := make([]map[any]any, b.N)
for i := 0; i < b.N; i++ {
maps[i] = map[interface{}]interface{}{
1: map[interface{}]interface{}{
maps[i] = map[any]any{
1: map[any]any{
4: 32,
5: 43,
6: map[interface{}]interface{}{
6: map[any]any{
7: 32,
8: 43,
},
},
2: []interface{}{"a", "b"},
2: []any{"a", "b"},
3: "d",
}
}

View File

@@ -64,7 +64,7 @@ func TestFormatFromContentString(t *testing.T) {
for i, test := range []struct {
data string
expect interface{}
expect any
}{
{`foo = "bar"`, TOML},
{` foo = "bar"`, TOML},

View File

@@ -33,7 +33,7 @@ func (i Item) ValStr() string {
return string(i.Val)
}
func (i Item) ValTyped() interface{} {
func (i Item) ValTyped() any {
str := i.ValStr()
if i.isString {
// A quoted value that is a string even if it looks like a number etc.

View File

@@ -176,7 +176,7 @@ func (l *pageLexer) ignore() {
var lf = []byte("\n")
// nil terminates the parser
func (l *pageLexer) errorf(format string, args ...interface{}) stateFunc {
func (l *pageLexer) errorf(format string, args ...any) stateFunc {
l.items = append(l.items, Item{tError, l.start, []byte(fmt.Sprintf(format, args...)), true})
return nil
}

View File

@@ -42,7 +42,7 @@ func Parse(r io.Reader, cfg Config) (Result, error) {
type ContentFrontMatter struct {
Content []byte
FrontMatter map[string]interface{}
FrontMatter map[string]any
FrontMatterFormat metadecoders.Format
}