github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/config.go (about)

     1  // The reason that so many of these functions seem like duplication is simply for the better error
     2  // messages so each driver doesn't need to deal with validation on these simple field types
     3  
     4  package drivers
     5  
     6  import (
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/friendsofgo/errors"
    12  )
    13  
    14  // Config is a map with helper functions
    15  type Config map[string]interface{}
    16  
    17  // MustString retrieves a string that must exist and must be a string, it must also not be the empty string
    18  func (c Config) MustString(key string) string {
    19  	s, ok := c[key]
    20  	if !ok {
    21  		panic(errors.Errorf("failed to find key %s in config", key))
    22  	}
    23  
    24  	str, ok := s.(string)
    25  	if !ok {
    26  		panic(errors.Errorf("found key %s in config, but it was not a string (%T)", key, s))
    27  	}
    28  
    29  	if len(str) == 0 {
    30  		panic(errors.Errorf("found key %s in config, but it was an empty string", key))
    31  	}
    32  
    33  	return str
    34  }
    35  
    36  // MustInt retrieves a string that must exist and must be an int, and it must not be 0
    37  func (c Config) MustInt(key string) int {
    38  	i, ok := c[key]
    39  	if !ok {
    40  		panic(errors.Errorf("failed to find key %s in config", key))
    41  	}
    42  
    43  	var integer int
    44  	switch t := i.(type) {
    45  	case int:
    46  		integer = t
    47  	case float64:
    48  		integer = int(t)
    49  	case string:
    50  		var err error
    51  		integer, err = strconv.Atoi(t)
    52  		if err != nil {
    53  			panic(errors.Errorf("failed to parse key %s (%s) to int: %v", key, t, err))
    54  		}
    55  	default:
    56  		panic(errors.Errorf("found key %s in config, but it was not an int (%T)", key, i))
    57  	}
    58  
    59  	if integer == 0 {
    60  		panic(errors.Errorf("found key %s in config, but its value was 0", key))
    61  	}
    62  
    63  	return integer
    64  }
    65  
    66  // String retrieves a non-empty string, the bool says if it exists, is of appropriate type,
    67  // and has a non-zero length or not.
    68  func (c Config) String(key string) (string, bool) {
    69  	s, ok := c[key]
    70  	if !ok {
    71  		return "", false
    72  	}
    73  
    74  	str, ok := s.(string)
    75  	if !ok {
    76  		return "", false
    77  	}
    78  
    79  	if len(str) == 0 {
    80  		return "", false
    81  	}
    82  
    83  	return str, true
    84  }
    85  
    86  // DefaultString retrieves a non-empty string or the default value provided.
    87  func (c Config) DefaultString(key, def string) string {
    88  	str, ok := c.String(key)
    89  	if !ok {
    90  		return def
    91  	}
    92  
    93  	return str
    94  }
    95  
    96  // Int retrieves an int, the bool says if it exists, is of the appropriate type,
    97  // and is non-zero. Coerces float64 to int because JSON and Javascript kinda suck.
    98  func (c Config) Int(key string) (int, bool) {
    99  	i, ok := c[key]
   100  	if !ok {
   101  		return 0, false
   102  	}
   103  
   104  	var integer int
   105  	switch t := i.(type) {
   106  	case int:
   107  		integer = t
   108  	case float64:
   109  		integer = int(t)
   110  	case string:
   111  		var err error
   112  		integer, err = strconv.Atoi(t)
   113  		if err != nil {
   114  			return 0, false
   115  		}
   116  	default:
   117  		return 0, false
   118  	}
   119  
   120  	if integer == 0 {
   121  		return 0, false
   122  	}
   123  
   124  	return integer, true
   125  }
   126  
   127  // DefaultInt retrieves a non-zero int or the default value provided.
   128  func (c Config) DefaultInt(key string, def int) int {
   129  	i, ok := c.Int(key)
   130  	if !ok {
   131  		return def
   132  	}
   133  
   134  	return i
   135  }
   136  
   137  // StringSlice retrieves an string slice, the bool says if it exists, is of the appropriate type,
   138  // is non-nil and non-zero length
   139  func (c Config) StringSlice(key string) ([]string, bool) {
   140  	ss, ok := c[key]
   141  	if !ok {
   142  		return nil, false
   143  	}
   144  
   145  	var slice []string
   146  	if intfSlice, ok := ss.([]interface{}); ok {
   147  		for _, i := range intfSlice {
   148  			slice = append(slice, i.(string))
   149  		}
   150  	} else if stringSlice, ok := ss.([]string); ok {
   151  		slice = stringSlice
   152  	} else {
   153  		return nil, false
   154  	}
   155  
   156  	// Also detects nil
   157  	if len(slice) == 0 {
   158  		return nil, false
   159  	}
   160  	return slice, true
   161  }
   162  
   163  // DefaultEnv grabs a value from the environment or a default.
   164  // This is shared by drivers to get config for testing.
   165  func DefaultEnv(key, def string) string {
   166  	val := os.Getenv(key)
   167  	if len(val) == 0 {
   168  		val = def
   169  	}
   170  	return val
   171  }
   172  
   173  // TablesFromList takes a whitelist or blacklist and returns
   174  // the table names.
   175  func TablesFromList(list []string) []string {
   176  	if len(list) == 0 {
   177  		return nil
   178  	}
   179  
   180  	var tables []string
   181  	for _, i := range list {
   182  		splits := strings.Split(i, ".")
   183  
   184  		if len(splits) == 1 {
   185  			tables = append(tables, splits[0])
   186  		}
   187  	}
   188  
   189  	return tables
   190  }
   191  
   192  // ColumnsFromList takes a whitelist or blacklist and returns
   193  // the columns for a given table.
   194  func ColumnsFromList(list []string, tablename string) []string {
   195  	if len(list) == 0 {
   196  		return nil
   197  	}
   198  
   199  	var columns []string
   200  	for _, i := range list {
   201  		splits := strings.Split(i, ".")
   202  
   203  		if len(splits) != 2 {
   204  			continue
   205  		}
   206  
   207  		if splits[0] == tablename || splits[0] == "*" {
   208  			columns = append(columns, splits[1])
   209  		}
   210  	}
   211  
   212  	return columns
   213  }