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

Commit

Permalink
Use transformFnType in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Wang committed Oct 17, 2017
1 parent bee3b56 commit d32c7ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
58 changes: 27 additions & 31 deletions policy/aggregation_type_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type AggregationTypesConfiguration struct {
GaugeOverrides map[AggregationType]string `yaml:"gaugeOverrides"`

// TransformFnType configs the global type string transform function type.
TransformFnType string `yaml:"transformFnType"`
TransformFnType *transformFnType `yaml:"transformFnType"`

// Pool of aggregation types.
AggregationTypesPool pool.ObjectPoolConfiguration `yaml:"aggregationTypesPool"`
Expand All @@ -62,11 +62,14 @@ type AggregationTypesConfiguration struct {

// NewOptions creates a new Option.
func (c AggregationTypesConfiguration) NewOptions(instrumentOpts instrument.Options) (AggregationTypesOptions, error) {
fn, err := parseTransformFn(c.TransformFnType)
if err != nil {
return nil, err
opts := NewAggregationTypesOptions()
if c.TransformFnType != nil {
fn, err := c.TransformFnType.TransformFn()
if err != nil {
return nil, err
}
opts = opts.SetGlobalTypeStringTransformFn(fn)
}
opts := NewAggregationTypesOptions().SetGlobalTypeStringTransformFn(fn)

if c.DefaultCounterAggregationTypes != nil {
opts = opts.SetDefaultCounterAggregationTypes(*c.DefaultCounterAggregationTypes)
Expand Down Expand Up @@ -122,45 +125,38 @@ func parseTypeStringOverride(m map[AggregationType]string) map[AggregationType][
return res
}

func parseTransformFn(str string) (TypeStringTransformFn, error) {
t, err := parseTransformFnType(str)
if err != nil {
return nil, err
type transformFnType string

var (
unknownTransformType transformFnType = "unknown"
noopTransformType transformFnType = "noop"
suffixTransformType transformFnType = "suffix"

validTypes = []transformFnType{
noopTransformType,
suffixTransformType,
}
return t.TransformFn()
}
)

func parseTransformFnType(str string) (transformType, error) {
if str == "" {
return noopTransformType, nil
func (t *transformFnType) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
var validStrings []string
for _, validType := range validTypes {
validString := string(validType)
if validString == str {
return validType, nil
*t = validType
return nil
}
validStrings = append(validStrings, validString)
}

return unknownTransformType, fmt.Errorf("invalid transform type %s, valid types are: %v", str, validStrings)

return fmt.Errorf("invalid transform type %s, valid types are: %v", str, validStrings)
}

type transformType string

var (
unknownTransformType transformType = "unknown"
noopTransformType transformType = "noop"
suffixTransformType transformType = "suffix"

validTypes = []transformType{
noopTransformType,
suffixTransformType,
}
)

func (t transformType) TransformFn() (TypeStringTransformFn, error) {
func (t transformFnType) TransformFn() (TypeStringTransformFn, error) {
switch t {
case noopTransformType:
return noopTransformFn, nil
Expand Down
4 changes: 1 addition & 3 deletions policy/aggregation_type_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,5 @@ transformFnType: bla
`

var cfg AggregationTypesConfiguration
require.NoError(t, yaml.Unmarshal([]byte(str), &cfg))
_, err := cfg.NewOptions(instrument.NewOptions())
require.Error(t, err)
require.Error(t, yaml.Unmarshal([]byte(str), &cfg))
}

0 comments on commit d32c7ea

Please sign in to comment.