From 855c3fec30f31b08a153d8232415034a3e5b27f5 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 9 Nov 2021 16:44:11 -0500 Subject: [PATCH] Changing from an array to a map --- pkg/netceptor/netceptor.go | 18 +++++++++++++----- pkg/netceptor/netceptor_test.go | 8 ++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/netceptor/netceptor.go b/pkg/netceptor/netceptor.go index 6382580a6..7353a7b45 100644 --- a/pkg/netceptor/netceptor.go +++ b/pkg/netceptor/netceptor.go @@ -218,16 +218,23 @@ type routingUpdate struct { } const ( - // If adding/changing an ConnType, make sure to change ConnTypeStrings as well. These are friendly strings printed out in a status command + // If adding/changing an ConnType, make sure to change ConnTypeStrings as well. + // These are friendly strings printed out in a status command. // ConnTypeDatagram indicates a packetconn (datagram) service listener. ConnTypeDatagram = 0 // ConnTypeStream indicates a conn (stream) service listener, without a user-defined TLS. ConnTypeStream = 1 // ConnTypeStreamTLS indicates the service listens on a packetconn connection, with a user-defined TLS. ConnTypeStreamTLS = 2 + // Default Label for an unknown connection type. + UnknownConnTypeStr = "Unknown" ) -var ConnTypeStrings = [...]string{"Datagram", "Stream", "StreamTLS"} +var ConnTypeStrings = map[byte]string{ + ConnTypeDatagram: "Datagram", + ConnTypeStream: "Stream", + ConnTypeStreamTLS: "StreamTLS", +} // WorkCommand tracks available work types and whether they verify work submissions. type WorkCommand struct { @@ -444,11 +451,12 @@ func (s *Netceptor) MaxConnectionIdleTime() time.Duration { // Convert the connection type to a string. func (s *Netceptor) GetConnectionTypeAsString(connectionType byte) string { // A byte can't be < 0 so we don't need to check the lower bounds - if connectionType < byte(len(ConnTypeStrings)) { - return ConnTypeStrings[connectionType] + connTypeString, ok := ConnTypeStrings[connectionType] + if !ok { + connTypeString = UnknownConnTypeStr } - return "Unknown" + return connTypeString } type backendInfo struct { diff --git a/pkg/netceptor/netceptor_test.go b/pkg/netceptor/netceptor_test.go index dda2fae8b..c39c00e8e 100644 --- a/pkg/netceptor/netceptor_test.go +++ b/pkg/netceptor/netceptor_test.go @@ -795,11 +795,11 @@ func TestConnTypeString(t *testing.T) { }() n1 := New(context.Background(), "node1") - if ConnTypeStrings[ConnTypeDatagram] != n1.GetConnectionTypeAsString(0) { - t.Fatal("Datagram should be the first entry in ConnTypeStrings") + if ConnTypeStrings[ConnTypeDatagram] != n1.GetConnectionTypeAsString(ConnTypeDatagram) { + t.Fatal("The function did not properly return the constant for a datagram type") } - if n1.GetConnectionTypeAsString(254) != "Unknown" { - t.Fatal("Either we now have 254 ConnTypes or GetConnectionTypeAsString did not properly return Unknown") + if n1.GetConnectionTypeAsString(254) != UnknownConnTypeStr { + t.Fatal("Either we now have 254 ConnTypes or GetConnectionTypeAsString did not properly return the string constant") } // Shutdown the network n1.Shutdown()