Skip to content

Commit

Permalink
chore: update instance type for better type safety (#412)
Browse files Browse the repository at this point in the history
use 0 value as unknown to force intentional use of type
  • Loading branch information
MSevey authored Jun 4, 2024
1 parent 5bc5c3d commit 7346907
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/instance/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}
49 changes: 49 additions & 0 deletions pkg/instance/type_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 7346907

Please sign in to comment.