From c0721738c44f841ef3e47e296f0d848e07e8863c Mon Sep 17 00:00:00 2001 From: philipch07 <59272129+philipch07@users.noreply.github.com> Date: Sun, 14 Sep 2025 21:55:37 -0400 Subject: [PATCH] Apply go modernize (#650) --- mediastream_test.go | 9 ++------- pkg/codec/codec.go | 2 +- pkg/driver/vncdriver/vnc/client.go | 17 +++++++++-------- pkg/driver/vncdriver/vnc/server_messages.go | 4 ++-- pkg/io/audio/broadcast.go | 8 ++++---- pkg/io/broadcast.go | 6 +++--- pkg/io/broadcast_test.go | 4 ++-- pkg/io/reader.go | 6 +++--- pkg/io/video/broadcast.go | 8 ++++---- pkg/prop/duration.go | 7 +++---- pkg/prop/float.go | 7 +++---- pkg/prop/format.go | 7 +++---- pkg/prop/int.go | 7 +++---- pkg/prop/prop.go | 10 +++++----- pkg/prop/string.go | 7 +++---- 15 files changed, 50 insertions(+), 59 deletions(-) diff --git a/mediastream_test.go b/mediastream_test.go index 8518e04..14fd212 100644 --- a/mediastream_test.go +++ b/mediastream_test.go @@ -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) diff --git a/pkg/codec/codec.go b/pkg/codec/codec.go index 8521d8b..f584c8f 100644 --- a/pkg/codec/codec.go +++ b/pkg/codec/codec.go @@ -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 diff --git a/pkg/driver/vncdriver/vnc/client.go b/pkg/driver/vncdriver/vnc/client.go index ba94d17..83117f3 100644 --- a/pkg/driver/vncdriver/vnc/client.go +++ b/pkg/driver/vncdriver/vnc/client.go @@ -1,7 +1,8 @@ // Package vnc implements a VNC client. // // References: -// [PROTOCOL]: http://tools.ietf.org/html/rfc6143 +// +// [PROTOCOL]: http://tools.ietf.org/html/rfc6143 package vnc import ( @@ -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)) @@ -319,7 +320,7 @@ func (c *ClientConn) handshake() error { } // 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 { return err } @@ -331,7 +332,7 @@ func (c *ClientConn) handshake() error { if numSecurityTypes == 0 { return fmt.Errorf("no security types: %s", c.readErrorReason()) } - }else{ + } else { if _, err = c.c.Write([]byte("RFB 003.008\n")); err != nil { return err } diff --git a/pkg/driver/vncdriver/vnc/server_messages.go b/pkg/driver/vncdriver/vnc/server_messages.go index 6f2eeed..b25cde0 100644 --- a/pkg/driver/vncdriver/vnc/server_messages.go +++ b/pkg/driver/vncdriver/vnc/server_messages.go @@ -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, diff --git a/pkg/io/audio/broadcast.go b/pkg/io/audio/broadcast.go index 6f6fe84..70634df 100644 --- a/pkg/io/audio/broadcast.go +++ b/pkg/io/audio/broadcast.go @@ -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() })) } diff --git a/pkg/io/broadcast.go b/pkg/io/broadcast.go index eafac6c..7a2f2ff 100644 --- a/pkg/io/broadcast.go +++ b/pkg/io/broadcast.go @@ -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() diff --git a/pkg/io/broadcast_test.go b/pkg/io/broadcast_test.go index 01fefb9..368286a 100644 --- a/pkg/io/broadcast_test.go +++ b/pkg/io/broadcast_test.go @@ -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 diff --git a/pkg/io/reader.go b/pkg/io/reader.go index 7471e65..2ee949e 100644 --- a/pkg/io/reader.go +++ b/pkg/io/reader.go @@ -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 } diff --git a/pkg/io/video/broadcast.go b/pkg/io/video/broadcast.go index b042485..e8b0a77 100644 --- a/pkg/io/video/broadcast.go +++ b/pkg/io/video/broadcast.go @@ -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() })) } diff --git a/pkg/prop/duration.go b/pkg/prop/duration.go index ba16541..529123a 100644 --- a/pkg/prop/duration.go +++ b/pkg/prop/duration.go @@ -3,6 +3,7 @@ package prop import ( "fmt" "math" + "slices" "strings" "time" ) @@ -54,10 +55,8 @@ type DurationOneOf []time.Duration // Compare implements DurationConstraint. func (d DurationOneOf) Compare(a time.Duration) (float64, bool) { - for _, ii := range d { - if ii == a { - return 0.0, true - } + if slices.Contains(d, a) { + return 0.0, true } return 1.0, false } diff --git a/pkg/prop/float.go b/pkg/prop/float.go index 7f36584..56b5c80 100644 --- a/pkg/prop/float.go +++ b/pkg/prop/float.go @@ -3,6 +3,7 @@ package prop import ( "fmt" "math" + "slices" "strings" ) @@ -53,10 +54,8 @@ type FloatOneOf []float32 // Compare implements FloatConstraint. func (f FloatOneOf) Compare(a float32) (float64, bool) { - for _, ff := range f { - if ff == a { - return 0.0, true - } + if slices.Contains(f, a) { + return 0.0, true } return 1.0, false } diff --git a/pkg/prop/format.go b/pkg/prop/format.go index 6a4be04..ac56d02 100644 --- a/pkg/prop/format.go +++ b/pkg/prop/format.go @@ -3,6 +3,7 @@ package prop import ( "fmt" "github.com/pion/mediadevices/pkg/frame" + "slices" "strings" ) @@ -56,10 +57,8 @@ type FrameFormatOneOf []frame.Format // Compare implements FrameFormatConstraint. func (f FrameFormatOneOf) Compare(a frame.Format) (float64, bool) { - for _, ff := range f { - if ff == a { - return 0.0, true - } + if slices.Contains(f, a) { + return 0.0, true } return 1.0, false } diff --git a/pkg/prop/int.go b/pkg/prop/int.go index d3c983b..75e4d09 100644 --- a/pkg/prop/int.go +++ b/pkg/prop/int.go @@ -3,6 +3,7 @@ package prop import ( "fmt" "math" + "slices" "strings" ) @@ -53,10 +54,8 @@ type IntOneOf []int // Compare implements IntConstraint. func (i IntOneOf) Compare(a int) (float64, bool) { - for _, ii := range i { - if ii == a { - return 0.0, true - } + if slices.Contains(i, a) { + return 0.0, true } return 1.0, false } diff --git a/pkg/prop/prop.go b/pkg/prop/prop.go index 3aafe94..80cc937 100644 --- a/pkg/prop/prop.go +++ b/pkg/prop/prop.go @@ -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, }, ) diff --git a/pkg/prop/string.go b/pkg/prop/string.go index 0e898c4..a0a7222 100644 --- a/pkg/prop/string.go +++ b/pkg/prop/string.go @@ -2,6 +2,7 @@ package prop import ( "fmt" + "slices" "strings" ) @@ -55,10 +56,8 @@ type StringOneOf []string // Compare implements StringConstraint. func (f StringOneOf) Compare(a string) (float64, bool) { - for _, ff := range f { - if ff == a { - return 0.0, true - } + if slices.Contains(f, a) { + return 0.0, true } return 1.0, false }