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

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