github.com/m3db/m3@v1.5.0/src/query/storage/m3/storagemetadata/config.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package storagemetadata
    22  
    23  import (
    24  	"fmt"
    25  )
    26  
    27  var (
    28  	validMetricsTypes = []MetricsType{
    29  		UnaggregatedMetricsType,
    30  		AggregatedMetricsType,
    31  	}
    32  )
    33  
    34  func (t MetricsType) String() string {
    35  	switch t {
    36  	case UnaggregatedMetricsType:
    37  		return "unaggregated"
    38  	case AggregatedMetricsType:
    39  		return "aggregated"
    40  	default:
    41  		return "unknown"
    42  	}
    43  }
    44  
    45  // ParseMetricsType parses a metric type.
    46  func ParseMetricsType(str string) (MetricsType, error) {
    47  	for _, valid := range validMetricsTypes {
    48  		if str == valid.String() {
    49  			return valid, nil
    50  		}
    51  	}
    52  
    53  	return 0, fmt.Errorf("unrecognized metrics type: %v", str)
    54  }
    55  
    56  // ValidateMetricsType validates a stored metrics type.
    57  func ValidateMetricsType(v MetricsType) error {
    58  	for _, valid := range validMetricsTypes {
    59  		if valid == v {
    60  			return nil
    61  		}
    62  	}
    63  
    64  	return fmt.Errorf("invalid stored metrics type '%v': should be one of %v",
    65  		v, validMetricsTypes)
    66  }
    67  
    68  // UnmarshalYAML unmarshals a stored merics type.
    69  func (t *MetricsType) UnmarshalYAML(unmarshal func(interface{}) error) error {
    70  	var str string
    71  	if err := unmarshal(&str); err != nil {
    72  		return err
    73  	}
    74  
    75  	if value, err := ParseMetricsType(str); err == nil {
    76  		*t = value
    77  		return nil
    78  	}
    79  
    80  	return fmt.Errorf("invalid MetricsType '%s' valid types are: %v",
    81  		str, validMetricsTypes)
    82  }