Skip to content

Commit

Permalink
Merge pull request #24 from mackerelio/retr
Browse files Browse the repository at this point in the history
add ParseStatusMap and deprecate Parse
  • Loading branch information
lufia authored Oct 11, 2023
2 parents 1fd6e2d + f384267 commit cc5a030
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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

0 comments on commit cc5a030

Please sign in to comment.