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

Commit

Permalink
YAML unmarshal aggregation types (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
cw9 committed Jun 10, 2017
1 parent 296d5d2 commit a8d3dce
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
15 changes: 15 additions & 0 deletions policy/aggregation_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ func NewAggregationTypesFromSchema(input []schema.AggregationType) (AggregationT
return res, nil
}

// UnmarshalYAML unmarshals aggregation types from a string.
func (aggTypes *AggregationTypes) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}

parsed, err := ParseAggregationTypes(str)
if err != nil {
return err
}
*aggTypes = parsed
return nil
}

// IsDefault checks if the AggregationTypes is the default aggregation type.
func (aggTypes AggregationTypes) IsDefault() bool {
return len(aggTypes) == 0
Expand Down
53 changes: 52 additions & 1 deletion policy/aggregation_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"testing"

"github.com/m3db/m3x/pool"

"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

func TestAggregationTypeIsValid(t *testing.T) {
Expand All @@ -38,7 +40,56 @@ func TestAggregationTypesIsDefault(t *testing.T) {
require.False(t, AggregationTypes{Upper}.IsDefault())
}

func TestParseParseAggregationTypes(t *testing.T) {
func TestAggregationTypesUnmarshalYAML(t *testing.T) {
inputs := []struct {
str string
expected AggregationTypes
expectedErr bool
}{
{
str: "Lower",
expected: AggregationTypes{Lower},
},
{
str: "Mean,Upper,P99,P9999",
expected: AggregationTypes{Mean, Upper, P99, P9999},
},
{
str: "Lower,Upper,P99,P9999,P100",
expectedErr: true,
},
{
str: "Lower,Upper,P99,P9999,P100",
expectedErr: true,
},
{
str: ",Mean",
expectedErr: true,
},
{
str: "Mean,",
expectedErr: true,
},
{
str: ",Mean,",
expectedErr: true,
},
}
for _, input := range inputs {
var aggtypes AggregationTypes
err := yaml.Unmarshal([]byte(input.str), &aggtypes)

if input.expectedErr {
require.Error(t, err)
continue
}

require.NoError(t, err)
require.Equal(t, input.expected, aggtypes)
}
}

func TestParseAggregationTypes(t *testing.T) {
inputs := []struct {
str string
expected AggregationTypes
Expand Down

0 comments on commit a8d3dce

Please sign in to comment.