From 73469075e63829227e981bbccfb9cf506d89d15e Mon Sep 17 00:00:00 2001 From: Matthew Sevey <15232757+MSevey@users.noreply.github.com> Date: Tue, 4 Jun 2024 03:43:31 -0400 Subject: [PATCH] chore: update instance type for better type safety (#412) use 0 value as unknown to force intentional use of type --- pkg/instance/type.go | 17 ++++++++++---- pkg/instance/type_test.go | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 pkg/instance/type_test.go diff --git a/pkg/instance/type.go b/pkg/instance/type.go index f71ed3d6..13438834 100644 --- a/pkg/instance/type.go +++ b/pkg/instance/type.go @@ -5,15 +5,24 @@ type InstanceType int // Possible types of the instance const ( - BasicInstance InstanceType = iota + UnknownInstance InstanceType = iota + BasicInstance ExecutorInstance TimeoutHandlerInstance ) // String returns the string representation of the type func (s InstanceType) String() string { - if s < 0 || s > 2 { - return "Unknown" + switch s { + case BasicInstance: + return "BasicInstance" + case ExecutorInstance: + return "ExecutorInstance" + case TimeoutHandlerInstance: + return "TimeoutHandlerInstance" + case UnknownInstance: + default: } - return [...]string{"BasicInstance", "ExecutorInstance", "TimeoutHandlerInstance"}[s] + return "Unknown" + } diff --git a/pkg/instance/type_test.go b/pkg/instance/type_test.go new file mode 100644 index 00000000..990f3c79 --- /dev/null +++ b/pkg/instance/type_test.go @@ -0,0 +1,49 @@ +package instance + +import ( + "testing" +) + +func TestInstanceType(t *testing.T) { + t.Parallel() + tests := []struct { + name string + in InstanceType + want string + }{ + { + name: "BasicInstance", // Test case name + in: BasicInstance, // Input + want: "BasicInstance", // Expected output + }, + { + name: "ExecutorInstance", + in: ExecutorInstance, + want: "ExecutorInstance", + }, + { + name: "TimeoutHandlerInstance", + in: TimeoutHandlerInstance, + want: "TimeoutHandlerInstance", + }, + { + name: "UnknownInstance", + in: UnknownInstance, + want: "Unknown", + }, + { + name: "4", + in: 4, + want: "Unknown", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.in.String() + if got != tt.want { + t.Errorf("got %q; want %q", got, tt.want) + } + }) + } +}