gitlab.com/evatix-go/core@v1.3.55/coreutils/stringutil/replaceTemplate.go (about) 1 package stringutil 2 3 import ( 4 "fmt" 5 "strings" 6 7 "gitlab.com/evatix-go/core/constants" 8 ) 9 10 type replaceTemplate struct{} 11 12 func (it *replaceTemplate) CurlyOne( 13 format string, // {key}-text... 14 firstKey string, firstValue interface{}, 15 ) string { 16 if len(format) == 0 { 17 return format 18 } 19 20 return it.UsingMapOptions( 21 true, 22 format, 23 map[string]string{ 24 firstKey: fmt.Sprintf( 25 constants.SprintValueFormat, 26 firstValue), 27 }) 28 } 29 30 func (it *replaceTemplate) Curly( 31 format string, // {key}-text... 32 mapToReplace map[string]string, 33 ) string { 34 if len(format) == 0 { 35 return format 36 } 37 38 return it.UsingMapOptions( 39 true, 40 format, 41 mapToReplace) 42 43 } 44 45 func (it *replaceTemplate) CurlyTwo( 46 format string, // {key}-text... 47 firstKey string, firstValue interface{}, 48 secondKey string, secondValue interface{}, 49 ) string { 50 if len(format) == 0 { 51 return format 52 } 53 54 return it.UsingMapOptions( 55 true, 56 format, 57 map[string]string{ 58 firstKey: fmt.Sprintf( 59 constants.SprintValueFormat, 60 firstValue), 61 secondKey: fmt.Sprintf( 62 constants.SprintValueFormat, 63 secondValue), 64 }) 65 } 66 67 func (it *replaceTemplate) DirectOne( 68 format string, // key-text... 69 firstKey string, firstValue interface{}, 70 ) string { 71 if len(format) == 0 { 72 return format 73 } 74 75 return strings.ReplaceAll( 76 format, 77 firstKey, 78 fmt.Sprintf( 79 constants.SprintValueFormat, 80 firstValue)) 81 } 82 83 func (it *replaceTemplate) DirectTwoItem( 84 format string, // key-text... 85 firstKey string, firstValue interface{}, 86 secondKey string, secondValue interface{}, 87 ) string { 88 if len(format) == 0 { 89 return format 90 } 91 92 return it.UsingMapOptions( 93 false, 94 format, 95 map[string]string{ 96 firstKey: fmt.Sprintf( 97 constants.SprintValueFormat, 98 firstValue), 99 secondKey: fmt.Sprintf( 100 constants.SprintValueFormat, 101 secondValue), 102 }) 103 } 104 105 func (it *replaceTemplate) CurlyTwoItem( 106 format string, // {key}-text... 107 firstKey string, firstValue interface{}, 108 secondKey string, secondValue interface{}, 109 ) string { 110 if len(format) == 0 { 111 return format 112 } 113 114 return it.UsingMapOptions( 115 true, 116 format, 117 map[string]string{ 118 firstKey: fmt.Sprintf( 119 constants.SprintValueFormat, 120 firstValue), 121 secondKey: fmt.Sprintf( 122 constants.SprintValueFormat, 123 secondValue), 124 }) 125 } 126 127 func (it *replaceTemplate) DirectKeyUsingMap( 128 format string, // key-text... 129 mapToReplace map[string]string, 130 ) string { 131 if len(mapToReplace) == 0 || len(format) == 0 { 132 return format 133 } 134 135 return it.UsingMapOptions( 136 false, 137 format, 138 mapToReplace) 139 } 140 141 func (it *replaceTemplate) CurlyKeyUsingMap( 142 format string, // {key}-text... 143 mapToReplace map[string]string, 144 ) string { 145 if len(mapToReplace) == 0 || len(format) == 0 { 146 return format 147 } 148 149 return it.UsingMapOptions( 150 true, 151 format, 152 mapToReplace) 153 } 154 155 // UsingMapOptions 156 // 157 // Replaces format template using the map given. 158 // 159 // format : {key}-text-something/{path}... 160 // 161 // Options: 162 // - isConvKeysToCurlyBraceKeys : true 163 // will convert map keys to {key} and then use 164 // recursive replace to reduce the template format. 165 func (it *replaceTemplate) UsingMapOptions( 166 isConvKeysToCurlyBraceKeys bool, // true: conv key to {key} before replace 167 format string, // Template-format: {key}-text-something/{path}... 168 mapToReplace map[string]string, 169 ) string { 170 if len(mapToReplace) == 0 || len(format) == 0 { 171 return format 172 } 173 174 if isConvKeysToCurlyBraceKeys { 175 for key, valueToReplace := range mapToReplace { 176 keyCurly := fmt.Sprintf( 177 constants.CurlyWrapFormat, 178 key) 179 180 format = strings.ReplaceAll( 181 format, 182 keyCurly, 183 valueToReplace) 184 } 185 186 return format 187 } 188 189 for key, valueToReplace := range mapToReplace { 190 format = strings.ReplaceAll( 191 format, 192 key, 193 valueToReplace) 194 } 195 196 return format 197 } 198 199 func (it *replaceTemplate) UsingNamerMapOptions( 200 isConvKeysToCurlyBraceKeys bool, // true: conv key to {key} before replace 201 format string, // Template-format: {key}-text-something/{path}... 202 mapToReplace map[namer]string, 203 ) string { 204 if len(mapToReplace) == 0 || len(format) == 0 { 205 return format 206 } 207 208 if isConvKeysToCurlyBraceKeys { 209 for key, valueToReplace := range mapToReplace { 210 keyCurly := fmt.Sprintf( 211 constants.CurlyWrapFormat, 212 key) 213 214 format = strings.ReplaceAll( 215 format, 216 keyCurly, 217 valueToReplace) 218 } 219 220 return format 221 } 222 223 for keyNamer, valueToReplace := range mapToReplace { 224 format = strings.ReplaceAll( 225 format, 226 keyNamer.Name(), 227 valueToReplace) 228 } 229 230 return format 231 } 232 233 func (it *replaceTemplate) UsingStringerMapOptions( 234 isConvKeysToCurlyBraceKeys bool, // true: conv key to {key} before replace 235 format string, // Template-format: {key}-text-something/{path}... 236 mapToReplace map[fmt.Stringer]string, 237 ) string { 238 if len(mapToReplace) == 0 || len(format) == 0 { 239 return format 240 } 241 242 if isConvKeysToCurlyBraceKeys { 243 for key, valueToReplace := range mapToReplace { 244 keyCurly := fmt.Sprintf( 245 constants.CurlyWrapFormat, 246 key) 247 248 format = strings.ReplaceAll( 249 format, 250 keyCurly, 251 valueToReplace) 252 } 253 254 return format 255 } 256 257 for keyStringer, valueToReplace := range mapToReplace { 258 format = strings.ReplaceAll( 259 format, 260 keyStringer.String(), 261 valueToReplace) 262 } 263 264 return format 265 } 266 267 // UsingWrappedTemplate 268 // 269 // Replaces all constants.WrappedTemplate {wrapped} with replaceText 270 // 271 // Format: 272 // - "some format {wrapped} text here." 273 func (it *replaceTemplate) UsingWrappedTemplate( 274 format, 275 replacingText string, 276 ) string { 277 if len(format) == 0 { 278 return format 279 } 280 281 return strings.ReplaceAll( 282 format, 283 constants.WrappedTemplate, 284 replacingText) 285 } 286 287 // UsingBracketsWrappedTemplate 288 // 289 // Replaces all constants.BracketsWrappedTemplate {brackets-wrapped} with replaceText 290 // 291 // Format: 292 // - "some format {brackets-wrapped} text here." 293 func (it *replaceTemplate) UsingBracketsWrappedTemplate( 294 format, 295 replacingText string, 296 ) string { 297 if len(format) == 0 { 298 return format 299 } 300 301 return strings.ReplaceAll( 302 format, 303 constants.BracketsWrappedTemplate, 304 replacingText) 305 } 306 307 // UsingQuotesWrappedTemplate 308 // 309 // Replaces all constants.QuotesWrappedTemplate "{quotes-wrapped}" with replaceText 310 // 311 // Format: 312 // - "some format {quotes-wrapped} text here." 313 func (it *replaceTemplate) UsingQuotesWrappedTemplate( 314 format, 315 replacingText string, 316 ) string { 317 if len(format) == 0 { 318 return format 319 } 320 321 return strings.ReplaceAll( 322 format, 323 constants.QuotesWrappedTemplate, 324 replacingText) 325 } 326 327 // UsingValueTemplate 328 // 329 // Replaces all constants.ValueTemplate "{value}" with replaceText 330 // 331 // Format: 332 // - "some format {value} text here." 333 func (it *replaceTemplate) UsingValueTemplate( 334 format, 335 replacingText string, 336 ) string { 337 if len(format) == 0 { 338 return format 339 } 340 341 return strings.ReplaceAll( 342 format, 343 constants.ValueTemplate, 344 replacingText) 345 } 346 347 // UsingValueWithFieldsTemplate 348 // 349 // Replaces all constants.ValueWithFieldsTemplate "{value-fields}" with replaceText 350 // 351 // Format: 352 // - "some format {value-fields} text here." 353 func (it *replaceTemplate) UsingValueWithFieldsTemplate( 354 format, 355 replacingText string, 356 ) string { 357 if len(format) == 0 { 358 return format 359 } 360 361 return strings.ReplaceAll( 362 format, 363 constants.ValueWithFieldsTemplate, 364 replacingText) 365 }