Skip to content
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

add ParseStatusMap and deprecate Parse #24

Merged
merged 3 commits into from
Oct 11, 2023
Merged
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
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/mackerelio/checkers

go 1.20

retract (
v0.1.0 // Contains a misleading function name.
)
14 changes: 14 additions & 0 deletions asstatusparse.go → statusmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,21 @@ func parseStatusPrefix(src string) (Status, string, bool) {
// - unknown
//
// You can specify multiple key-value pairs separated by commas.
//
// Deprecated: use ParseStatusMap instead.
func Parse(src string) (map[Status]Status, error) {
return ParseStatusMap(src)
}

// ParseStatusMap parses a string of the form <status>=<status>. <status> is one of:
//
// - ok
// - warning
// - critical
// - unknown
//
// You can specify multiple key-value pairs separated by commas.
func ParseStatusMap(src string) (map[Status]Status, error) {
orig, stm := src, map[Status]Status{}
for src = strings.ToUpper(src); src != ""; {
var from, to Status
Expand Down
15 changes: 12 additions & 3 deletions asstatusparse_test.go → statusmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import (
"testing"
)

func TestParseStatusMap(t *testing.T) {
testParseFunc(t, "ParseStatusMap", ParseStatusMap)
}

func TestParse(t *testing.T) {
testParseFunc(t, "Parse", Parse)
}

func testParseFunc(t *testing.T, name string, fn func(src string) (map[Status]Status, error)) {
t.Helper()
tests := []struct {
name string
source string
Expand Down Expand Up @@ -72,13 +81,13 @@ func TestParse(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := Parse(tt.source)
actual, err := fn(tt.source)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("%s(%q) error = %v, wantErr %v", name, tt.source, err, tt.wantErr)
return
}
if !reflect.DeepEqual(tt.expected, actual) {
t.Errorf("Parse() should be %+v but got: %+v", tt.expected, actual)
t.Errorf("%s(%q) should be %+v but got: %+v", name, tt.source, tt.expected, actual)
}
})
}
Expand Down
Loading