github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/pgnotice/display_severity.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package pgnotice
    12  
    13  import (
    14  	"fmt"
    15  	"strings"
    16  )
    17  
    18  // DisplaySeverity indicates the severity of a given error for the
    19  // purposes of displaying notices.
    20  // This corresponds to the allowed values for the `client_min_messages`
    21  // variable in postgres.
    22  type DisplaySeverity int
    23  
    24  // It is important to keep the same order here as Postgres.
    25  // See https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-CLIENT-MIN-MESSAGES.
    26  
    27  const (
    28  	// DisplaySeverityError is a DisplaySeverity value allowing all notices
    29  	// of value <= DisplaySeverityError to display.
    30  	DisplaySeverityError = iota
    31  	// DisplaySeverityWarning is a DisplaySeverity value allowing all notices
    32  	// of value <= DisplaySeverityWarning to display.
    33  	DisplaySeverityWarning
    34  	// DisplaySeverityNotice is a DisplaySeverity value allowing all notices
    35  	// of value <= DisplaySeverityNotice to display.
    36  	DisplaySeverityNotice
    37  	// DisplaySeverityLog is a DisplaySeverity value allowing all notices
    38  	// of value <= DisplaySeverityLog.g to display.
    39  	DisplaySeverityLog
    40  	// DisplaySeverityDebug1 is a DisplaySeverity value allowing all notices
    41  	// of value <= DisplaySeverityDebug1 to display.
    42  	DisplaySeverityDebug1
    43  	// DisplaySeverityDebug2 is a DisplaySeverity value allowing all notices
    44  	// of value <= DisplaySeverityDebug2 to display.
    45  	DisplaySeverityDebug2
    46  	// DisplaySeverityDebug3 is a DisplaySeverity value allowing all notices
    47  	// of value <= DisplaySeverityDebug3 to display.
    48  	DisplaySeverityDebug3
    49  	// DisplaySeverityDebug4 is a DisplaySeverity value allowing all notices
    50  	// of value <= DisplaySeverityDebug4 to display.
    51  	DisplaySeverityDebug4
    52  	// DisplaySeverityDebug5 is a DisplaySeverity value allowing all notices
    53  	// of value <= DisplaySeverityDebug5 to display.
    54  	DisplaySeverityDebug5
    55  )
    56  
    57  // ParseDisplaySeverity translates a string to a DisplaySeverity.
    58  // Returns the severity, and a bool indicating whether the severity exists.
    59  func ParseDisplaySeverity(k string) (DisplaySeverity, bool) {
    60  	s, ok := namesToDisplaySeverity[strings.ToLower(k)]
    61  	return s, ok
    62  }
    63  
    64  func (ns DisplaySeverity) String() string {
    65  	if ns < 0 || ns > DisplaySeverity(len(noticeDisplaySeverityNames)-1) {
    66  		return fmt.Sprintf("DisplaySeverity(%d)", ns)
    67  	}
    68  	return noticeDisplaySeverityNames[ns]
    69  }
    70  
    71  // noticeDisplaySeverityNames maps a DisplaySeverity into it's string representation.
    72  var noticeDisplaySeverityNames = [...]string{
    73  	DisplaySeverityDebug5:  "debug5",
    74  	DisplaySeverityDebug4:  "debug4",
    75  	DisplaySeverityDebug3:  "debug3",
    76  	DisplaySeverityDebug2:  "debug2",
    77  	DisplaySeverityDebug1:  "debug1",
    78  	DisplaySeverityLog:     "log",
    79  	DisplaySeverityNotice:  "notice",
    80  	DisplaySeverityWarning: "warning",
    81  	DisplaySeverityError:   "error",
    82  }
    83  
    84  // namesToDisplaySeverity is the reverse mapping from string to DisplaySeverity.
    85  var namesToDisplaySeverity = map[string]DisplaySeverity{}
    86  
    87  // ValidDisplaySeverities returns a list of all valid severities.
    88  func ValidDisplaySeverities() []string {
    89  	ret := make([]string, 0, len(namesToDisplaySeverity))
    90  	for _, s := range noticeDisplaySeverityNames {
    91  		ret = append(ret, s)
    92  	}
    93  	return ret
    94  }
    95  
    96  func init() {
    97  	for k, v := range noticeDisplaySeverityNames {
    98  		namesToDisplaySeverity[v] = DisplaySeverity(k)
    99  	}
   100  }