github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/config/config.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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 config
    16  
    17  import (
    18  	"errors"
    19  	"strconv"
    20  )
    21  
    22  // ErrConfigParamNotFound - Error returned when the config does not contain the parameter requested
    23  var ErrConfigParamNotFound = errors.New("Param not found")
    24  
    25  // ReadableConfig interface provides a mechanisms for getting key value pairs from a config
    26  type ReadableConfig interface {
    27  
    28  	// GetString retrieves a string given a key.  If there is no config property with the given key then
    29  	// config.ErrConfigParamNotFound will be returned. Other errors may be returned depending on the
    30  	// ReadableConfig implementation.
    31  	GetString(key string) (value string, err error)
    32  
    33  	// GetStringOrDefault retrieves a string from the config hierarchy and returns it if available. Otherwise it returns
    34  	// the default string value
    35  	GetStringOrDefault(key, defStr string) string
    36  
    37  	// Iter will perform a callback for each value in a config until all values have been exhausted or until the
    38  	// callback returns true indicating that it should stop.
    39  	Iter(func(string, string) (stop bool))
    40  
    41  	// Size returns the number of properties contained within the config
    42  	Size() int
    43  }
    44  
    45  // WritableConfig interface provides a mechanism for setting key value pairs in a config
    46  type WritableConfig interface {
    47  
    48  	// SetStrings uses the updates map to set configuration parameter values.
    49  	SetStrings(updates map[string]string) error
    50  
    51  	// Unset removes a configuration parameter from the config
    52  	Unset(params []string) error
    53  }
    54  
    55  // ReadWriteConfig interface provides a mechanism for both getting and setting key value pairs in a config
    56  type ReadWriteConfig interface {
    57  	ReadableConfig
    58  	WritableConfig
    59  }
    60  
    61  // GetString retrieves a string value from a ReadableConfig
    62  func GetString(cs ReadableConfig, k string) (string, error) {
    63  	return cs.GetString(k)
    64  }
    65  
    66  // GetInt retrieves a string value from a ReadableConfig and converts it to an integer.
    67  func GetInt(cs ReadableConfig, k string) (int64, error) {
    68  	if s, err := cs.GetString(k); err == nil {
    69  		if val, err := strconv.ParseInt(s, 10, 64); err != nil {
    70  			return 0, err
    71  		} else {
    72  			return val, nil
    73  		}
    74  	} else {
    75  		return 0, err
    76  	}
    77  }
    78  
    79  // GetUint retrieves a string value from a ReadableConfig and converts it to an unsigned integer.
    80  func GetUint(cs ReadableConfig, k string) (uint64, error) {
    81  	if s, err := cs.GetString(k); err == nil {
    82  		if val, err := strconv.ParseUint(s, 10, 64); err != nil {
    83  			return 0, err
    84  		} else {
    85  			return val, nil
    86  		}
    87  	} else {
    88  		return 0, err
    89  	}
    90  }
    91  
    92  // GetFloat retrieves a string value from a ReadableConfig and converts it to a float.
    93  func GetFloat(cs ReadableConfig, k string) (float64, error) {
    94  	if s, err := cs.GetString(k); err == nil {
    95  		if val, err := strconv.ParseFloat(s, 64); err != nil {
    96  			return 0, err
    97  		} else {
    98  			return val, nil
    99  		}
   100  	} else {
   101  		return 0, err
   102  	}
   103  }
   104  
   105  // SetStrings sets configuration values from the values in the updates map
   106  func SetStrings(c WritableConfig, updates map[string]string) error {
   107  	return c.SetStrings(updates)
   108  }
   109  
   110  func SetString(c WritableConfig, key string, val string) error {
   111  	return c.SetStrings(map[string]string{key: val})
   112  }
   113  
   114  // SetInt sets a value in the WritableConfig for a given key to the string converted value of an integer
   115  func SetInt(c WritableConfig, key string, val int64) error {
   116  	s := strconv.FormatInt(val, 10)
   117  	return c.SetStrings(map[string]string{key: s})
   118  }
   119  
   120  // SetUint sets a value in the Writable for a given key to the string converted value of an unsigned int
   121  func SetUint(c WritableConfig, key string, val uint64) error {
   122  	s := strconv.FormatUint(val, 10)
   123  	return c.SetStrings(map[string]string{key: s})
   124  }
   125  
   126  // SetFloat sets a value in the WritableConfig for a given key to the string converted value of a float
   127  func SetFloat(c WritableConfig, key string, val float64) error {
   128  	s := strconv.FormatFloat(val, byte('f'), 8, 64)
   129  	return c.SetStrings(map[string]string{key: s})
   130  }
   131  
   132  // Equals compares a config against a map and returns whether the map contains all the values of the
   133  // config with the same values.
   134  func Equals(cfg ReadableConfig, compareProps map[string]string) bool {
   135  	if cfg.Size() != len(compareProps) {
   136  		return false
   137  	}
   138  
   139  	isEqual := true
   140  	cfg.Iter(func(name, value string) (stop bool) {
   141  		if compareVal, ok := compareProps[name]; ok {
   142  			isEqual = (compareVal == value)
   143  		} else {
   144  			isEqual = false
   145  		}
   146  
   147  		return !isEqual
   148  	})
   149  
   150  	return isEqual
   151  }