diff --git a/api/event_stream.go b/api/event_stream.go index 10825b5c8..e81d8ef52 100644 --- a/api/event_stream.go +++ b/api/event_stream.go @@ -31,6 +31,10 @@ type Events struct { // Topic is an event Topic type Topic string +// String is a convenience function which returns the topic as a string type +// representation. +func (t Topic) String() string { return string(t) } + // Event holds information related to an event that occurred in Nomad. // The Payload is a hydrated object related to the Topic type Event struct { diff --git a/api/event_stream_test.go b/api/event_stream_test.go index 72c742cac..685dd2945 100644 --- a/api/event_stream_test.go +++ b/api/event_stream_test.go @@ -11,6 +11,51 @@ import ( "github.com/stretchr/testify/require" ) +func TestTopic_String(t *testing.T) { + testutil.Parallel(t) + + testCases := []struct { + inputTopic Topic + expectedOutput string + }{ + { + inputTopic: TopicDeployment, + expectedOutput: "Deployment", + }, + { + inputTopic: TopicEvaluation, + expectedOutput: "Evaluation", + }, + { + inputTopic: TopicAllocation, + expectedOutput: "Allocation", + }, + { + inputTopic: TopicJob, + expectedOutput: "Job", + }, + { + inputTopic: TopicNode, + expectedOutput: "Node", + }, + { + inputTopic: TopicService, + expectedOutput: "Service", + }, + { + inputTopic: TopicAll, + expectedOutput: "*", + }, + } + + for _, tc := range testCases { + t.Run(tc.expectedOutput, func(t *testing.T) { + actualOutput := tc.inputTopic.String() + require.Equal(t, tc.expectedOutput, actualOutput) + }) + } +} + func TestEvent_Stream(t *testing.T) { testutil.Parallel(t)