-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using semantic names for priority levels. (#115)
This maps certain strings like "rollback" or "canary" to a standard integer priority so that priorities in repo configs may be specified with these strings.
- Loading branch information
1 parent
32a9293
commit 3a61116
Showing
8 changed files
with
127 additions
and
50 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
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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Package priority provides standard priority levels. | ||
package priority | ||
|
||
import "strconv" | ||
|
||
// Value is an integer priority associated with a repo. | ||
type Value int | ||
|
||
const ( | ||
// None is an unspecified priority level. | ||
None Value = 0 | ||
// Default is the default priority level for everything. | ||
Default Value = 500 | ||
// Canary is the priority level for canary repos. | ||
Canary Value = 1300 | ||
// Pin is the priority level for user-pinned repos. | ||
Pin Value = 1400 | ||
// Rollback is the priority level for rollback repos. | ||
Rollback Value = 1500 | ||
) | ||
|
||
// priorityNameToValue maps semantic priority names to their integer values. | ||
var priorityNameToValue = map[string]Value{ | ||
"default": Default, | ||
"canary": Canary, | ||
"pin": Pin, | ||
"rollback": Rollback, | ||
} | ||
|
||
// FromString converts the string s into a priority Value, where s represents | ||
// either a semantic priority name or an integer. | ||
func FromString(s string) (Value, error) { | ||
if v, ok := priorityNameToValue[s]; ok { | ||
return v, nil | ||
} | ||
i, err := strconv.Atoi(s) | ||
return Value(i), err | ||
} |
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,31 @@ | ||
package priority | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFromString(t *testing.T) { | ||
for _, tc := range []struct { | ||
s string | ||
want Value | ||
wantErr bool | ||
}{ | ||
{s: "default", want: Default}, | ||
{s: "canary", want: Canary}, | ||
{s: "pin", want: Pin}, | ||
{s: "rollback", want: Rollback}, | ||
{s: "100", want: Value(100)}, | ||
{s: "bad", wantErr: true}, | ||
} { | ||
t.Run(tc.s, func(t *testing.T) { | ||
got, err := FromString(tc.s) | ||
if err != nil && !tc.wantErr { | ||
t.Errorf("FromString(%s) failed: %v", tc.s, err) | ||
} else if err == nil && tc.wantErr { | ||
t.Errorf("FromString(%s) got nil error, wanted non-nil", tc.s) | ||
} else if got != tc.want { | ||
t.Errorf("FromString(%s) got: %v, want: %v", tc.s, got, tc.want) | ||
} | ||
}) | ||
} | ||
} |