github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/query/scanner/named.go (about)

     1  package scanner
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
     9  )
    10  
    11  type (
    12  	NamedScanner struct {
    13  		data *data
    14  	}
    15  	NamedDestination struct {
    16  		name string
    17  		ref  interface{}
    18  	}
    19  )
    20  
    21  func NamedRef(columnName string, destinationValueReference interface{}) (dst NamedDestination) {
    22  	if columnName == "" {
    23  		panic("columnName must be not empty")
    24  	}
    25  	dst.name = columnName
    26  	v := reflect.TypeOf(destinationValueReference)
    27  	if v.Kind() != reflect.Ptr {
    28  		panic(fmt.Errorf("%T is not reference type", destinationValueReference))
    29  	}
    30  	dst.ref = destinationValueReference
    31  
    32  	return dst
    33  }
    34  
    35  func Named(data *data) NamedScanner {
    36  	return NamedScanner{
    37  		data: data,
    38  	}
    39  }
    40  
    41  func (s NamedScanner) ScanNamed(dst ...NamedDestination) error {
    42  	for i := range dst {
    43  		v, err := s.data.seekByName(dst[i].name)
    44  		if err != nil {
    45  			return xerrors.WithStackTrace(err)
    46  		}
    47  		if err = value.CastTo(v, dst[i].ref); err != nil {
    48  			return xerrors.WithStackTrace(err)
    49  		}
    50  	}
    51  
    52  	return nil
    53  }