chore: Cleanup code by switching to type switching instead of string switching.

This commit is contained in:
Tera << 8 2025-02-16 18:11:01 -05:00
parent 17e1491f96
commit 62cc8b39ad
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
3 changed files with 37 additions and 133 deletions

View file

@ -1,7 +1,6 @@
package backendutil_custom
import (
"fmt"
"net"
"os"
@ -37,21 +36,15 @@ func (helper *BackendApplicationHelper) Start() error {
log.Debug("Sucessfully connected")
for {
commandType, commandRaw, err := datacommands.Unmarshal(helper.socket)
_, commandRaw, err := datacommands.Unmarshal(helper.socket)
if err != nil && err.Error() != "couldn't match command ID" {
return err
}
switch commandType {
case "proxyConnectionsRequest":
proxyConnectionRequest, ok := commandRaw.(*datacommands.ProxyConnectionsRequest)
if !ok {
return fmt.Errorf("failed to typecast")
}
connections := helper.Backend.GetAllClientConnections(proxyConnectionRequest.ProxyID)
switch command := commandRaw.(type) {
case *datacommands.ProxyConnectionsRequest:
connections := helper.Backend.GetAllClientConnections(command.ProxyID)
serverParams := &datacommands.ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
@ -67,14 +60,8 @@ func (helper *BackendApplicationHelper) Start() error {
if _, err = helper.socket.Write(byteData); err != nil {
return err
}
case "removeProxy":
command, ok := commandRaw.(*datacommands.RemoveProxy)
if !ok {
return fmt.Errorf("failed to typecast")
}
ok, err = helper.Backend.StopProxy(command)
case *datacommands.RemoveProxy:
ok, err := helper.Backend.StopProxy(command)
var hasAnyFailed bool
if !ok {
@ -100,21 +87,15 @@ func (helper *BackendApplicationHelper) Start() error {
helper.socket.Write(responseMarshalled)
default:
commandType, commandRaw, err := commonbackend.Unmarshal(helper.socket)
_, commandRaw, err := commonbackend.Unmarshal(helper.socket)
if err != nil {
return err
}
switch commandType {
case "start":
command, ok := commandRaw.(*commonbackend.Start)
if !ok {
return fmt.Errorf("failed to typecast")
}
ok, err = helper.Backend.StartBackend(command.Arguments)
switch command := commandRaw.(type) {
case *commonbackend.Start:
ok, err := helper.Backend.StartBackend(command.Arguments)
var (
message string
@ -143,14 +124,8 @@ func (helper *BackendApplicationHelper) Start() error {
}
helper.socket.Write(responseMarshalled)
case "stop":
_, ok := commandRaw.(*commonbackend.Stop)
if !ok {
return fmt.Errorf("failed to typecast")
}
ok, err = helper.Backend.StopBackend()
case *commonbackend.Stop:
ok, err := helper.Backend.StopBackend()
var (
message string
@ -179,13 +154,7 @@ func (helper *BackendApplicationHelper) Start() error {
}
helper.socket.Write(responseMarshalled)
case "backendStatusRequest":
_, ok := commandRaw.(*commonbackend.BackendStatusRequest)
if !ok {
return fmt.Errorf("failed to typecast")
}
case *commonbackend.BackendStatusRequest:
ok, err := helper.Backend.GetBackendStatus()
var (
@ -215,13 +184,7 @@ func (helper *BackendApplicationHelper) Start() error {
}
helper.socket.Write(responseMarshalled)
case "addProxy":
command, ok := commandRaw.(*commonbackend.AddProxy)
if !ok {
return fmt.Errorf("failed to typecast")
}
case *commonbackend.AddProxy:
id, ok, err := helper.Backend.StartProxy(command)
var hasAnyFailed bool
@ -247,13 +210,7 @@ func (helper *BackendApplicationHelper) Start() error {
}
helper.socket.Write(responseMarshalled)
case "checkClientParameters":
command, ok := commandRaw.(*commonbackend.CheckClientParameters)
if !ok {
return fmt.Errorf("failed to typecast")
}
case *commonbackend.CheckClientParameters:
resp := helper.Backend.CheckParametersForConnections(command)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkClientParameters"
@ -267,13 +224,7 @@ func (helper *BackendApplicationHelper) Start() error {
if _, err = helper.socket.Write(byteData); err != nil {
return err
}
case "checkServerParameters":
command, ok := commandRaw.(*commonbackend.CheckServerParameters)
if !ok {
return fmt.Errorf("failed to typecast")
}
case *commonbackend.CheckServerParameters:
resp := helper.Backend.CheckParametersForBackend(command.Arguments)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkServerParameters"
@ -288,7 +239,7 @@ func (helper *BackendApplicationHelper) Start() error {
return err
}
default:
log.Warn("Unsupported command recieved: %s", commandType)
log.Warnf("Unsupported command recieved: %T", command)
}
}
}