Apply go modernize (#650)

This commit is contained in:
philipch07
2025-09-14 21:55:37 -04:00
committed by GitHub
parent 6047a32ea0
commit c0721738c4
15 changed files with 50 additions and 59 deletions

View File

@@ -2,6 +2,7 @@ package mediadevices
import (
"io"
"slices"
"testing"
"github.com/pion/mediadevices/pkg/codec"
@@ -93,13 +94,7 @@ func TestMediaStreamFilters(t *testing.T) {
}
for _, a := range actual {
found := false
for _, e := range expected {
if e == a {
found = true
break
}
}
found := slices.Contains(expected, a)
if !found {
t.Fatalf("%s: Expected to find %p in the query results", t.Name(), a)

View File

@@ -156,7 +156,7 @@ type ReadCloser interface {
// EncoderController is the interface allowing to control the encoder behaviour after it's initialisation.
// It will possibly have common control method in the future.
// A controller can have optional methods represented by *Controller interfaces
type EncoderController interface{}
type EncoderController any
// Controllable is a interface representing a encoder which can be controlled
// after it's initialisation with an EncoderController

View File

@@ -1,6 +1,7 @@
// Package vnc implements a VNC client.
//
// References:
//
// [PROTOCOL]: http://tools.ietf.org/html/rfc6143
package vnc
@@ -96,7 +97,7 @@ func (c *ClientConn) CutText(text string) error {
var buf bytes.Buffer
// This is the fixed size data we'll send
fixedData := []interface{}{
fixedData := []any{
uint8(6),
uint8(0),
uint8(0),
@@ -141,7 +142,7 @@ func (c *ClientConn) FramebufferUpdateRequest(incremental bool, x, y, width, hei
incrementalByte = 1
}
data := []interface{}{
data := []any{
uint8(3),
incrementalByte,
x, y, width, height,
@@ -172,7 +173,7 @@ func (c *ClientConn) KeyEvent(keysym uint32, down bool) error {
downFlag = 1
}
data := []interface{}{
data := []any{
uint8(4),
downFlag,
uint8(0),
@@ -199,7 +200,7 @@ func (c *ClientConn) KeyEvent(keysym uint32, down bool) error {
func (c *ClientConn) PointerEvent(mask ButtonMask, x, y uint16) error {
var buf bytes.Buffer
data := []interface{}{
data := []any{
uint8(5),
uint8(mask),
x,
@@ -225,7 +226,7 @@ func (c *ClientConn) PointerEvent(mask ButtonMask, x, y uint16) error {
//
// See RFC 6143 Section 7.5.2
func (c *ClientConn) SetEncodings(encs []Encoding) error {
data := make([]interface{}, 3+len(encs))
data := make([]any, 3+len(encs))
data[0] = uint8(2)
data[1] = uint8(0)
data[2] = uint16(len(encs))

View File

@@ -63,7 +63,7 @@ func (*FramebufferUpdateMessage) Read(c *ClientConn, r io.Reader) (ServerMessage
var encodingType int32
rect := &rects[i]
data := []interface{}{
data := []any{
&rect.X,
&rect.Y,
&rect.Width,
@@ -128,7 +128,7 @@ func (*SetColorMapEntriesMessage) Read(c *ClientConn, r io.Reader) (ServerMessag
for i := uint16(0); i < numColors; i++ {
color := &result.Colors[i]
data := []interface{}{
data := []any{
&color.R,
&color.G,
&color.B,

View File

@@ -27,7 +27,7 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
coreConfig = config.Core
}
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (interface{}, func(), error) {
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (any, func(), error) {
return source.Read()
}), coreConfig)
@@ -39,11 +39,11 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
// buffer, this means that slow readers might miss some data if they're really late and the data is no longer
// in the ring buffer.
func (broadcaster *Broadcaster) NewReader(copyChunk bool) Reader {
copyFn := func(src interface{}) interface{} { return src }
copyFn := func(src any) any { return src }
if copyChunk {
buffer := wave.NewBuffer()
copyFn = func(src interface{}) interface{} {
copyFn = func(src any) any {
realSrc, _ := src.(wave.Audio)
buffer.StoreCopy(realSrc)
return buffer.Load()
@@ -60,7 +60,7 @@ func (broadcaster *Broadcaster) NewReader(copyChunk bool) Reader {
// ReplaceSource replaces the underlying source. This operation is thread safe.
func (broadcaster *Broadcaster) ReplaceSource(source Reader) error {
return broadcaster.ioBroadcaster.ReplaceSource(io.ReaderFunc(func() (interface{}, func(), error) {
return broadcaster.ioBroadcaster.ReplaceSource(io.ReaderFunc(func() (any, func(), error) {
return source.Read()
}))
}

View File

@@ -17,7 +17,7 @@ const (
var errEmptySource = fmt.Errorf("Source can't be nil")
type broadcasterData struct {
data interface{}
data any
count uint32
err error
}
@@ -124,10 +124,10 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
// copyFn is used to copy the data from the source to individual readers. Broadcaster uses a small ring
// buffer, this means that slow readers might miss some data if they're really late and the data is no longer
// in the ring buffer.
func (broadcaster *Broadcaster) NewReader(copyFn func(interface{}) interface{}) Reader {
func (broadcaster *Broadcaster) NewReader(copyFn func(any) any) Reader {
currentCount := broadcaster.buffer.lastCount()
return ReaderFunc(func() (data interface{}, release func(), err error) {
return ReaderFunc(func() (data any, release func(), err error) {
currentCount++
if push := broadcaster.buffer.acquire(currentCount); push != nil {
data, _, err = broadcaster.source.Load().(Reader).Read()

View File

@@ -57,7 +57,7 @@ func TestBroadcast(t *testing.T) {
frameCount := 0
frameSent := 0
lastSend := time.Now()
src = ReaderFunc(func() (interface{}, func(), error) {
src = ReaderFunc(func() (any, func(), error) {
if pauseCond.src && frameSent == 30 {
time.Sleep(time.Second)
}
@@ -85,7 +85,7 @@ func TestBroadcast(t *testing.T) {
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
reader := broadcaster.NewReader(func(src interface{}) interface{} { return src })
reader := broadcaster.NewReader(func(src any) any { return src })
count := 0
lastFrameCount := -1
droppedFrames := 0

View File

@@ -11,13 +11,13 @@ type Reader interface {
// there will be new allocations during streaming, and old unused memory will become garbage. As a consequence,
// these garbage will put a lot of pressure to the garbage collector and makes it to run more often and finish
// slower as the heap memory usage increases and more garbage to collect.
Read() (data interface{}, release func(), err error)
Read() (data any, release func(), err error)
}
// ReaderFunc is a proxy type for Reader
type ReaderFunc func() (data interface{}, release func(), err error)
type ReaderFunc func() (data any, release func(), err error)
func (f ReaderFunc) Read() (data interface{}, release func(), err error) {
func (f ReaderFunc) Read() (data any, release func(), err error) {
data, release, err = f()
return
}

View File

@@ -27,7 +27,7 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
coreConfig = config.Core
}
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (interface{}, func(), error) {
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (any, func(), error) {
return source.Read()
}), coreConfig)
@@ -39,11 +39,11 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
// buffer, this means that slow readers might miss some data if they're really late and the data is no longer
// in the ring buffer.
func (broadcaster *Broadcaster) NewReader(copyFrame bool) Reader {
copyFn := func(src interface{}) interface{} { return src }
copyFn := func(src any) any { return src }
if copyFrame {
buffer := NewFrameBuffer(0)
copyFn = func(src interface{}) interface{} {
copyFn = func(src any) any {
realSrc, _ := src.(image.Image)
buffer.StoreCopy(realSrc)
return buffer.Load()
@@ -60,7 +60,7 @@ func (broadcaster *Broadcaster) NewReader(copyFrame bool) Reader {
// ReplaceSource replaces the underlying source. This operation is thread safe.
func (broadcaster *Broadcaster) ReplaceSource(source Reader) error {
return broadcaster.ioBroadcaster.ReplaceSource(io.ReaderFunc(func() (interface{}, func(), error) {
return broadcaster.ioBroadcaster.ReplaceSource(io.ReaderFunc(func() (any, func(), error) {
return source.Read()
}))
}

View File

@@ -3,6 +3,7 @@ package prop
import (
"fmt"
"math"
"slices"
"strings"
"time"
)
@@ -54,11 +55,9 @@ type DurationOneOf []time.Duration
// Compare implements DurationConstraint.
func (d DurationOneOf) Compare(a time.Duration) (float64, bool) {
for _, ii := range d {
if ii == a {
if slices.Contains(d, a) {
return 0.0, true
}
}
return 1.0, false
}

View File

@@ -3,6 +3,7 @@ package prop
import (
"fmt"
"math"
"slices"
"strings"
)
@@ -53,11 +54,9 @@ type FloatOneOf []float32
// Compare implements FloatConstraint.
func (f FloatOneOf) Compare(a float32) (float64, bool) {
for _, ff := range f {
if ff == a {
if slices.Contains(f, a) {
return 0.0, true
}
}
return 1.0, false
}

View File

@@ -3,6 +3,7 @@ package prop
import (
"fmt"
"github.com/pion/mediadevices/pkg/frame"
"slices"
"strings"
)
@@ -56,11 +57,9 @@ type FrameFormatOneOf []frame.Format
// Compare implements FrameFormatConstraint.
func (f FrameFormatOneOf) Compare(a frame.Format) (float64, bool) {
for _, ff := range f {
if ff == a {
if slices.Contains(f, a) {
return 0.0, true
}
}
return 1.0, false
}

View File

@@ -3,6 +3,7 @@ package prop
import (
"fmt"
"math"
"slices"
"strings"
)
@@ -53,11 +54,9 @@ type IntOneOf []int
// Compare implements IntConstraint.
func (i IntOneOf) Compare(a int) (float64, bool) {
for _, ii := range i {
if ii == a {
if slices.Contains(i, a) {
return 0.0, true
}
}
return 1.0, false
}

View File

@@ -32,7 +32,7 @@ func (m *Media) String() string {
return prettifyStruct(m)
}
func prettifyStruct(i interface{}) string {
func prettifyStruct(i any) string {
var rows []string
var addRows func(int, reflect.Value)
addRows = func(level int, obj reflect.Value) {
@@ -67,7 +67,7 @@ type setterFn func(fieldA, fieldB reflect.Value)
// merge merges all the field values from o to p, except zero values. It's guaranteed that setterFn will be called
// when fieldA and fieldB are not struct.
func (p *Media) merge(o interface{}, set setterFn) {
func (p *Media) merge(o any, set setterFn) {
rp := reflect.ValueOf(p).Elem()
ro := reflect.ValueOf(o)
@@ -159,13 +159,13 @@ func (p *MediaConstraints) FitnessDistance(o Media) (float64, bool) {
}
type comparisons []struct {
desired, actual interface{}
desired, actual any
}
func (c *comparisons) add(desired, actual interface{}) {
func (c *comparisons) add(desired, actual any) {
if desired != nil {
*c = append(*c,
struct{ desired, actual interface{} }{
struct{ desired, actual any }{
desired, actual,
},
)

View File

@@ -2,6 +2,7 @@ package prop
import (
"fmt"
"slices"
"strings"
)
@@ -55,11 +56,9 @@ type StringOneOf []string
// Compare implements StringConstraint.
func (f StringOneOf) Compare(a string) (float64, bool) {
for _, ff := range f {
if ff == a {
if slices.Contains(f, a) {
return 0.0, true
}
}
return 1.0, false
}