gitlab.com/evatix-go/core@v1.3.55/typesconv/string.go (about)

     1  package typesconv
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  )
     8  
     9  func StringPtr(val string) *string {
    10  	return &val
    11  }
    12  
    13  // StringPtrToSimple if nil then 0
    14  func StringPtrToSimple(val *string) string {
    15  	if val == nil {
    16  		return constants.EmptyString
    17  	}
    18  
    19  	return *val
    20  }
    21  
    22  // StringPtrToSimpleDef if nil then 0
    23  func StringPtrToSimpleDef(val *string, defVal string) string {
    24  	if val == nil {
    25  		return defVal
    26  	}
    27  
    28  	return *val
    29  }
    30  
    31  // StringPtrToDefPtr if nil then 0
    32  func StringPtrToDefPtr(val *string, defVal string) *string {
    33  	if val == nil {
    34  		return &defVal
    35  	}
    36  
    37  	return val
    38  }
    39  
    40  // StringPtrDefValFunc if nil then executes returns defValFunc result as pointer
    41  func StringPtrDefValFunc(val *string, defValFunc func() string) *string {
    42  	if val == nil {
    43  		result := defValFunc()
    44  
    45  		return &result
    46  	}
    47  
    48  	return val
    49  }
    50  
    51  func StringToBool(s string) bool {
    52  	if s == "" {
    53  		return false
    54  	}
    55  
    56  	switch s {
    57  	case "yes", "Yes", "YES":
    58  		return true
    59  	case "no", "NO", "No":
    60  		return false
    61  	}
    62  
    63  	isBool, err := strconv.ParseBool(s)
    64  
    65  	if err != nil {
    66  		return false
    67  	}
    68  
    69  	return isBool
    70  }
    71  
    72  func StringPointerToBool(s *string) bool {
    73  	if s == nil || *s == "" {
    74  		return false
    75  	}
    76  
    77  	return StringToBool(*s)
    78  }
    79  
    80  func StringPointerToBoolPtr(s *string) *bool {
    81  	if s == nil || *s == "" {
    82  		toFalse := false
    83  
    84  		return &toFalse
    85  	}
    86  
    87  	result := StringToBool(*s)
    88  
    89  	return &result
    90  }
    91  
    92  func StringToBoolPtr(s string) *bool {
    93  	if s == "" {
    94  		toFalse := false
    95  
    96  		return &toFalse
    97  	}
    98  
    99  	result := StringToBool(s)
   100  
   101  	return &result
   102  }