github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/string.go (about)

     1  // Copyright 2017 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 settings
    12  
    13  import "github.com/cockroachdb/errors"
    14  
    15  // StringSetting is the interface of a setting variable that will be
    16  // updated automatically when the corresponding cluster-wide setting
    17  // of type "string" is updated.
    18  type StringSetting struct {
    19  	defaultValue string
    20  	validateFn   func(*Values, string) error
    21  	common
    22  }
    23  
    24  var _ extendedSetting = &StringSetting{}
    25  
    26  func (s *StringSetting) String(sv *Values) string {
    27  	return s.Get(sv)
    28  }
    29  
    30  // Encoded returns the encoded value of the current value of the setting.
    31  func (s *StringSetting) Encoded(sv *Values) string {
    32  	return s.String(sv)
    33  }
    34  
    35  // EncodedDefault returns the encoded value of the default value of the setting.
    36  func (s *StringSetting) EncodedDefault() string {
    37  	return s.defaultValue
    38  }
    39  
    40  // Typ returns the short (1 char) string denoting the type of setting.
    41  func (*StringSetting) Typ() string {
    42  	return "s"
    43  }
    44  
    45  // Get retrieves the string value in the setting.
    46  func (s *StringSetting) Get(sv *Values) string {
    47  	loaded := sv.getGeneric(s.slotIdx)
    48  	if loaded == nil {
    49  		return ""
    50  	}
    51  	return loaded.(string)
    52  }
    53  
    54  // Validate that a value conforms with the validation function.
    55  func (s *StringSetting) Validate(sv *Values, v string) error {
    56  	if s.validateFn != nil {
    57  		if err := s.validateFn(sv, v); err != nil {
    58  			return err
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func (s *StringSetting) set(sv *Values, v string) error {
    65  	if err := s.Validate(sv, v); err != nil {
    66  		return err
    67  	}
    68  	if s.Get(sv) != v {
    69  		sv.setGeneric(s.slotIdx, v)
    70  	}
    71  	return nil
    72  }
    73  
    74  func (s *StringSetting) setToDefault(sv *Values) {
    75  	if err := s.set(sv, s.defaultValue); err != nil {
    76  		panic(err)
    77  	}
    78  }
    79  
    80  // RegisterStringSetting defines a new setting with type string.
    81  func RegisterStringSetting(key, desc string, defaultValue string) *StringSetting {
    82  	return RegisterValidatedStringSetting(key, desc, defaultValue, nil)
    83  }
    84  
    85  // RegisterPublicStringSetting defines a new setting with type string and makes it public.
    86  func RegisterPublicStringSetting(key, desc string, defaultValue string) *StringSetting {
    87  	s := RegisterValidatedStringSetting(key, desc, defaultValue, nil)
    88  	s.SetVisibility(Public)
    89  	return s
    90  }
    91  
    92  // RegisterValidatedStringSetting defines a new setting with type string with a
    93  // validation function.
    94  func RegisterValidatedStringSetting(
    95  	key, desc string, defaultValue string, validateFn func(*Values, string) error,
    96  ) *StringSetting {
    97  	if validateFn != nil {
    98  		if err := validateFn(nil, defaultValue); err != nil {
    99  			panic(errors.Wrap(err, "invalid default"))
   100  		}
   101  	}
   102  	setting := &StringSetting{
   103  		defaultValue: defaultValue,
   104  		validateFn:   validateFn,
   105  	}
   106  	// By default all string settings are considered to perhaps contain
   107  	// PII and are thus non-reportable (to exclude them from telemetry
   108  	// reports).
   109  	setting.SetReportable(false)
   110  	register(key, desc, setting)
   111  	return setting
   112  }