-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from mackerelio/support-as-status
overwrite status.
- Loading branch information
Showing
5 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package checkers | ||
|
||
import "strings" | ||
|
||
type invalidStatusMapError string | ||
|
||
func (err invalidStatusMapError) Error() string { | ||
return "invalid status map: " + string(err) | ||
} | ||
|
||
type duplicateStatusMapError Status | ||
|
||
func (err duplicateStatusMapError) Error() string { | ||
return "duplicate status in map: " + Status(err).String() | ||
} | ||
|
||
func parseStatusPrefix(src string) (Status, string, bool) { | ||
switch { | ||
case strings.HasPrefix(src, "OK"): | ||
return OK, src[len("OK"):], true | ||
case strings.HasPrefix(src, "WARNING"): | ||
return WARNING, src[len("WARNING"):], true | ||
case strings.HasPrefix(src, "CRITICAL"): | ||
return CRITICAL, src[len("CRITICAL"):], true | ||
case strings.HasPrefix(src, "UNKNOWN"): | ||
return UNKNOWN, src[len("UNKNOWN"):], true | ||
default: | ||
return OK, src, false | ||
} | ||
} | ||
|
||
// Parse 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 Parse(src string) (map[Status]Status, error) { | ||
orig, stm := src, map[Status]Status{} | ||
for src = strings.ToUpper(src); src != ""; { | ||
var from, to Status | ||
var ok bool | ||
if from, src, ok = parseStatusPrefix(src); !ok { | ||
return nil, invalidStatusMapError(orig) | ||
} | ||
if src, ok = strings.CutPrefix(src, "="); !ok { | ||
return nil, invalidStatusMapError(orig) | ||
} | ||
if to, src, ok = parseStatusPrefix(src); !ok { | ||
return nil, invalidStatusMapError(orig) | ||
} | ||
if src, ok = strings.CutPrefix(src, ","); ok == (src == "") { | ||
return nil, invalidStatusMapError(orig) | ||
} | ||
if _, ok = stm[from]; ok { | ||
return nil, duplicateStatusMapError(from) | ||
} | ||
stm[from] = to | ||
} | ||
return stm, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package checkers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
source string | ||
expected map[Status]Status | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "normal", | ||
source: "unknown=critical", | ||
expected: map[Status]Status{ | ||
UNKNOWN: CRITICAL, | ||
}, | ||
}, | ||
{ | ||
name: "uppercase", | ||
source: "UNKNOWN=CRITICAL", | ||
expected: map[Status]Status{ | ||
UNKNOWN: CRITICAL, | ||
}, | ||
}, | ||
{ | ||
name: "multi", | ||
source: "unknown=ok,warning=critical", | ||
expected: map[Status]Status{ | ||
UNKNOWN: OK, | ||
WARNING: CRITICAL, | ||
}, | ||
}, | ||
{ | ||
name: "from error", | ||
source: "invalid=critical", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "= error", | ||
source: "unknown critical", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "to error", | ||
source: "critical=invalid", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "argument error", | ||
source: "unknown=critical=invalid", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing comma error", | ||
source: "unknown=criticalwarning=critical", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "trailing comma error", | ||
source: "unknown=ok,", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "duplicate error", | ||
source: "critical=unknown,critical=warning", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, err := Parse(tt.source) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(tt.expected, actual) { | ||
t.Errorf("Parse() should be %+v but got: %+v", tt.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/mackerelio/checkers | ||
|
||
go 1.18 | ||
go 1.20 |