gitlab.com/evatix-go/core@v1.3.55/converters/anyItemConverter.go (about) 1 package converters 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "reflect" 8 "strings" 9 10 "gitlab.com/evatix-go/core/constants" 11 "gitlab.com/evatix-go/core/coreappend" 12 "gitlab.com/evatix-go/core/internal/reflectinternal" 13 ) 14 15 type anyItemConverter struct{} 16 17 func (it anyItemConverter) ToString( 18 isIncludeFullName bool, 19 any interface{}, 20 ) string { 21 if any == nil { 22 return "" 23 } 24 25 if isIncludeFullName { 26 return fmt.Sprintf( 27 constants.SprintFullPropertyNameValueFormat, 28 any) 29 } 30 31 return fmt.Sprintf( 32 constants.SprintValueFormat, 33 any) 34 } 35 36 func (it anyItemConverter) String( 37 any interface{}, 38 ) string { 39 if any == nil { 40 return "" 41 } 42 43 return fmt.Sprintf( 44 constants.SprintValueFormat, 45 any) 46 } 47 48 func (it anyItemConverter) FullString( 49 any interface{}, 50 ) string { 51 if any == nil { 52 return "" 53 } 54 55 return fmt.Sprintf( 56 constants.SprintPropertyNameValueFormat, 57 any) 58 } 59 60 func (it anyItemConverter) StringWithType( 61 any interface{}, 62 ) string { 63 if any == nil { 64 return "" 65 } 66 67 return fmt.Sprintf( 68 constants.SprintPropertyValueWithTypeFormat, 69 any, 70 any) 71 } 72 73 // ToSafeSerializedString 74 // 75 // warning : on error swallows it 76 func (it anyItemConverter) ToSafeSerializedString( 77 any interface{}, 78 ) string { 79 if any == nil { 80 return "" 81 } 82 83 switch casted := any.(type) { 84 case []byte: 85 return BytesToString(casted) 86 case *[]byte: 87 return BytesPtrToString(casted) 88 } 89 90 allBytes, _ := json.Marshal(any) 91 92 return BytesToString(allBytes) 93 } 94 95 // ToSafeSerializedStringSprintValue 96 // 97 // return value using %v 98 // 99 // warning : on error swallows it 100 func (it anyItemConverter) ToSafeSerializedStringSprintValue( 101 any interface{}, 102 ) string { 103 value := it.ToSafeSerializedString( 104 any) 105 106 return fmt.Sprintf( 107 constants.SprintValueFormat, 108 value) 109 } 110 111 func (it anyItemConverter) ToStrings( 112 isSkipOnNil bool, 113 anyItem interface{}, 114 ) []string { 115 if isSkipOnNil && anyItem == nil { 116 return []string{} 117 } 118 119 reflectVal := reflect.ValueOf(anyItem) 120 121 anyItems := reflectinternal.ReflectValToInterfaces( 122 isSkipOnNil, 123 reflectVal) 124 125 return it.ItemsToStringsSkipOnNil(anyItems) 126 } 127 128 func (it anyItemConverter) ToStringsUsingProcessor( 129 isSkipOnNil bool, 130 processor func(index int, in interface{}) (out string, isTake, isBreak bool), 131 any interface{}, 132 ) []string { 133 if any == nil { 134 return []string{} 135 } 136 137 anyItems := it.ToAnyItems(isSkipOnNil, any) 138 slice := make([]string, 0, len(anyItems)) 139 140 if len(anyItems) == 0 { 141 return slice 142 } 143 144 for i, item := range anyItems { 145 out, isTake, isBreak := processor(i, item) 146 147 if isTake { 148 slice = append(slice, out) 149 } 150 151 if isBreak { 152 return slice 153 } 154 } 155 156 return slice 157 } 158 159 func (it anyItemConverter) ToStringsUsingSimpleProcessor( 160 isSkipOnNil bool, 161 simpleProcessor func(index int, in interface{}) (out string), 162 any interface{}, 163 ) []string { 164 if any == nil { 165 return []string{} 166 } 167 168 anyItems := it.ToAnyItems(isSkipOnNil, any) 169 slice := make([]string, len(anyItems)) 170 171 if len(anyItems) == 0 { 172 return slice 173 } 174 175 for i, item := range anyItems { 176 out := simpleProcessor(i, item) 177 178 slice[i] = out 179 } 180 181 return slice 182 } 183 184 func (it anyItemConverter) ToValueString( 185 any interface{}, 186 ) string { 187 if any == nil { 188 return "" 189 } 190 191 return fmt.Sprintf( 192 constants.SprintValueFormat, 193 any) 194 } 195 196 func (it anyItemConverter) ToValueStringWithType( 197 any interface{}, 198 ) string { 199 if any == nil { 200 return fmt.Sprintf( 201 constants.SprintNilValueTypeInParenthesisFormat, 202 any) 203 } 204 205 return fmt.Sprintf( 206 constants.SprintValueWithTypeFormat, 207 any, 208 any) 209 } 210 211 func (it anyItemConverter) ToAnyItems( 212 isSkipOnNil bool, 213 anyItem interface{}, 214 ) []interface{} { 215 if isSkipOnNil && anyItem == nil { 216 return []interface{}{} 217 } 218 219 reflectVal := reflect.ValueOf(anyItem) 220 221 return reflectinternal.ReflectValToInterfaces( 222 isSkipOnNil, 223 reflectVal) 224 } 225 226 func (it anyItemConverter) ToNonNullItems( 227 isSkipOnNil bool, 228 anyItem interface{}, 229 ) []interface{} { 230 if isSkipOnNil && anyItem == nil || reflectinternal.IsNull(anyItem) { 231 return []interface{}{} 232 } 233 234 reflectVal := reflect.ValueOf(anyItem) 235 236 return reflectinternal.ReflectValToInterfaces( 237 isSkipOnNil, 238 reflectVal) 239 } 240 241 func (it anyItemConverter) ItemsToStringsSkipOnNil( 242 anyItems ...interface{}, 243 ) []string { 244 return coreappend.PrependAppendAnyItemsToStringsSkipOnNil( 245 nil, 246 nil, 247 anyItems...) 248 } 249 250 func (it anyItemConverter) ItemsJoin( 251 joiner string, 252 anyItems ...interface{}, 253 ) string { 254 if anyItems == nil { 255 return constants.EmptyString 256 } 257 258 anyStrings := it.ItemsToStringsSkipOnNil(anyItems...) 259 260 return strings.Join(anyStrings, joiner) 261 } 262 263 func (it anyItemConverter) ToItemsThenJoin( 264 isSkipOnNil bool, 265 joiner string, 266 anySlice interface{}, 267 ) string { 268 if anySlice == nil { 269 return constants.EmptyString 270 } 271 272 anyStrings := it.ToStrings( 273 isSkipOnNil, 274 anySlice) 275 276 return strings.Join( 277 anyStrings, 278 joiner) 279 } 280 281 func (it anyItemConverter) ToFullNameValueString( 282 any interface{}, 283 ) string { 284 if any == nil { 285 return "" 286 } 287 288 return fmt.Sprintf( 289 constants.SprintFullPropertyNameValueFormat, 290 any) 291 } 292 293 // ToPrettyJson 294 // 295 // Warning: 296 // swallows error 297 func (it anyItemConverter) ToPrettyJson( 298 anyItem interface{}, 299 ) string { 300 if anyItem == nil { 301 return "" 302 } 303 304 allBytes, err := json.Marshal(anyItem) 305 306 if err != nil || len(allBytes) == 0 { 307 return "" 308 } 309 310 var prettyJSON bytes.Buffer 311 312 json.Indent( 313 &prettyJSON, 314 allBytes, 315 constants.EmptyString, 316 constants.Tab) 317 318 return prettyJSON.String() 319 }