gitlab.com/evatix-go/core@v1.3.55/internal/strutilinternal/SliceToMapConverter.go (about)

     1  package strutilinternal
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  type SliceToMapConverter []string
     8  
     9  func (it SliceToMapConverter) SafeStrings() []string {
    10  	if it.IsEmpty() {
    11  		return []string{}
    12  	}
    13  
    14  	return it
    15  }
    16  
    17  func (it SliceToMapConverter) Strings() []string {
    18  	return it
    19  }
    20  
    21  func (it SliceToMapConverter) Hashset() map[string]bool {
    22  	length := it.Length()
    23  	hashset := make(map[string]bool, length)
    24  
    25  	for _, s := range it {
    26  		hashset[s] = true
    27  	}
    28  
    29  	return hashset
    30  }
    31  
    32  func (it *SliceToMapConverter) Length() int {
    33  	if it == nil || *it == nil {
    34  		return 0
    35  	}
    36  
    37  	return len(*it)
    38  }
    39  
    40  func (it *SliceToMapConverter) IsEmpty() bool {
    41  	return it.Length() == 0
    42  }
    43  
    44  func (it *SliceToMapConverter) HasAnyItem() bool {
    45  	return it.Length() > 0
    46  }
    47  
    48  func (it *SliceToMapConverter) LastIndex() int {
    49  	return it.Length() - 1
    50  }
    51  
    52  func (it SliceToMapConverter) LineSplitMapOptions(
    53  	isTrim bool,
    54  	splitter string,
    55  ) map[string]string {
    56  	if isTrim {
    57  		return it.LineSplitMapTrim(splitter)
    58  	}
    59  
    60  	return it.LineSplitMap(splitter)
    61  }
    62  
    63  func (it SliceToMapConverter) LineProcessorMapOptions(
    64  	isTrimBefore bool,
    65  	processorFunc func(line string) (key, val string),
    66  ) map[string]string {
    67  	length := it.Length()
    68  	if processorFunc == nil || length == 0 {
    69  		return map[string]string{}
    70  	}
    71  
    72  	newMap := make(map[string]string, length+1)
    73  
    74  	if isTrimBefore {
    75  		for _, line := range it {
    76  			trimmedLine := strings.TrimSpace(line)
    77  
    78  			if trimmedLine == "" {
    79  				continue
    80  			}
    81  
    82  			k, v := processorFunc(line)
    83  			newMap[k] = v
    84  		}
    85  
    86  		return newMap
    87  	}
    88  
    89  	for _, line := range it {
    90  		k, v := processorFunc(line)
    91  		newMap[k] = v
    92  	}
    93  
    94  	return newMap
    95  }
    96  
    97  func (it SliceToMapConverter) LineProcessorMapStringIntegerTrim(
    98  	processorFunc func(line string) (key string, val int),
    99  ) map[string]int {
   100  	return it.LineProcessorMapStringIntegerOptions(
   101  		true,
   102  		processorFunc)
   103  }
   104  
   105  func (it SliceToMapConverter) LineProcessorMapStringIntegerOptions(
   106  	isTrimBefore bool,
   107  	processorFunc func(line string) (key string, val int),
   108  ) map[string]int {
   109  	length := it.Length()
   110  	if processorFunc == nil || length == 0 {
   111  		return map[string]int{}
   112  	}
   113  
   114  	newMap := make(map[string]int, length+1)
   115  
   116  	if isTrimBefore {
   117  		for _, line := range it {
   118  			trimmedLine := strings.TrimSpace(line)
   119  
   120  			if trimmedLine == "" {
   121  				continue
   122  			}
   123  
   124  			k, v := processorFunc(line)
   125  			newMap[k] = v
   126  		}
   127  
   128  		return newMap
   129  	}
   130  
   131  	for _, line := range it {
   132  		k, v := processorFunc(line)
   133  		newMap[k] = v
   134  	}
   135  
   136  	return newMap
   137  }
   138  
   139  func (it SliceToMapConverter) LineProcessorMapStringAnyTrim(
   140  	processorFunc func(line string) (key string, val interface{}),
   141  ) map[string]interface{} {
   142  	return it.LineProcessorMapStringAnyOptions(
   143  		true,
   144  		processorFunc)
   145  }
   146  
   147  func (it SliceToMapConverter) LineProcessorMapStringAnyOptions(
   148  	isTrimBefore bool,
   149  	processorFunc func(line string) (key string, val interface{}),
   150  ) map[string]interface{} {
   151  	length := it.Length()
   152  	if processorFunc == nil || length == 0 {
   153  		return map[string]interface{}{}
   154  	}
   155  
   156  	newMap := make(map[string]interface{}, length+1)
   157  
   158  	if isTrimBefore {
   159  		for _, line := range it {
   160  			trimmedLine := strings.TrimSpace(line)
   161  
   162  			if trimmedLine == "" {
   163  				continue
   164  			}
   165  
   166  			k, v := processorFunc(line)
   167  			newMap[k] = v
   168  		}
   169  
   170  		return newMap
   171  	}
   172  
   173  	for _, line := range it {
   174  		k, v := processorFunc(line)
   175  		newMap[k] = v
   176  	}
   177  
   178  	return newMap
   179  }
   180  
   181  func (it SliceToMapConverter) LineSplitMapTrim(
   182  	splitter string,
   183  ) map[string]string {
   184  	length := it.Length()
   185  	if length == 0 {
   186  		return map[string]string{}
   187  	}
   188  
   189  	newMap := make(map[string]string, length+1)
   190  
   191  	for _, line := range it {
   192  		trimmedLine := strings.TrimSpace(line)
   193  
   194  		if trimmedLine == "" {
   195  			continue
   196  		}
   197  
   198  		k, v := SplitLeftRightTrim(
   199  			splitter,
   200  			line)
   201  
   202  		newMap[k] = v
   203  	}
   204  
   205  	return newMap
   206  }
   207  
   208  func (it SliceToMapConverter) LineSplitMap(
   209  	splitter string,
   210  ) map[string]string {
   211  	length := it.Length()
   212  	if length == 0 {
   213  		return map[string]string{}
   214  	}
   215  
   216  	newMap := make(map[string]string, length+1)
   217  
   218  	for _, line := range it {
   219  		k, v := SplitLeftRight(
   220  			splitter,
   221  			line)
   222  
   223  		newMap[k] = v
   224  	}
   225  
   226  	return newMap
   227  }