github.com/blend/go-sdk@v1.20220411.3/reflectutil/slice_type.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package reflectutil
     9  
    10  import "reflect"
    11  
    12  // SliceType returns the inner type of a slice following pointers.
    13  func SliceType(collection interface{}) reflect.Type {
    14  	v := reflect.ValueOf(collection)
    15  	for v.Kind() == reflect.Ptr {
    16  		v = v.Elem()
    17  	}
    18  	if v.Len() == 0 {
    19  		t := v.Type()
    20  		for t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
    21  			t = t.Elem()
    22  		}
    23  		return t
    24  	}
    25  	v = v.Index(0)
    26  	for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr {
    27  		v = v.Elem()
    28  	}
    29  
    30  	return v.Type()
    31  }