github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/default_setter/default_setter.go (about)

     1  package default_setter
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
     9  )
    10  
    11  func Set(dft, tar interface{}) error {
    12  	// TODO should traverse all filed if struct
    13  	// make sure tar can set
    14  	rvTar := reflectx.Indirect(reflect.ValueOf(tar))
    15  	rtTar := reflect.TypeOf(tar)
    16  	if !rvTar.CanSet() {
    17  		return errors.Errorf("invalid tar parameter: %v", rtTar)
    18  	}
    19  	rvDft := reflectx.Indirect(reflect.ValueOf(dft))
    20  	rtDft := reflect.TypeOf(dft)
    21  
    22  	if rtDft.AssignableTo(rtTar) {
    23  		return errors.Errorf("unassignable from %v to %v", rtDft, rtTar)
    24  	}
    25  	rvTar.Set(rvDft)
    26  	return nil
    27  }