gitlab.com/evatix-go/core@v1.3.55/keymk/templateReplacer.go (about)

     1  package keymk
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  )
     7  
     8  type templateReplacer struct {
     9  	key *Key
    10  }
    11  
    12  func (it *templateReplacer) RequestIntRange(
    13  	isCurly bool,
    14  	tempReplace TempReplace,
    15  ) []string {
    16  	return it.IntRange(
    17  		isCurly,
    18  		tempReplace.KeyName,
    19  		tempReplace.Range.Start,
    20  		tempReplace.Range.End)
    21  }
    22  
    23  func (it *templateReplacer) IntRange(
    24  	isCurly bool,
    25  	keyName string,
    26  	startIncluding, endIncluding int,
    27  ) []string {
    28  	keyOuts := make(
    29  		[]string,
    30  		0,
    31  		endIncluding-startIncluding+1)
    32  
    33  	keyName = curlyWrapIf(isCurly, keyName)
    34  	templateFormat := it.key.KeyCompiled() // format may hold {key-name}
    35  
    36  	for i := startIncluding; i <= endIncluding; i++ {
    37  		numString := strconv.Itoa(i)
    38  
    39  		keyOuts = append(
    40  			keyOuts,
    41  			strings.ReplaceAll(
    42  				templateFormat,
    43  				keyName,
    44  				numString))
    45  	}
    46  
    47  	return keyOuts
    48  }
    49  
    50  func (it *templateReplacer) CompileUsingReplacerMap(
    51  	isCurly bool,
    52  	replacerMap map[string]string, // key ==> find, value ==> replace
    53  ) string {
    54  	templateFormat := it.key.KeyCompiled() // format may hold {key-name}
    55  
    56  	if templateFormat == "" || len(replacerMap) == 0 {
    57  		return templateFormat
    58  	}
    59  
    60  	for finder, replacer := range replacerMap {
    61  		finderCurly := curlyWrapIf(isCurly, finder)
    62  
    63  		templateFormat = strings.ReplaceAll(
    64  			templateFormat,
    65  			finderCurly,
    66  			replacer)
    67  	}
    68  
    69  	return templateFormat
    70  }