From 53c83367f35ac88d243d1e9fe12d98b054460fb8 Mon Sep 17 00:00:00 2001 From: SeongUk Cho Date: Mon, 6 Feb 2023 22:26:30 +0900 Subject: [PATCH 1/2] generic codec --- go.mod | 2 +- websocket/websocket.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 1b6c3b80d3..dd851e214b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module golang.org/x/net -go 1.17 +go 1.18 require ( golang.org/x/sys v0.4.0 diff --git a/websocket/websocket.go b/websocket/websocket.go index 90a2257cd5..5dfb107e0c 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -298,13 +298,13 @@ func (ws *Conn) Config() *Config { return ws.config } func (ws *Conn) Request() *http.Request { return ws.request } // Codec represents a symmetric pair of functions that implement a codec. -type Codec struct { - Marshal func(v interface{}) (data []byte, payloadType byte, err error) +type Codec[T any] struct { + Marshal func(v T) (data []byte, payloadType byte, err error) Unmarshal func(data []byte, payloadType byte, v interface{}) (err error) } // Send sends v marshaled by cd.Marshal as single frame to ws. -func (cd Codec) Send(ws *Conn, v interface{}) (err error) { +func (cd Codec[T]) Send(ws *Conn, v T) (err error) { data, payloadType, err := cd.Marshal(v) if err != nil { return err @@ -326,7 +326,7 @@ func (cd Codec) Send(ws *Conn, v interface{}) (err error) { // limit, ErrFrameTooLarge is returned; in this case frame is not read off wire // completely. The next call to Receive would read and discard leftover data of // previous oversized frame before processing next frame. -func (cd Codec) Receive(ws *Conn, v interface{}) (err error) { +func (cd Codec[T]) Receive(ws *Conn, v interface{}) (err error) { ws.rio.Lock() defer ws.rio.Unlock() if ws.frameReader != nil { @@ -416,7 +416,7 @@ Trivial usage: data = []byte{0, 1, 2} websocket.Message.Send(ws, data) */ -var Message = Codec{marshal, unmarshal} +var Message = Codec[interface{}]{Marshal: marshal, Unmarshal: unmarshal} func jsonMarshal(v interface{}) (msg []byte, payloadType byte, err error) { msg, err = json.Marshal(v) @@ -446,4 +446,4 @@ Trivial usage: // send JSON type T websocket.JSON.Send(ws, data) */ -var JSON = Codec{jsonMarshal, jsonUnmarshal} +var JSON = Codec[interface{}]{Marshal: jsonMarshal, Unmarshal: jsonUnmarshal} From 2227acd407dd3f192c260f14f20f2ff93f384693 Mon Sep 17 00:00:00 2001 From: SeongUk Cho Date: Mon, 6 Feb 2023 22:27:35 +0900 Subject: [PATCH 2/2] changed deprecated ioutil functions to io --- websocket/websocket.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/websocket/websocket.go b/websocket/websocket.go index 5dfb107e0c..86812deb0a 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -17,7 +17,6 @@ import ( "encoding/json" "errors" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -208,7 +207,7 @@ again: n, err = ws.frameReader.Read(msg) if err == io.EOF { if trailer := ws.frameReader.TrailerReader(); trailer != nil { - io.Copy(ioutil.Discard, trailer) + io.Copy(io.Discard, trailer) } ws.frameReader = nil goto again @@ -330,7 +329,7 @@ func (cd Codec[T]) Receive(ws *Conn, v interface{}) (err error) { ws.rio.Lock() defer ws.rio.Unlock() if ws.frameReader != nil { - _, err = io.Copy(ioutil.Discard, ws.frameReader) + _, err = io.Copy(io.Discard, ws.frameReader) if err != nil { return err } @@ -362,7 +361,7 @@ again: return ErrFrameTooLarge } payloadType := frame.PayloadType() - data, err := ioutil.ReadAll(frame) + data, err := io.ReadAll(frame) if err != nil { return err }