diff --git a/metric/types.go b/metric/types.go index bd280cb..9713bd1 100644 --- a/metric/types.go +++ b/metric/types.go @@ -64,11 +64,11 @@ func (t *Type) UnmarshalYAML(unmarshal func(interface{}) error) error { } validTypeStrs := make([]string, 0, len(validTypes)) for _, valid := range validTypes { - if str == string(valid) { + if str == valid.String() { *t = valid return nil } - validTypeStrs = append(validTypeStrs, string(valid)) + validTypeStrs = append(validTypeStrs, valid.String()) } return fmt.Errorf("invalid metric type '%s' valid types are: %s", str, strings.Join(validTypeStrs, ", ")) diff --git a/metric/types_test.go b/metric/types_test.go new file mode 100644 index 0000000..db2b0bd --- /dev/null +++ b/metric/types_test.go @@ -0,0 +1,57 @@ +// Copyright (c) 2017 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package metric + +import ( + "testing" + + "github.com/stretchr/testify/require" + yaml "gopkg.in/yaml.v2" +) + +func TestTypeUnmarshalYAML(t *testing.T) { + inputs := []struct { + str string + expected Type + }{ + {str: "counter", expected: CounterType}, + {str: "timer", expected: TimerType}, + {str: "gauge", expected: GaugeType}, + } + for _, input := range inputs { + var typ Type + require.NoError(t, yaml.Unmarshal([]byte(input.str), &typ)) + require.Equal(t, input.expected, typ) + } +} + +func TestTypeUnmarshalYAMLErrors(t *testing.T) { + inputs := []string{ + "huh", + "blah", + } + for _, input := range inputs { + var typ Type + err := yaml.Unmarshal([]byte(input), &typ) + require.Error(t, err) + require.Equal(t, "invalid metric type '"+input+"' valid types are: counter, timer, gauge", err.Error()) + } +}