Skip to content

Replace all interface{} with any (Go 1.18+) #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions buffer/ring_growing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package buffer
// RingGrowing is a growing ring buffer.
// Not thread safe.
type RingGrowing struct {
data []interface{}
data []any
n int // Size of Data
beg int // First available element
readable int // Number of data items available
Expand All @@ -28,13 +28,13 @@ type RingGrowing struct {
// NewRingGrowing constructs a new RingGrowing instance with provided parameters.
func NewRingGrowing(initialSize int) *RingGrowing {
return &RingGrowing{
data: make([]interface{}, initialSize),
data: make([]any, initialSize),
n: initialSize,
}
}

// ReadOne reads (consumes) first item from the buffer if it is available, otherwise returns false.
func (r *RingGrowing) ReadOne() (data interface{}, ok bool) {
func (r *RingGrowing) ReadOne() (data any, ok bool) {
if r.readable == 0 {
return nil, false
}
Expand All @@ -51,11 +51,11 @@ func (r *RingGrowing) ReadOne() (data interface{}, ok bool) {
}

// WriteOne adds an item to the end of the buffer, growing it if it is full.
func (r *RingGrowing) WriteOne(data interface{}) {
func (r *RingGrowing) WriteOne(data any) {
if r.readable == r.n {
// Time to grow
newN := r.n * 2
newData := make([]interface{}, newN)
newData := make([]any, newN)
to := r.beg + r.readable
if to <= r.n {
copy(newData, r.data[r.beg:to])
Expand Down
14 changes: 7 additions & 7 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func StringDiff(a, b string) string {
// ObjectDiff writes the two objects out as JSON and prints out the identical part of
// the objects followed by the remaining part of 'a' and finally the remaining part of 'b'.
// For debugging tests.
func ObjectDiff(a, b interface{}) string {
func ObjectDiff(a, b any) string {
ab, err := json.Marshal(a)
if err != nil {
panic(fmt.Sprintf("a: %v", err))
Expand All @@ -70,7 +70,7 @@ func ObjectDiff(a, b interface{}) string {
// (go's %#v formatters OTOH stop at a certain point). This is needed when you
// can't figure out why reflect.DeepEqual is returning false and nothing is
// showing you differences. This will.
func ObjectGoPrintDiff(a, b interface{}) string {
func ObjectGoPrintDiff(a, b any) string {
s := spew.ConfigState{DisableMethods: true}
return StringDiff(
s.Sprintf("%#v", a),
Expand All @@ -79,7 +79,7 @@ func ObjectGoPrintDiff(a, b interface{}) string {
}

// ObjectReflectDiff returns a diff computed through reflection, without serializing to JSON.
func ObjectReflectDiff(a, b interface{}) string {
func ObjectReflectDiff(a, b any) string {
vA, vB := reflect.ValueOf(a), reflect.ValueOf(b)
if vA.Type() != vB.Type() {
return fmt.Sprintf("type A %T and type B %T do not match", a, b)
Expand All @@ -104,7 +104,7 @@ func ObjectReflectDiff(a, b interface{}) string {
// 1. stringifies aObj and bObj
// 2. elides identical prefixes if either is too long
// 3. elides remaining content from the end if either is too long
func limit(aObj, bObj interface{}, max int) (string, string) {
func limit(aObj, bObj any, max int) (string, string) {
elidedPrefix := ""
elidedASuffix := ""
elidedBSuffix := ""
Expand Down Expand Up @@ -155,7 +155,7 @@ func public(s string) bool {

type diff struct {
path *field.Path
a, b interface{}
a, b any
}

type orderedDiffs []diff
Expand Down Expand Up @@ -232,7 +232,7 @@ func objectReflectDiff(path *field.Path, a, b reflect.Value) []diff {
if reflect.DeepEqual(a.Interface(), b.Interface()) {
return nil
}
aKeys := make(map[interface{}]interface{})
aKeys := make(map[any]any)
for _, key := range a.MapKeys() {
aKeys[key.Interface()] = a.MapIndex(key).Interface()
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func objectReflectDiff(path *field.Path, a, b reflect.Value) []diff {

// ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
// enabling easy visual scanning for mismatches.
func ObjectGoPrintSideBySide(a, b interface{}) string {
func ObjectGoPrintSideBySide(a, b any) string {
s := spew.ConfigState{
Indent: " ",
// Extra deep spew.
Expand Down
8 changes: 4 additions & 4 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestObjectReflectDiff(t *testing.T) {
type struct1 struct{ A []int }

testCases := map[string]struct {
a, b interface{}
a, b any
out string
}{
"map": {
Expand Down Expand Up @@ -75,7 +75,7 @@ object.A:
a: []int(nil)
b: []int{}`,
},
"display type differences": {a: []interface{}{int64(1)}, b: []interface{}{uint64(1)}, out: `
"display type differences": {a: []any{int64(1)}, b: []any{uint64(1)}, out: `
object[0]:
a: 1 (int64)
b: 0x1 (uint64)`,
Expand All @@ -102,8 +102,8 @@ func TestStringDiff(t *testing.T) {

func TestLimit(t *testing.T) {
testcases := []struct {
a interface{}
b interface{}
a any
b any
expectA string
expectB string
}{
Expand Down
16 changes: 8 additions & 8 deletions internal/third_party/forked/golang/golang-lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ type Cache struct {

// OnEvicted optionally specifies a callback function to be
// executed when an entry is purged from the cache.
OnEvicted func(key Key, value interface{})
OnEvicted func(key Key, value any)

ll *list.List
cache map[interface{}]*list.Element
cache map[any]*list.Element
}

// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
type Key interface{}
type Key any

type entry struct {
key Key
value interface{}
value any
}

// New creates a new Cache.
Expand All @@ -48,14 +48,14 @@ func New(maxEntries int) *Cache {
return &Cache{
MaxEntries: maxEntries,
ll: list.New(),
cache: make(map[interface{}]*list.Element),
cache: make(map[any]*list.Element),
}
}

// Add adds a value to the cache.
func (c *Cache) Add(key Key, value interface{}) {
func (c *Cache) Add(key Key, value any) {
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.cache = make(map[any]*list.Element)
c.ll = list.New()
}
if ee, ok := c.cache[key]; ok {
Expand All @@ -71,7 +71,7 @@ func (c *Cache) Add(key Key, value interface{}) {
}

// Get looks up a key's value from the cache.
func (c *Cache) Get(key Key) (value interface{}, ok bool) {
func (c *Cache) Get(key Key) (value any, ok bool) {
if c.cache == nil {
return
}
Expand Down
14 changes: 7 additions & 7 deletions keymutex/keymutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_SingleLock_NoUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh := make(chan interface{})
callbackCh := make(chan any)

// Act
go lockAndCallback(km, key, callbackCh)
Expand All @@ -52,7 +52,7 @@ func Test_SingleLock_SingleUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh := make(chan interface{})
callbackCh := make(chan any)

// Act & Assert
go lockAndCallback(km, key, callbackCh)
Expand All @@ -65,8 +65,8 @@ func Test_DoubleLock_DoubleUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh1stLock := make(chan interface{})
callbackCh2ndLock := make(chan interface{})
callbackCh1stLock := make(chan any)
callbackCh2ndLock := make(chan any)

// Act & Assert
go lockAndCallback(km, key, callbackCh1stLock)
Expand All @@ -79,12 +79,12 @@ func Test_DoubleLock_DoubleUnlock(t *testing.T) {
}
}

func lockAndCallback(km KeyMutex, id string, callbackCh chan<- interface{}) {
func lockAndCallback(km KeyMutex, id string, callbackCh chan<- any) {
km.LockKey(id)
callbackCh <- true
}

func verifyCallbackHappens(t *testing.T, callbackCh <-chan interface{}) bool {
func verifyCallbackHappens(t *testing.T, callbackCh <-chan any) bool {
select {
case <-callbackCh:
return true
Expand All @@ -94,7 +94,7 @@ func verifyCallbackHappens(t *testing.T, callbackCh <-chan interface{}) bool {
}
}

func verifyCallbackDoesntHappens(t *testing.T, callbackCh <-chan interface{}) bool {
func verifyCallbackDoesntHappens(t *testing.T, callbackCh <-chan any) bool {
select {
case <-callbackCh:
t.Fatalf("Unexpected callback.")
Expand Down
6 changes: 3 additions & 3 deletions lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type Key = groupcache.Key
type EvictionFunc = func(key Key, value interface{})
type EvictionFunc = func(key Key, value any)

// Cache is a thread-safe fixed size LRU cache.
type Cache struct {
Expand Down Expand Up @@ -57,14 +57,14 @@ func (c *Cache) SetEvictionFunc(f EvictionFunc) error {
}

// Add adds a value to the cache.
func (c *Cache) Add(key Key, value interface{}) {
func (c *Cache) Add(key Key, value any) {
c.lock.Lock()
defer c.lock.Unlock()
c.cache.Add(key, value)
}

// Get looks up a key's value from the cache.
func (c *Cache) Get(key Key) (value interface{}, ok bool) {
func (c *Cache) Get(key Key) (value any, ok bool) {
c.lock.Lock()
defer c.lock.Unlock()
return c.cache.Get(key)
Expand Down
14 changes: 7 additions & 7 deletions lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type complexStruct struct {

var getTests = []struct {
name string
keyToAdd interface{}
keyToGet interface{}
keyToAdd any
keyToGet any
expectedOk bool
}{
{"string_hit", "myKey", "myKey", true},
Expand Down Expand Up @@ -116,9 +116,9 @@ func TestGetRace(t *testing.T) {

func TestEviction(t *testing.T) {
var seenKey Key
var seenVal interface{}
var seenVal any

lru := NewWithEvictionFunc(1, func(key Key, value interface{}) {
lru := NewWithEvictionFunc(1, func(key Key, value any) {
seenKey = key
seenVal = value
})
Expand All @@ -133,11 +133,11 @@ func TestEviction(t *testing.T) {

func TestSetEviction(t *testing.T) {
var seenKey Key
var seenVal interface{}
var seenVal any

lru := New(1)

err := lru.SetEvictionFunc(func(key Key, value interface{}) {
err := lru.SetEvictionFunc(func(key Key, value any) {
seenKey = key
seenVal = value
})
Expand All @@ -153,7 +153,7 @@ func TestSetEviction(t *testing.T) {
t.Errorf("unexpected eviction data: key=%v val=%v", seenKey, seenVal)
}

err = lru.SetEvictionFunc(func(key Key, value interface{}) {})
err = lru.SetEvictionFunc(func(key Key, value any) {})
if err == nil {
t.Errorf("expected error but got none")
}
Expand Down
2 changes: 1 addition & 1 deletion mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (mountError MountError) Error() string {
return mountError.Message
}

func NewMountError(mountErrorValue MountErrorType, format string, args ...interface{}) error {
func NewMountError(mountErrorValue MountErrorType, format string, args ...any) error {
mountError := MountError{
Type: mountErrorValue,
Message: fmt.Sprintf(format, args...),
Expand Down
2 changes: 1 addition & 1 deletion pointer/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestAllPtrFieldsNil(t *testing.T) {
testCases := []struct {
obj interface{}
obj any
expected bool
}{
{struct{}{}, true},
Expand Down
2 changes: 1 addition & 1 deletion ptr/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
//
// This function is only valid for structs and pointers to structs. Any other
// type will cause a panic. Passing a typed nil pointer will return true.
func AllPtrFieldsNil(obj interface{}) bool {
func AllPtrFieldsNil(obj any) bool {
v := reflect.ValueOf(obj)
if !v.IsValid() {
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
Expand Down
2 changes: 1 addition & 1 deletion ptr/ptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func TestAllPtrFieldsNil(t *testing.T) {
testCases := []struct {
obj interface{}
obj any
expected bool
}{
{struct{}{}, true},
Expand Down
2 changes: 1 addition & 1 deletion semantic/deep_equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import (
type Equalities = reflect.Equalities

// EqualitiesOrDie adds the given funcs and panics on any error.
func EqualitiesOrDie(funcs ...interface{}) Equalities {
func EqualitiesOrDie(funcs ...any) Equalities {
return reflect.EqualitiesOrDie(funcs...)
}
2 changes: 1 addition & 1 deletion set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New[E ordered](items ...E) Set[E] {
return ss
}

// KeySet creates a Set[E] from a keys of a map[E](? extends interface{}).
// KeySet creates a Set[E] from a keys of a map[E](? extends any).
func KeySet[E ordered, A any](theMap map[E]A) Set[E] {
ret := Set[E]{}
for key := range theMap {
Expand Down
Loading