github.com/m3db/m3@v1.5.0/src/metrics/rules/validator/namespace/static/validator.go (about)

     1  // Copyright (c) 2017 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 static
    22  
    23  import (
    24  	"fmt"
    25  	"strings"
    26  
    27  	"github.com/m3db/m3/src/metrics/rules/validator/namespace"
    28  )
    29  
    30  // ValidationResult is the validation result.
    31  type ValidationResult string
    32  
    33  // A list of supported validation result.
    34  const (
    35  	Invalid ValidationResult = "invalid"
    36  	Valid   ValidationResult = "valid"
    37  )
    38  
    39  var (
    40  	validValidationResults = []ValidationResult{
    41  		Invalid,
    42  		Valid,
    43  	}
    44  )
    45  
    46  // UnmarshalYAML unmarshals YAML object into a validation result.
    47  func (t *ValidationResult) UnmarshalYAML(unmarshal func(interface{}) error) error {
    48  	var str string
    49  	if err := unmarshal(&str); err != nil {
    50  		return err
    51  	}
    52  	validResults := make([]string, 0, len(validValidationResults))
    53  	for _, valid := range validValidationResults {
    54  		if str == string(valid) {
    55  			*t = valid
    56  			return nil
    57  		}
    58  		validResults = append(validResults, string(valid))
    59  	}
    60  	return fmt.Errorf("invalid validation result '%s' valid results are: %s",
    61  		str, strings.Join(validResults, ", "))
    62  }
    63  
    64  type validator struct {
    65  	validationResult ValidationResult
    66  }
    67  
    68  // NewNamespaceValidator creates a new static namespace validator.
    69  func NewNamespaceValidator(res ValidationResult) namespace.Validator {
    70  	return &validator{validationResult: res}
    71  }
    72  
    73  func (v *validator) Validate(ns string) error {
    74  	switch v.validationResult {
    75  	case Invalid:
    76  		return fmt.Errorf("static validator returns invalid for %s", ns)
    77  	default:
    78  		return nil
    79  	}
    80  }
    81  
    82  func (v *validator) Close() {}