Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Add UnmarshalYAML for DropPolicy type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Skillington committed Jul 26, 2018
1 parent 33eb82b commit a436125
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
38 changes: 36 additions & 2 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ var (
// DefaultPolicy represents a default policy.
DefaultPolicy Policy

errNilPolicyProto = errors.New("nil policy proto")
errInvalidPolicyString = errors.New("invalid policy string")
errNilPolicyProto = errors.New("nil policy proto")
errInvalidPolicyString = errors.New("invalid policy string")
errInvalidDropPolicyString = errors.New("invalid drop policy string")
)

// Policy contains a storage policy and a list of custom aggregation types.
Expand Down Expand Up @@ -202,3 +203,36 @@ func (p Policies) Equals(other Policies) bool {
}
return true
}

// UnmarshalYAML unmarshals a drop policy value from a string.
func (p *DropPolicy) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}

// Allow default string value (not specified) to mean default
if str == "" {
*p = DefaultDropPolicy
return nil
}

parsed, err := ParseDropPolicy(str)
if err != nil {
return err
}

*p = parsed
return nil
}

// ParseDropPolicy parses a drop policy.
func ParseDropPolicy(str string) (DropPolicy, error) {
for _, valid := range validDropPolicies {
if valid.String() == str {
return valid, nil
}
}

return DefaultDropPolicy, errInvalidDropPolicyString
}
40 changes: 40 additions & 0 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,43 @@ func TestParsePolicyIntoProto(t *testing.T) {
require.Equal(t, input.expected, sp, input.str)
}
}

func TestDropPolicyUnmarshalYAML(t *testing.T) {
inputs := []struct {
str string
expected DropPolicy
}{
{
str: "",
expected: DropNone,
},
{
str: DropNone.String(),
expected: DropNone,
},
{
str: DropMust.String(),
expected: DropMust,
},
{
str: DropIfOnlyMatch.String(),
expected: DropIfOnlyMatch,
},
}
for _, input := range inputs {
var p DropPolicy
require.NoError(t, yaml.Unmarshal([]byte(input.str), &p))
require.Equal(t, input.expected, p)
}
}

func TestDropPolicyUnmarshalYAMLErrors(t *testing.T) {
inputs := []string{
"drop_musty",
"drop_unknown",
}
for _, input := range inputs {
var p DropPolicy
require.Error(t, yaml.Unmarshal([]byte(input), &p))
}
}

0 comments on commit a436125

Please sign in to comment.