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

     1  // Copyright (c) 2019 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 models
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  )
    27  
    28  var validIDSchemes = []IDSchemeType{
    29  	TypeQuoted,
    30  	TypePrependMeta,
    31  	TypeGraphite,
    32  }
    33  
    34  // Validate validates that the scheme type is valid.
    35  func (t IDSchemeType) Validate() error {
    36  	if t == TypeDefault {
    37  		return errors.New("id scheme type not set")
    38  	}
    39  
    40  	if t >= TypeQuoted && t <= TypeGraphite {
    41  		return nil
    42  	}
    43  
    44  	return fmt.Errorf("invalid config id schema type '%v': should be one of %v",
    45  		t, validIDSchemes)
    46  }
    47  
    48  func (t IDSchemeType) String() string {
    49  	switch t {
    50  	case TypeDefault:
    51  		return ""
    52  	case TypeQuoted:
    53  		return "quoted"
    54  	case TypePrependMeta:
    55  		return "prepend_meta"
    56  	case TypeGraphite:
    57  		return "graphite"
    58  	default:
    59  		// Should never get here.
    60  		return "unknown"
    61  	}
    62  }
    63  
    64  // MarshalYAML returns the YAML representation of the IDSchemeType.
    65  func (t IDSchemeType) MarshalYAML() (interface{}, error) {
    66  	return t.String(), nil
    67  }
    68  
    69  // UnmarshalYAML unmarshals a stored merics type.
    70  func (t *IDSchemeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
    71  	var str string
    72  	if err := unmarshal(&str); err != nil {
    73  		return err
    74  	}
    75  
    76  	if str == "" {
    77  		*t = TypeDefault
    78  		return nil
    79  	}
    80  
    81  	for _, valid := range validIDSchemes {
    82  		if valid == TypeGraphite {
    83  			// NB: while the graphite scheme is valid, it is not available to choose
    84  			// as a general ID scheme; instead, it is set on any metric coming through
    85  			// the graphite ingestion path.
    86  			continue
    87  		}
    88  
    89  		if str == valid.String() {
    90  			*t = valid
    91  			return nil
    92  		}
    93  	}
    94  
    95  	return fmt.Errorf("invalid MetricsType '%s' valid types are: %v",
    96  		str, validIDSchemes)
    97  }