gitlab.com/evatix-go/core@v1.3.55/coredata/stringslice/SafeIndexesDefaultWithDetail.go (about)

     1  package stringslice
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/defaultcapacity"
     6  )
     7  
     8  // SafeIndexesDefaultWithDetail Only indexes which are present values will be included.
     9  //
    10  // Warning : Not found indexes will not be included in the values.
    11  func SafeIndexesDefaultWithDetail(
    12  	slice []string,
    13  	indexes ...int,
    14  ) *IndexValuesDetail {
    15  	length := len(slice)
    16  
    17  	if length == 0 {
    18  		return InvalidIndexValuesDetail()
    19  	}
    20  
    21  	values := make(
    22  		[]string,
    23  		constants.Zero,
    24  		length)
    25  	missingIndexes := make(
    26  		[]int,
    27  		constants.Zero,
    28  		defaultcapacity.OfSearch(length))
    29  
    30  	inputIndex := 0
    31  	lastIndex := length - 1
    32  	for _, index := range indexes {
    33  		if index > lastIndex || index < 0 {
    34  			missingIndexes = append(
    35  				missingIndexes,
    36  				index)
    37  			// don't include
    38  			continue
    39  		}
    40  
    41  		values = append(values, slice[index])
    42  		inputIndex++
    43  	}
    44  
    45  	return &IndexValuesDetail{
    46  		Values:         values,
    47  		MissingIndexes: missingIndexes,
    48  		IsAnyMissing:   len(missingIndexes) > 0,
    49  		IsValid:        true,
    50  	}
    51  }