gitee.com/quant1x/engine@v1.8.4/factors/feature_test.go (about)

     1  package factors
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  	"unsafe"
     9  )
    10  
    11  //func TestNewDataBuilder(t *testing.T) {
    12  //	date := "2023-07-04"
    13  //	v := NewDataBuilder("test", date, nil)
    14  //	fmt.Println(v)
    15  //}
    16  
    17  // typelinks2 for 1.7 ~
    18  //
    19  //go:linkname typelinks2 reflect.typelinks
    20  func typelinks2() (sections []unsafe.Pointer, offset [][]int32)
    21  
    22  //go:linkname resolveTypeOff reflect.resolveTypeOff
    23  func resolveTypeOff(rtype unsafe.Pointer, off int32) unsafe.Pointer
    24  
    25  var types map[string]reflect.Type
    26  var packages map[string]map[string]reflect.Type
    27  
    28  type emptyInterface struct {
    29  	typ  unsafe.Pointer
    30  	word unsafe.Pointer
    31  }
    32  
    33  func loadGoTypes() {
    34  	fia := reflect.TypeOf((*Feature)(nil)).Elem()
    35  	var obj interface{} = reflect.TypeOf(0)
    36  	sections, offset := typelinks2()
    37  	for i, offs := range offset {
    38  		rodata := sections[i]
    39  		for _, off := range offs {
    40  			(*emptyInterface)(unsafe.Pointer(&obj)).word = resolveTypeOff(unsafe.Pointer(rodata), off)
    41  			typ := obj.(reflect.Type)
    42  			if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
    43  				loadedType := typ.Elem()
    44  				pkgTypes := packages[loadedType.PkgPath()]
    45  				if pkgTypes == nil {
    46  					pkgTypes = map[string]reflect.Type{}
    47  					packages[loadedType.PkgPath()] = pkgTypes
    48  				}
    49  				typeName := loadedType.String()
    50  				//fmt.Println(typeName, "==>", loadedType.PkgPath())
    51  				types[typeName] = loadedType
    52  				pkgTypes[loadedType.Name()] = loadedType
    53  				//if loadedType.Implements(Feature) {
    54  				//	fmt.Println(typeName, "==>", loadedType.PkgPath())
    55  				//}
    56  				pkgPath := loadedType.PkgPath()
    57  				if strings.HasPrefix(pkgPath, "gitee.com/quant1x/engine") {
    58  					//fmt.Println(typeName, "==>", loadedType.PkgPath())
    59  					if reflect.PtrTo(loadedType).Implements(fia) {
    60  						fmt.Println("found", pkgPath, "==>", loadedType.Name())
    61  					}
    62  				}
    63  			}
    64  		}
    65  	}
    66  }
    67  
    68  func TestInterface(t *testing.T) {
    69  	types = make(map[string]reflect.Type)
    70  	packages = make(map[string]map[string]reflect.Type)
    71  	loadGoTypes()
    72  }