gitlab.com/evatix-go/core@v1.3.55/internal/reflectinternal/ReflectValToInterfacesUsingProcessor.go (about)

     1  package reflectinternal
     2  
     3  import "reflect"
     4  
     5  func ReflectValToInterfacesUsingProcessor(
     6  	isSkipOnNil bool,
     7  	processorFunc func(item interface{}) (result interface{}, isTake, isBreak bool),
     8  	reflectVal reflect.Value,
     9  ) []interface{} {
    10  	if reflectVal.Kind() == reflect.Ptr {
    11  		return ReflectValToInterfaces(
    12  			isSkipOnNil,
    13  			reflect.Indirect(reflect.ValueOf(reflectVal)))
    14  	}
    15  
    16  	k := reflectVal.Kind()
    17  	isSliceOrArray := k == reflect.Slice ||
    18  		k == reflect.Array
    19  
    20  	if !isSliceOrArray {
    21  		return []interface{}{}
    22  	}
    23  
    24  	length := reflectVal.Len()
    25  	slice := make([]interface{}, 0, length)
    26  
    27  	if length == 0 {
    28  		return slice
    29  	}
    30  
    31  	for i := 0; i < length; i++ {
    32  		value := reflectVal.Index(i)
    33  
    34  		if value.Kind() == reflect.Ptr {
    35  			value = value.Elem()
    36  		}
    37  
    38  		valueInf := value.Interface()
    39  
    40  		if isSkipOnNil && IsNull(valueInf) {
    41  			continue
    42  		}
    43  
    44  		rs, isTake, isBreak :=
    45  			processorFunc(valueInf)
    46  
    47  		if isTake {
    48  			slice = append(slice, rs)
    49  		}
    50  
    51  		if isBreak {
    52  			return slice
    53  		}
    54  	}
    55  
    56  	return slice
    57  }