github.com/goplus/xtypes@v0.2.1/xcall.go (about)

     1  package xtypes
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  
     7  	xcall "github.com/goplus/xtypes/internal/reflect"
     8  )
     9  
    10  func FieldAddr(v interface{}, index int) (interface{}, error) {
    11  	x := xcall.ValueOf(v).Elem()
    12  	if !x.IsValid() {
    13  		return nil, errors.New("invalid memory address or nil pointer dereference")
    14  	}
    15  	return x.Field(index).Addr().Interface(), nil
    16  }
    17  
    18  func Field(v interface{}, index int) (interface{}, error) {
    19  	x := xcall.ValueOf(v)
    20  	for x.Kind() == xcall.Ptr {
    21  		x = x.Elem()
    22  	}
    23  	if !x.IsValid() {
    24  		return nil, errors.New("invalid memory address or nil pointer dereference")
    25  	}
    26  	return x.Field(index).Interface(), nil
    27  }
    28  
    29  func methodByName(v reflect.Value, name string) xcall.Value {
    30  	return xcall.MethodByName(v, name)
    31  }
    32  
    33  func callValue(v xcall.Value, args []reflect.Value) []reflect.Value {
    34  	return xcall.Call(v, args)
    35  }