Go generics and changes in v1.18 (#5)

* ArrayIn to use generics.

* interface{} -> any.
This commit is contained in:
Miroslav Šedivý
2022-07-28 12:20:20 +02:00
committed by GitHub
parent c725e96c90
commit babddacbf3
26 changed files with 100 additions and 110 deletions

View File

@ -79,14 +79,14 @@ type VideoConfig struct {
}
func (config *VideoConfig) GetPipeline(screen ScreenSize) (string, error) {
values := map[string]interface{}{
values := map[string]any{
"width": screen.Width,
"height": screen.Height,
"fps": screen.Rate,
}
language := []gval.Language{
gval.Function("round", func(args ...interface{}) (interface{}, error) {
gval.Function("round", func(args ...any) (any, error) {
return (int)(math.Round(args[0].(float64))), nil
}),
}

View File

@ -32,9 +32,9 @@ type SystemAdmin struct {
type SystemLogs = []SystemLog
type SystemLog struct {
Level string `json:"level"`
Fields map[string]interface{} `json:"fields"`
Message string `json:"message"`
Level string `json:"level"`
Fields map[string]any `json:"fields"`
Message string `json:"message"`
}
type SystemDisconnect struct {
@ -163,14 +163,14 @@ type BroadcastStatus struct {
/////////////////////////////
type SendUnicast struct {
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Subject string `json:"subject"`
Body interface{} `json:"body"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Subject string `json:"subject"`
Body any `json:"body"`
}
type SendBroadcast struct {
Sender string `json:"sender"`
Subject string `json:"subject"`
Body interface{} `json:"body"`
Sender string `json:"sender"`
Subject string `json:"subject"`
Body any `json:"body"`
}

View File

@ -43,7 +43,7 @@ type Session interface {
SetWebSocketPeer(websocketPeer WebSocketPeer)
SetWebSocketConnected(websocketPeer WebSocketPeer, connected bool)
GetWebSocketPeer() WebSocketPeer
Send(event string, payload interface{})
Send(event string, payload any)
// webrtc
SetWebRTCPeer(webrtcPeer WebRTCPeer)
@ -66,9 +66,9 @@ type SessionManager interface {
SetCursor(cursor Cursor, session Session)
PopCursors() map[Session][]Cursor
Broadcast(event string, payload interface{}, exclude interface{})
AdminBroadcast(event string, payload interface{}, exclude interface{})
InactiveCursorsBroadcast(event string, payload interface{}, exclude interface{})
Broadcast(event string, payload any, exclude ...string)
AdminBroadcast(event string, payload any, exclude ...string)
InactiveCursorsBroadcast(event string, payload any, exclude ...string)
OnCreated(listener func(session Session))
OnDeleted(listener func(session Session))

View File

@ -15,7 +15,7 @@ type WebSocketHandler func(Session, WebSocketMessage) bool
type CheckOrigin func(r *http.Request) bool
type WebSocketPeer interface {
Send(event string, payload interface{})
Send(event string, payload any)
Ping() error
Destroy(reason string)
}

View File

@ -1,22 +1,12 @@
package utils
import (
"reflect"
)
func ArrayIn[T comparable](val T, array []T) (exists bool, index int) {
exists, index = false, -1
func ArrayIn(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) {
index = i
exists = true
return
}
for i, a := range array {
if a == val {
exists, index = true, i
return
}
}

View File

@ -29,6 +29,6 @@ func Color(str string) string {
return result + str[lastIndex:]
}
func Colorf(format string, a ...interface{}) string {
func Colorf(format string, a ...any) string {
return fmt.Sprintf(Color(format), a...)
}

View File

@ -9,7 +9,7 @@ import (
"github.com/rs/zerolog/log"
)
func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res interface{}) error {
func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res any) error {
err := json.NewDecoder(r.Body).Decode(res)
if err == nil {
@ -23,7 +23,7 @@ func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res interface{}) er
return HttpBadRequest("unable to parse provided data").WithInternalErr(err)
}
func HttpJsonResponse(w http.ResponseWriter, code int, res interface{}) {
func HttpJsonResponse(w http.ResponseWriter, code int, res any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
@ -32,7 +32,7 @@ func HttpJsonResponse(w http.ResponseWriter, code int, res interface{}) {
}
}
func HttpSuccess(w http.ResponseWriter, res ...interface{}) error {
func HttpSuccess(w http.ResponseWriter, res ...any) error {
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
} else {
@ -78,13 +78,13 @@ func (e *HTTPError) WithInternalMsg(msg string) *HTTPError {
}
// WithInternalMsg adds internal formated message information to the error
func (e *HTTPError) WithInternalMsgf(fmtStr string, args ...interface{}) *HTTPError {
func (e *HTTPError) WithInternalMsgf(fmtStr string, args ...any) *HTTPError {
e.InternalMsg = fmt.Sprintf(fmtStr, args...)
return e
}
// Sends error with custom formated message
func (e *HTTPError) Msgf(fmtSt string, args ...interface{}) *HTTPError {
func (e *HTTPError) Msgf(fmtSt string, args ...any) *HTTPError {
e.Message = fmt.Sprintf(fmtSt, args...)
return e
}

View File

@ -5,15 +5,15 @@ import (
"reflect"
)
func Unmarshal(in interface{}, raw []byte, callback func() error) error {
func Unmarshal(in any, raw []byte, callback func() error) error {
if err := json.Unmarshal(raw, &in); err != nil {
return err
}
return callback()
}
func JsonStringAutoDecode(m interface{}) func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) {
return func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) {
func JsonStringAutoDecode(m any) func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
return func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
if rf != reflect.String || rt == reflect.String {
return data, nil
}