-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust for reflect.Type.NumMethod change in Go1.16 (#240)
In Go1.16, the reflect.Type.NumMethod method will no longer report unexported fields, matching the documented behavior on the method. This means that t.NumMethod() == 0 is no longer a reliable means to detect whether an interface type is the empty interface or not. Fix the code to check whether the empty interface itself implements the target type.
- Loading branch information
Showing
4 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2020, The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE.md file. | ||
|
||
package value | ||
|
||
import "reflect" | ||
|
||
var emptyIfaceType = reflect.TypeOf((*interface{})(nil)).Elem() | ||
|
||
// IsEmptyInterface reports whether t is an interface type with no methods. | ||
func IsEmptyInterface(t reflect.Type) bool { | ||
return t.Kind() == reflect.Interface && emptyIfaceType.Implements(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2020, The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE.md file. | ||
|
||
package value | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestIsEmptyInterface(t *testing.T) { | ||
type ( | ||
Empty interface{} | ||
Exported interface{ X() } | ||
Unexported interface{ x() } | ||
) | ||
tests := []struct { | ||
in reflect.Type | ||
want bool | ||
}{ | ||
{reflect.TypeOf((*interface{})(nil)).Elem(), true}, | ||
{reflect.TypeOf((*Empty)(nil)).Elem(), true}, | ||
{reflect.TypeOf((*Exported)(nil)).Elem(), false}, | ||
{reflect.TypeOf((*Unexported)(nil)).Elem(), false}, | ||
{reflect.TypeOf(5), false}, | ||
{reflect.TypeOf(struct{}{}), false}, | ||
} | ||
for _, tt := range tests { | ||
got := IsEmptyInterface(tt.in) | ||
if got != tt.want { | ||
t.Errorf("IsEmptyInterface(%v) = %v, want %v", tt.in, got, tt.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters