Commit 297b85cb authored by Steven Allen's avatar Steven Allen

feat: add WithDefault for flag/priority

This makes it easier to resolve these fields.
parent a3038592
...@@ -55,6 +55,19 @@ const ( ...@@ -55,6 +55,19 @@ const (
True Flag = 1 True Flag = 1
) )
func (f Flag) WithDefault(defaultValue bool) bool {
switch f {
case False:
return false
case Default:
return defaultValue
case True:
return true
default:
panic(fmt.Sprintf("invalid flag value %d", f))
}
}
func (f Flag) MarshalJSON() ([]byte, error) { func (f Flag) MarshalJSON() ([]byte, error) {
switch f { switch f {
case Default: case Default:
...@@ -110,6 +123,23 @@ const ( ...@@ -110,6 +123,23 @@ const (
Disabled Priority = -1 Disabled Priority = -1
) )
// WithDefault resolves the priority with the given default.
//
// If `defaultPriority` is Default/0, this function will return 0.
func (p Priority) WithDefault(defaultPriority Priority) (priority int64, enabled bool) {
switch p {
case Disabled:
return 0, false
case DefaultPriority:
if defaultPriority < 0 {
return 0, false
}
return int64(defaultPriority), true
default:
return int64(p), true
}
}
func (p Priority) MarshalJSON() ([]byte, error) { func (p Priority) MarshalJSON() ([]byte, error) {
// > 0 == Priority // > 0 == Priority
if p > 0 { if p > 0 {
......
...@@ -91,6 +91,30 @@ func TestFlag(t *testing.T) { ...@@ -91,6 +91,30 @@ func TestFlag(t *testing.T) {
t.Errorf("expected default flag to be %q, got %q", Default, defaultFlag) t.Errorf("expected default flag to be %q, got %q", Default, defaultFlag)
} }
if defaultFlag.WithDefault(true) != true {
t.Error("expected default & true to be true")
}
if defaultFlag.WithDefault(false) != false {
t.Error("expected default & false to be false")
}
if True.WithDefault(false) != true {
t.Error("default should only apply to default")
}
if False.WithDefault(true) != false {
t.Error("default should only apply to default")
}
if True.WithDefault(true) != true {
t.Error("true & true is true")
}
if False.WithDefault(true) != false {
t.Error("false & false is false")
}
for jsonStr, goValue := range map[string]Flag{ for jsonStr, goValue := range map[string]Flag{
"null": Default, "null": Default,
"true": True, "true": True,
...@@ -135,6 +159,18 @@ func TestPriority(t *testing.T) { ...@@ -135,6 +159,18 @@ func TestPriority(t *testing.T) {
t.Errorf("expected default priority to be %q, got %q", DefaultPriority, defaultPriority) t.Errorf("expected default priority to be %q, got %q", DefaultPriority, defaultPriority)
} }
if _, ok := defaultPriority.WithDefault(Disabled); ok {
t.Error("should have been disabled")
}
if p, ok := defaultPriority.WithDefault(1); !ok || p != 1 {
t.Errorf("priority should have been 1, got %d", p)
}
if p, ok := defaultPriority.WithDefault(DefaultPriority); !ok || p != 0 {
t.Errorf("priority should have been 0, got %d", p)
}
for jsonStr, goValue := range map[string]Priority{ for jsonStr, goValue := range map[string]Priority{
"null": DefaultPriority, "null": DefaultPriority,
"false": Disabled, "false": Disabled,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment