Skip to content

Add helper function to transform string states into int states #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package check

import (
"strings"
)

const (
// OK means everything is fine
OK = 0
Expand Down Expand Up @@ -29,3 +33,22 @@ func StatusText(status int) string {

return UnknownString
}

// StatusText returns a state corresponding to its
// common string representation
func StatusInt(status string) int {
status = strings.ToUpper(status)

switch status {
case OKString, "0":
return OK
case WarningString, "1":
return Warning
case CriticalString, "2":
return Critical
case UnknownString, "3":
return Unknown
default:
return Unknown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do understand the wish for consistency, but IMHO there is a clear error case here and I feel uncomfortable with the function handling this the way it is now.

I think I would be rather (unpleasantly) surprised to find out, that this function transforms "Critiacl" (a typo of "Critical") to Unknown without giving me a way to verify it.

And yes, the function is rather trivial and I might be heavily prone to overengineering.

}
}
38 changes: 38 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,41 @@ func TestStatusText(t *testing.T) {
})
}
}

func TestStatusInt(t *testing.T) {
testcases := map[string]struct {
input string
expected int
}{
"OK": {
expected: 0,
input: "OK",
},
"WARNING": {
expected: 1,
input: "warning",
},
"CRITICAL": {
expected: 2,
input: "Critical",
},
"UNKNOWN": {
expected: 3,
input: "unknown",
},
"Invalid-Input": {
expected: 3,
input: "Something else",
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
actual := StatusInt(tc.input)

if actual != tc.expected {
t.Error("\nActual: ", actual, "\nExpected: ", tc.expected)
}
})
}
}
Loading