github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/convert.go (about) 1 package utils 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "strings" 7 ) 8 9 /* 10 String Conversion helper functions 11 */ 12 13 func StrToFloat64(s string) float64 { 14 v, _ := strconv.ParseFloat(s, 64) 15 16 return v 17 } 18 19 func StrToFloat32(s string) float32 { 20 v, _ := strconv.ParseFloat(s, 32) 21 22 return float32(v) 23 } 24 25 func StrToInt64(s string) int64 { 26 v, _ := strconv.ParseInt(s, 10, 64) 27 28 return v 29 } 30 31 func StrToInt32(s string) int32 { 32 v, _ := strconv.ParseInt(s, 10, 32) 33 34 return int32(v) 35 } 36 37 func StrToUInt64(s string) uint64 { 38 v, _ := strconv.ParseInt(s, 10, 64) 39 40 return uint64(v) 41 } 42 43 func StrToUInt32(s string) uint32 { 44 v, _ := strconv.ParseInt(s, 10, 32) 45 46 return uint32(v) 47 } 48 49 func StrToInt(s string) int { 50 v, _ := strconv.ParseInt(s, 10, 32) 51 52 return int(v) 53 } 54 55 func StrToUInt(s string) uint { 56 v, _ := strconv.ParseInt(s, 10, 32) 57 58 return uint(v) 59 } 60 61 func Int64ToStr(x int64) string { 62 return strconv.FormatInt(x, 10) 63 } 64 65 func Int32ToStr(x int32) string { 66 return strconv.FormatInt(int64(x), 10) 67 } 68 69 func UInt64ToStr(x uint64) string { 70 return strconv.FormatUint(x, 10) 71 } 72 73 func UInt32ToStr(x uint32) string { 74 return strconv.FormatUint(uint64(x), 10) 75 } 76 77 func Float64ToStr(x float64) string { 78 return strconv.FormatFloat(x, 'f', -1, 64) 79 } 80 81 func F64ToStr(x float64) string { 82 return Float64ToStr(x) 83 } 84 85 func Float32ToStr(x float32) string { 86 return strconv.FormatFloat(float64(x), 'f', -1, 32) 87 } 88 89 func F32ToStr(x float32) string { 90 return Float32ToStr(x) 91 } 92 93 func IntToStr(x int) string { 94 return strconv.FormatUint(uint64(x), 10) 95 } 96 97 func UIntToStr(x uint) string { 98 return strconv.FormatUint(uint64(x), 10) 99 } 100 101 func StrTruncate(s string, maxSize int) string { 102 count := 0 103 builder := strings.Builder{} 104 for _, char := range s { 105 if maxSize <= 0 { 106 break 107 } 108 builder.WriteString(string(char)) 109 110 count++ 111 if count >= maxSize { 112 break 113 } 114 } 115 116 return builder.String() 117 } 118 119 // Numeric represents float64 number which is decodable from string, int or float. 120 // It's useful when a struct field should be numeric but form of the data being decoded from is unknown or variable. 121 type Numeric struct { 122 value float64 123 precision int 124 } 125 126 const defaultPrecision = 2 127 128 func (n *Numeric) UnmarshalJSON(bb []byte) error { 129 type medium any 130 m := new(medium) 131 if err := json.Unmarshal(bb, m); err != nil { 132 return err 133 } 134 135 *n = ParseNumeric(*m) 136 137 return nil 138 } 139 140 func (n Numeric) MarshalJSON() ([]byte, error) { 141 return json.Marshal(n.String()) 142 } 143 144 func (n Numeric) Value() float64 { 145 return n.value 146 } 147 148 func (n Numeric) String() string { 149 if n.precision == 0 { 150 n.precision = defaultPrecision 151 } 152 153 return strconv.FormatFloat(n.Value(), 'f', n.precision, 64) 154 } 155 156 func (n Numeric) WithPrecision(p int) Numeric { 157 n.precision = p 158 159 return n 160 } 161 162 func (n Numeric) WithoutPrecision() Numeric { 163 return n.WithPrecision(-1) 164 } 165 166 // ParseNumeric converts int, string, float to Numeric. 167 func ParseNumeric(src any) Numeric { 168 var number float64 169 switch v := src.(type) { 170 case float64: 171 number = v 172 173 case string: 174 number, _ = strconv.ParseFloat(v, 64) 175 176 case int64: 177 number = float64(v) 178 179 case float32: 180 return ParseNumeric(float64(v)) 181 182 case int: 183 return ParseNumeric(int64(v)) 184 } 185 186 return Numeric{ 187 value: number, 188 precision: defaultPrecision, 189 } 190 }