github.com/enbility/spine-go@v0.7.0/spine/util.go (about)

     1  package spine
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  
     7  	"github.com/enbility/spine-go/api"
     8  	"github.com/enbility/spine-go/model"
     9  )
    10  
    11  var notFoundError = errors.New("data not found")
    12  
    13  func dataCopyOfType[T any](rdata any) (T, error) {
    14  	x := any(*new(T))
    15  
    16  	if rdata == nil {
    17  		return x.(T), notFoundError
    18  	}
    19  
    20  	v := reflect.ValueOf(rdata)
    21  	kind := v.Kind()
    22  	if kind == reflect.Ptr && v.IsNil() {
    23  		return x.(T), notFoundError
    24  	}
    25  
    26  	data, ok := rdata.(T)
    27  	if !ok {
    28  		return x.(T), notFoundError
    29  	}
    30  
    31  	return data, nil
    32  }
    33  
    34  // Note: the type has to be a pointer!
    35  func LocalFeatureDataCopyOfType[T any](feature api.FeatureLocalInterface, function model.FunctionType) (T, error) {
    36  	return dataCopyOfType[T](feature.DataCopy(function))
    37  }
    38  
    39  // Note: the type has to be a pointer!
    40  func RemoteFeatureDataCopyOfType[T any](remote api.FeatureRemoteInterface, function model.FunctionType) (T, error) {
    41  	return dataCopyOfType[T](remote.DataCopy(function))
    42  }