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 ( import (
"io" "io"
"slices"
"testing" "testing"
"github.com/pion/mediadevices/pkg/codec" "github.com/pion/mediadevices/pkg/codec"
@@ -93,13 +94,7 @@ func TestMediaStreamFilters(t *testing.T) {
} }
for _, a := range actual { for _, a := range actual {
found := false found := slices.Contains(expected, a)
for _, e := range expected {
if e == a {
found = true
break
}
}
if !found { if !found {
t.Fatalf("%s: Expected to find %p in the query results", t.Name(), a) 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. // EncoderController is the interface allowing to control the encoder behaviour after it's initialisation.
// It will possibly have common control method in the future. // It will possibly have common control method in the future.
// A controller can have optional methods represented by *Controller interfaces // 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 // Controllable is a interface representing a encoder which can be controlled
// after it's initialisation with an EncoderController // after it's initialisation with an EncoderController

View File

@@ -1,7 +1,8 @@
// Package vnc implements a VNC client. // Package vnc implements a VNC client.
// //
// References: // References:
// [PROTOCOL]: http://tools.ietf.org/html/rfc6143 //
// [PROTOCOL]: http://tools.ietf.org/html/rfc6143
package vnc package vnc
import ( import (
@@ -96,7 +97,7 @@ func (c *ClientConn) CutText(text string) error {
var buf bytes.Buffer var buf bytes.Buffer
// This is the fixed size data we'll send // This is the fixed size data we'll send
fixedData := []interface{}{ fixedData := []any{
uint8(6), uint8(6),
uint8(0), uint8(0),
uint8(0), uint8(0),
@@ -141,7 +142,7 @@ func (c *ClientConn) FramebufferUpdateRequest(incremental bool, x, y, width, hei
incrementalByte = 1 incrementalByte = 1
} }
data := []interface{}{ data := []any{
uint8(3), uint8(3),
incrementalByte, incrementalByte,
x, y, width, height, x, y, width, height,
@@ -172,7 +173,7 @@ func (c *ClientConn) KeyEvent(keysym uint32, down bool) error {
downFlag = 1 downFlag = 1
} }
data := []interface{}{ data := []any{
uint8(4), uint8(4),
downFlag, downFlag,
uint8(0), 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 { func (c *ClientConn) PointerEvent(mask ButtonMask, x, y uint16) error {
var buf bytes.Buffer var buf bytes.Buffer
data := []interface{}{ data := []any{
uint8(5), uint8(5),
uint8(mask), uint8(mask),
x, x,
@@ -225,7 +226,7 @@ func (c *ClientConn) PointerEvent(mask ButtonMask, x, y uint16) error {
// //
// See RFC 6143 Section 7.5.2 // See RFC 6143 Section 7.5.2
func (c *ClientConn) SetEncodings(encs []Encoding) error { func (c *ClientConn) SetEncodings(encs []Encoding) error {
data := make([]interface{}, 3+len(encs)) data := make([]any, 3+len(encs))
data[0] = uint8(2) data[0] = uint8(2)
data[1] = uint8(0) data[1] = uint8(0)
data[2] = uint16(len(encs)) data[2] = uint16(len(encs))
@@ -319,7 +320,7 @@ func (c *ClientConn) handshake() error {
} }
// Respond with the version we will support // Respond with the version we will support
if maxMinor<8 { if maxMinor < 8 {
if _, err = c.c.Write([]byte("RFB 003.003\n")); err != nil { if _, err = c.c.Write([]byte("RFB 003.003\n")); err != nil {
return err return err
} }
@@ -331,7 +332,7 @@ func (c *ClientConn) handshake() error {
if numSecurityTypes == 0 { if numSecurityTypes == 0 {
return fmt.Errorf("no security types: %s", c.readErrorReason()) return fmt.Errorf("no security types: %s", c.readErrorReason())
} }
}else{ } else {
if _, err = c.c.Write([]byte("RFB 003.008\n")); err != nil { if _, err = c.c.Write([]byte("RFB 003.008\n")); err != nil {
return err return err
} }

View File

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

View File

@@ -27,7 +27,7 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
coreConfig = config.Core coreConfig = config.Core
} }
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (interface{}, func(), error) { broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (any, func(), error) {
return source.Read() return source.Read()
}), coreConfig) }), 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 // 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. // in the ring buffer.
func (broadcaster *Broadcaster) NewReader(copyChunk bool) Reader { func (broadcaster *Broadcaster) NewReader(copyChunk bool) Reader {
copyFn := func(src interface{}) interface{} { return src } copyFn := func(src any) any { return src }
if copyChunk { if copyChunk {
buffer := wave.NewBuffer() buffer := wave.NewBuffer()
copyFn = func(src interface{}) interface{} { copyFn = func(src any) any {
realSrc, _ := src.(wave.Audio) realSrc, _ := src.(wave.Audio)
buffer.StoreCopy(realSrc) buffer.StoreCopy(realSrc)
return buffer.Load() return buffer.Load()
@@ -60,7 +60,7 @@ func (broadcaster *Broadcaster) NewReader(copyChunk bool) Reader {
// ReplaceSource replaces the underlying source. This operation is thread safe. // ReplaceSource replaces the underlying source. This operation is thread safe.
func (broadcaster *Broadcaster) ReplaceSource(source Reader) error { 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() return source.Read()
})) }))
} }

View File

@@ -17,7 +17,7 @@ const (
var errEmptySource = fmt.Errorf("Source can't be nil") var errEmptySource = fmt.Errorf("Source can't be nil")
type broadcasterData struct { type broadcasterData struct {
data interface{} data any
count uint32 count uint32
err error 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 // 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 // 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. // 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() currentCount := broadcaster.buffer.lastCount()
return ReaderFunc(func() (data interface{}, release func(), err error) { return ReaderFunc(func() (data any, release func(), err error) {
currentCount++ currentCount++
if push := broadcaster.buffer.acquire(currentCount); push != nil { if push := broadcaster.buffer.acquire(currentCount); push != nil {
data, _, err = broadcaster.source.Load().(Reader).Read() data, _, err = broadcaster.source.Load().(Reader).Read()

View File

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

View File

@@ -27,7 +27,7 @@ func NewBroadcaster(source Reader, config *BroadcasterConfig) *Broadcaster {
coreConfig = config.Core coreConfig = config.Core
} }
broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (interface{}, func(), error) { broadcaster := io.NewBroadcaster(io.ReaderFunc(func() (any, func(), error) {
return source.Read() return source.Read()
}), coreConfig) }), 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 // 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. // in the ring buffer.
func (broadcaster *Broadcaster) NewReader(copyFrame bool) Reader { func (broadcaster *Broadcaster) NewReader(copyFrame bool) Reader {
copyFn := func(src interface{}) interface{} { return src } copyFn := func(src any) any { return src }
if copyFrame { if copyFrame {
buffer := NewFrameBuffer(0) buffer := NewFrameBuffer(0)
copyFn = func(src interface{}) interface{} { copyFn = func(src any) any {
realSrc, _ := src.(image.Image) realSrc, _ := src.(image.Image)
buffer.StoreCopy(realSrc) buffer.StoreCopy(realSrc)
return buffer.Load() return buffer.Load()
@@ -60,7 +60,7 @@ func (broadcaster *Broadcaster) NewReader(copyFrame bool) Reader {
// ReplaceSource replaces the underlying source. This operation is thread safe. // ReplaceSource replaces the underlying source. This operation is thread safe.
func (broadcaster *Broadcaster) ReplaceSource(source Reader) error { 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() return source.Read()
})) }))
} }

View File

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

View File

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

View File

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

View File

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

View File

@@ -32,7 +32,7 @@ func (m *Media) String() string {
return prettifyStruct(m) return prettifyStruct(m)
} }
func prettifyStruct(i interface{}) string { func prettifyStruct(i any) string {
var rows []string var rows []string
var addRows func(int, reflect.Value) var addRows func(int, reflect.Value)
addRows = func(level int, obj 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 // 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. // 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() rp := reflect.ValueOf(p).Elem()
ro := reflect.ValueOf(o) ro := reflect.ValueOf(o)
@@ -159,13 +159,13 @@ func (p *MediaConstraints) FitnessDistance(o Media) (float64, bool) {
} }
type comparisons []struct { 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 { if desired != nil {
*c = append(*c, *c = append(*c,
struct{ desired, actual interface{} }{ struct{ desired, actual any }{
desired, actual, desired, actual,
}, },
) )

View File

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