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

     1  package stringslice
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  )
     9  
    10  func AnyLinesProcessAsyncUsingProcessor(
    11  	lines interface{},
    12  	lineProcessor func(index int, lineIn interface{}) (lineOut string),
    13  ) []string {
    14  	if lines == nil {
    15  		return []string{}
    16  	}
    17  
    18  	reflectType := reflect.TypeOf(lines)
    19  	kind := reflectType.Kind()
    20  	isArrayOrSlice := kind == reflect.Slice ||
    21  		kind == reflect.Array
    22  
    23  	if !isArrayOrSlice {
    24  		return []string{}
    25  	}
    26  
    27  	reflectValue := reflect.ValueOf(lines)
    28  	length := reflectValue.Len()
    29  
    30  	if length == 0 {
    31  		return []string{}
    32  	}
    33  
    34  	slice := Make(constants.Zero, length)
    35  	wg := sync.WaitGroup{}
    36  
    37  	wg.Add(length)
    38  
    39  	asyncProcessor := func(index int, lineIn interface{}) {
    40  		slice[index] = lineProcessor(index, lineIn)
    41  
    42  		wg.Done()
    43  	}
    44  
    45  	for i := 0; i < length; i++ {
    46  		itemAt := reflectValue.Index(i)
    47  		go asyncProcessor(i, itemAt.Interface())
    48  	}
    49  
    50  	wg.Wait()
    51  
    52  	return slice
    53  }