istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/diag/level.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package diag
    16  
    17  import (
    18  	"strings"
    19  )
    20  
    21  // Level is the severity level of a message.
    22  type Level struct {
    23  	sortOrder int
    24  	name      string
    25  }
    26  
    27  func (l Level) String() string {
    28  	return l.name
    29  }
    30  
    31  func (l Level) IsWorseThanOrEqualTo(target Level) bool {
    32  	return l.sortOrder <= target.sortOrder
    33  }
    34  
    35  var (
    36  	// Info level is for informational messages
    37  	Info = Level{2, "Info"}
    38  
    39  	// Warning level is for warning messages
    40  	Warning = Level{1, "Warning"}
    41  
    42  	// Error level is for error messages
    43  	Error = Level{0, "Error"}
    44  )
    45  
    46  // GetAllLevels returns an arbitrarily ordered slice of all Levels defined.
    47  func GetAllLevels() []Level {
    48  	return []Level{Info, Warning, Error}
    49  }
    50  
    51  // GetAllLevelStrings returns a list of strings representing the names of all Levels defined. The order is arbitrary but
    52  // should be the same as GetAllLevels.
    53  func GetAllLevelStrings() []string {
    54  	levels := GetAllLevels()
    55  	var s []string
    56  	for _, l := range levels {
    57  		s = append(s, l.name)
    58  	}
    59  	return s
    60  }
    61  
    62  // GetUppercaseStringToLevelMap returns a mapping of uppercase strings to Level structs. This function is intended to be
    63  // used to convert user input to structs.
    64  func GetUppercaseStringToLevelMap() map[string]Level {
    65  	m := make(map[string]Level)
    66  	for _, l := range GetAllLevels() {
    67  		m[strings.ToUpper(l.name)] = l
    68  	}
    69  	return m
    70  }