github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/types/types.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
    10  )
    11  
    12  type Type interface {
    13  	Yql() string
    14  	String() string
    15  
    16  	ToYDB(a *allocator.Allocator) *Ydb.Type
    17  	equalsTo(rhs Type) bool
    18  }
    19  
    20  func TypeToYDB(t Type, a *allocator.Allocator) *Ydb.Type {
    21  	return t.ToYDB(a)
    22  }
    23  
    24  func TypeFromYDB(x *Ydb.Type) Type {
    25  	switch v := x.GetType().(type) {
    26  	case *Ydb.Type_TypeId:
    27  		return primitiveTypeFromYDB(v.TypeId)
    28  
    29  	case *Ydb.Type_OptionalType:
    30  		return NewOptional(TypeFromYDB(v.OptionalType.GetItem()))
    31  
    32  	case *Ydb.Type_ListType:
    33  		return NewList(TypeFromYDB(v.ListType.GetItem()))
    34  
    35  	case *Ydb.Type_DecimalType:
    36  		d := v.DecimalType
    37  
    38  		return NewDecimal(d.GetPrecision(), d.GetScale())
    39  
    40  	case *Ydb.Type_TupleType:
    41  		t := v.TupleType
    42  
    43  		return NewTuple(FromYDB(t.GetElements())...)
    44  
    45  	case *Ydb.Type_StructType:
    46  		s := v.StructType
    47  
    48  		return NewStruct(StructFields(s.GetMembers())...)
    49  
    50  	case *Ydb.Type_DictType:
    51  		keyType, valueType := TypeFromYDB(v.DictType.GetKey()), TypeFromYDB(v.DictType.GetPayload())
    52  		if valueType.equalsTo(NewVoid()) {
    53  			return NewSet(keyType)
    54  		}
    55  
    56  		return NewDict(keyType, valueType)
    57  
    58  	case *Ydb.Type_VariantType:
    59  		t := v.VariantType
    60  		switch x := t.GetType().(type) {
    61  		case *Ydb.VariantType_TupleItems:
    62  			return NewVariantTuple(FromYDB(x.TupleItems.GetElements())...)
    63  		case *Ydb.VariantType_StructItems:
    64  			return NewVariantStruct(StructFields(x.StructItems.GetMembers())...)
    65  		default:
    66  			panic("ydb: unknown variant type")
    67  		}
    68  
    69  	case *Ydb.Type_VoidType:
    70  		return NewVoid()
    71  
    72  	case *Ydb.Type_NullType:
    73  		return NewNull()
    74  
    75  	case *Ydb.Type_PgType:
    76  		return &PgType{
    77  			OID: x.GetPgType().GetOid(),
    78  		}
    79  
    80  	default:
    81  		panic("ydb: unknown type")
    82  	}
    83  }
    84  
    85  //nolint:funlen
    86  func primitiveTypeFromYDB(t Ydb.Type_PrimitiveTypeId) Type {
    87  	switch t {
    88  	case Ydb.Type_BOOL:
    89  		return Bool
    90  	case Ydb.Type_INT8:
    91  		return Int8
    92  	case Ydb.Type_UINT8:
    93  		return Uint8
    94  	case Ydb.Type_INT16:
    95  		return Int16
    96  	case Ydb.Type_UINT16:
    97  		return Uint16
    98  	case Ydb.Type_INT32:
    99  		return Int32
   100  	case Ydb.Type_UINT32:
   101  		return Uint32
   102  	case Ydb.Type_INT64:
   103  		return Int64
   104  	case Ydb.Type_UINT64:
   105  		return Uint64
   106  	case Ydb.Type_FLOAT:
   107  		return Float
   108  	case Ydb.Type_DOUBLE:
   109  		return Double
   110  	case Ydb.Type_DATE:
   111  		return Date
   112  	case Ydb.Type_DATETIME:
   113  		return Datetime
   114  	case Ydb.Type_TIMESTAMP:
   115  		return Timestamp
   116  	case Ydb.Type_INTERVAL:
   117  		return Interval
   118  	case Ydb.Type_TZ_DATE:
   119  		return TzDate
   120  	case Ydb.Type_TZ_DATETIME:
   121  		return TzDatetime
   122  	case Ydb.Type_TZ_TIMESTAMP:
   123  		return TzTimestamp
   124  	case Ydb.Type_STRING:
   125  		return Bytes
   126  	case Ydb.Type_UTF8:
   127  		return Text
   128  	case Ydb.Type_YSON:
   129  		return YSON
   130  	case Ydb.Type_JSON:
   131  		return JSON
   132  	case Ydb.Type_UUID:
   133  		return UUID
   134  	case Ydb.Type_JSON_DOCUMENT:
   135  		return JSONDocument
   136  	case Ydb.Type_DYNUMBER:
   137  		return DyNumber
   138  	default:
   139  		panic("ydb: unexpected type")
   140  	}
   141  }
   142  
   143  func FromYDB(es []*Ydb.Type) []Type {
   144  	ts := make([]Type, len(es))
   145  	for i, el := range es {
   146  		ts[i] = TypeFromYDB(el)
   147  	}
   148  
   149  	return ts
   150  }
   151  
   152  func Equal(a, b Type) bool {
   153  	return a.equalsTo(b)
   154  }
   155  
   156  type Decimal struct {
   157  	precision uint32
   158  	scale     uint32
   159  }
   160  
   161  func (v *Decimal) Precision() uint32 {
   162  	return v.precision
   163  }
   164  
   165  func (v *Decimal) Scale() uint32 {
   166  	return v.scale
   167  }
   168  
   169  func (v *Decimal) String() string {
   170  	return v.Yql()
   171  }
   172  
   173  func (v *Decimal) Name() string {
   174  	return "Decimal"
   175  }
   176  
   177  func (v *Decimal) Yql() string {
   178  	return fmt.Sprintf("%s(%d,%d)", v.Name(), v.precision, v.scale)
   179  }
   180  
   181  func (v *Decimal) equalsTo(rhs Type) bool {
   182  	vv, ok := rhs.(*Decimal)
   183  
   184  	return ok && *v == *vv
   185  }
   186  
   187  func (v *Decimal) ToYDB(a *allocator.Allocator) *Ydb.Type {
   188  	decimal := a.Decimal()
   189  
   190  	decimal.Scale = v.scale
   191  	decimal.Precision = v.precision
   192  
   193  	typeDecimal := a.TypeDecimal()
   194  	typeDecimal.DecimalType = decimal
   195  
   196  	t := a.Type()
   197  	t.Type = typeDecimal
   198  
   199  	return t
   200  }
   201  
   202  func NewDecimal(precision, scale uint32) *Decimal {
   203  	return &Decimal{
   204  		precision: precision,
   205  		scale:     scale,
   206  	}
   207  }
   208  
   209  type Dict struct {
   210  	keyType   Type
   211  	valueType Type
   212  }
   213  
   214  func (v *Dict) KeyType() Type {
   215  	return v.keyType
   216  }
   217  
   218  func (v *Dict) ValueType() Type {
   219  	return v.valueType
   220  }
   221  
   222  func (v *Dict) String() string {
   223  	return v.Yql()
   224  }
   225  
   226  func (v *Dict) Yql() string {
   227  	buffer := xstring.Buffer()
   228  	defer buffer.Free()
   229  	buffer.WriteString("Dict<")
   230  	buffer.WriteString(v.keyType.Yql())
   231  	buffer.WriteByte(',')
   232  	buffer.WriteString(v.valueType.Yql())
   233  	buffer.WriteByte('>')
   234  
   235  	return buffer.String()
   236  }
   237  
   238  func (v *Dict) equalsTo(rhs Type) bool {
   239  	vv, ok := rhs.(*Dict)
   240  	if !ok {
   241  		return false
   242  	}
   243  	if !v.keyType.equalsTo(vv.keyType) {
   244  		return false
   245  	}
   246  	if !v.valueType.equalsTo(vv.valueType) {
   247  		return false
   248  	}
   249  
   250  	return true
   251  }
   252  
   253  func (v *Dict) ToYDB(a *allocator.Allocator) *Ydb.Type {
   254  	t := a.Type()
   255  
   256  	typeDict := a.TypeDict()
   257  
   258  	typeDict.DictType = a.Dict()
   259  
   260  	typeDict.DictType.Key = v.keyType.ToYDB(a)
   261  	typeDict.DictType.Payload = v.valueType.ToYDB(a)
   262  
   263  	t.Type = typeDict
   264  
   265  	return t
   266  }
   267  
   268  func NewDict(key, value Type) (v *Dict) {
   269  	return &Dict{
   270  		keyType:   key,
   271  		valueType: value,
   272  	}
   273  }
   274  
   275  type EmptyList struct{}
   276  
   277  func (v EmptyList) Yql() string {
   278  	return "EmptyList"
   279  }
   280  
   281  func (v EmptyList) String() string {
   282  	return v.Yql()
   283  }
   284  
   285  func (EmptyList) equalsTo(rhs Type) bool {
   286  	_, ok := rhs.(EmptyList)
   287  
   288  	return ok
   289  }
   290  
   291  func (EmptyList) ToYDB(a *allocator.Allocator) *Ydb.Type {
   292  	t := a.Type()
   293  
   294  	t.Type = a.TypeEmptyList()
   295  
   296  	return t
   297  }
   298  
   299  func NewEmptyList() EmptyList {
   300  	return EmptyList{}
   301  }
   302  
   303  type EmptyDict struct{}
   304  
   305  func (v EmptyDict) String() string {
   306  	return v.Yql()
   307  }
   308  
   309  func (v EmptyDict) Yql() string {
   310  	return "EmptyDict"
   311  }
   312  
   313  func (EmptyDict) equalsTo(rhs Type) bool {
   314  	_, ok := rhs.(EmptyDict)
   315  
   316  	return ok
   317  }
   318  
   319  func (EmptyDict) ToYDB(a *allocator.Allocator) *Ydb.Type {
   320  	t := a.Type()
   321  
   322  	t.Type = a.TypeEmptyDict()
   323  
   324  	return t
   325  }
   326  
   327  func EmptySet() EmptyDict {
   328  	return EmptyDict{}
   329  }
   330  
   331  func NewEmptyDict() EmptyDict {
   332  	return EmptyDict{}
   333  }
   334  
   335  type List struct {
   336  	itemType Type
   337  }
   338  
   339  func (v *List) ItemType() Type {
   340  	return v.itemType
   341  }
   342  
   343  func (v *List) String() string {
   344  	return v.Yql()
   345  }
   346  
   347  func (v *List) Yql() string {
   348  	return "List<" + v.itemType.Yql() + ">"
   349  }
   350  
   351  func (v *List) equalsTo(rhs Type) bool {
   352  	vv, ok := rhs.(*List)
   353  	if !ok {
   354  		return false
   355  	}
   356  
   357  	return v.itemType.equalsTo(vv.itemType)
   358  }
   359  
   360  func (v *List) ToYDB(a *allocator.Allocator) *Ydb.Type {
   361  	t := a.Type()
   362  
   363  	list := a.List()
   364  
   365  	list.Item = v.itemType.ToYDB(a)
   366  
   367  	typeList := a.TypeList()
   368  	typeList.ListType = list
   369  
   370  	t.Type = typeList
   371  
   372  	return t
   373  }
   374  
   375  func NewList(t Type) *List {
   376  	return &List{
   377  		itemType: t,
   378  	}
   379  }
   380  
   381  type Set struct {
   382  	itemType Type
   383  }
   384  
   385  func (v *Set) ItemType() Type {
   386  	return v.itemType
   387  }
   388  
   389  func (v *Set) String() string {
   390  	return v.Yql()
   391  }
   392  
   393  func (v *Set) Yql() string {
   394  	return "Set<" + v.itemType.Yql() + ">"
   395  }
   396  
   397  func (v *Set) equalsTo(rhs Type) bool {
   398  	vv, ok := rhs.(*Set)
   399  	if !ok {
   400  		return false
   401  	}
   402  
   403  	return v.itemType.equalsTo(vv.itemType)
   404  }
   405  
   406  func (v *Set) ToYDB(a *allocator.Allocator) *Ydb.Type {
   407  	t := a.Type()
   408  
   409  	typeDict := a.TypeDict()
   410  
   411  	typeDict.DictType = a.Dict()
   412  
   413  	typeDict.DictType.Key = v.itemType.ToYDB(a)
   414  	typeDict.DictType.Payload = _voidType
   415  
   416  	t.Type = typeDict
   417  
   418  	return t
   419  }
   420  
   421  func NewSet(t Type) *Set {
   422  	return &Set{
   423  		itemType: t,
   424  	}
   425  }
   426  
   427  type Optional struct {
   428  	innerType Type
   429  }
   430  
   431  func (v Optional) IsOptional() {}
   432  
   433  func (v Optional) InnerType() Type {
   434  	return v.innerType
   435  }
   436  
   437  func (v Optional) String() string {
   438  	return v.Yql()
   439  }
   440  
   441  func (v Optional) Yql() string {
   442  	return "Optional<" + v.innerType.Yql() + ">"
   443  }
   444  
   445  func (v Optional) equalsTo(rhs Type) bool {
   446  	vv, ok := rhs.(Optional)
   447  	if !ok {
   448  		return false
   449  	}
   450  
   451  	return v.innerType.equalsTo(vv.innerType)
   452  }
   453  
   454  func (v Optional) ToYDB(a *allocator.Allocator) *Ydb.Type {
   455  	t := a.Type()
   456  
   457  	typeOptional := a.TypeOptional()
   458  
   459  	typeOptional.OptionalType = a.Optional()
   460  
   461  	typeOptional.OptionalType.Item = v.innerType.ToYDB(a)
   462  
   463  	t.Type = typeOptional
   464  
   465  	return t
   466  }
   467  
   468  func NewOptional(t Type) Optional {
   469  	return Optional{
   470  		innerType: t,
   471  	}
   472  }
   473  
   474  type PgType struct {
   475  	OID uint32
   476  }
   477  
   478  func (v PgType) String() string {
   479  	return v.Yql()
   480  }
   481  
   482  func (v PgType) Yql() string {
   483  	return fmt.Sprintf("PgType(%v)", v.OID)
   484  }
   485  
   486  func (v PgType) ToYDB(a *allocator.Allocator) *Ydb.Type {
   487  	//nolint:godox
   488  	// TODO: make allocator
   489  	return &Ydb.Type{Type: &Ydb.Type_PgType{
   490  		PgType: &Ydb.PgType{
   491  			Oid: v.OID,
   492  		},
   493  	}}
   494  }
   495  
   496  func (v PgType) equalsTo(rhs Type) bool {
   497  	vv, ok := rhs.(PgType)
   498  	if !ok {
   499  		return false
   500  	}
   501  
   502  	return v.OID == vv.OID
   503  }
   504  
   505  type Primitive uint
   506  
   507  func (v Primitive) String() string {
   508  	return v.Yql()
   509  }
   510  
   511  func (v Primitive) Yql() string {
   512  	return primitiveString[v]
   513  }
   514  
   515  const (
   516  	Unknown Primitive = iota
   517  	Bool
   518  	Int8
   519  	Uint8
   520  	Int16
   521  	Uint16
   522  	Int32
   523  	Uint32
   524  	Int64
   525  	Uint64
   526  	Float
   527  	Double
   528  	Date
   529  	Datetime
   530  	Timestamp
   531  	Interval
   532  	TzDate
   533  	TzDatetime
   534  	TzTimestamp
   535  	Bytes
   536  	Text
   537  	YSON
   538  	JSON
   539  	UUID
   540  	JSONDocument
   541  	DyNumber
   542  )
   543  
   544  var primitive = [...]*Ydb.Type{
   545  	Bool:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_BOOL}},
   546  	Int8:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INT8}},
   547  	Uint8:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UINT8}},
   548  	Int16:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INT16}},
   549  	Uint16:       {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UINT16}},
   550  	Int32:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INT32}},
   551  	Uint32:       {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UINT32}},
   552  	Int64:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INT64}},
   553  	Uint64:       {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UINT64}},
   554  	Float:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_FLOAT}},
   555  	Double:       {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DOUBLE}},
   556  	Date:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DATE}},
   557  	Datetime:     {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DATETIME}},
   558  	Timestamp:    {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_TIMESTAMP}},
   559  	Interval:     {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INTERVAL}},
   560  	TzDate:       {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_TZ_DATE}},
   561  	TzDatetime:   {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_TZ_DATETIME}},
   562  	TzTimestamp:  {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_TZ_TIMESTAMP}},
   563  	Bytes:        {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_STRING}},
   564  	Text:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UTF8}},
   565  	YSON:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_YSON}},
   566  	JSON:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_JSON}},
   567  	UUID:         {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID}},
   568  	JSONDocument: {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_JSON_DOCUMENT}},
   569  	DyNumber:     {Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DYNUMBER}},
   570  }
   571  
   572  var primitiveString = [...]string{
   573  	Unknown:      "<unknown>",
   574  	Bool:         "Bool",
   575  	Int8:         "Int8",
   576  	Uint8:        "Uint8",
   577  	Int16:        "Int16",
   578  	Uint16:       "Uint16",
   579  	Int32:        "Int32",
   580  	Uint32:       "Uint32",
   581  	Int64:        "Int64",
   582  	Uint64:       "Uint64",
   583  	Float:        "Float",
   584  	Double:       "Double",
   585  	Date:         "Date",
   586  	Datetime:     "Datetime",
   587  	Timestamp:    "Timestamp",
   588  	Interval:     "Interval",
   589  	TzDate:       "TzDate",
   590  	TzDatetime:   "TzDatetime",
   591  	TzTimestamp:  "TzTimestamp",
   592  	Bytes:        "String",
   593  	Text:         "Utf8",
   594  	YSON:         "Yson",
   595  	JSON:         "Json",
   596  	UUID:         "Uuid",
   597  	JSONDocument: "JsonDocument",
   598  	DyNumber:     "DyNumber",
   599  }
   600  
   601  func (v Primitive) equalsTo(rhs Type) bool {
   602  	vv, ok := rhs.(Primitive)
   603  	if !ok {
   604  		return false
   605  	}
   606  
   607  	return v == vv
   608  }
   609  
   610  func (v Primitive) ToYDB(*allocator.Allocator) *Ydb.Type {
   611  	return primitive[v]
   612  }
   613  
   614  type (
   615  	StructField struct {
   616  		Name string
   617  		T    Type
   618  	}
   619  	Struct struct {
   620  		fields []StructField
   621  	}
   622  )
   623  
   624  func (v *Struct) Field(i int) StructField {
   625  	return v.fields[i]
   626  }
   627  
   628  func (v *Struct) Fields() []StructField {
   629  	return v.fields
   630  }
   631  
   632  func (v *Struct) String() string {
   633  	return v.Yql()
   634  }
   635  
   636  func (v *Struct) Yql() string {
   637  	buffer := xstring.Buffer()
   638  	defer buffer.Free()
   639  	buffer.WriteString("Struct<")
   640  	for i := range v.fields {
   641  		if i > 0 {
   642  			buffer.WriteByte(',')
   643  		}
   644  		buffer.WriteString("'" + v.fields[i].Name + "'")
   645  		buffer.WriteByte(':')
   646  		buffer.WriteString(v.fields[i].T.Yql())
   647  	}
   648  	buffer.WriteByte('>')
   649  
   650  	return buffer.String()
   651  }
   652  
   653  func (v *Struct) equalsTo(rhs Type) bool {
   654  	vv, ok := rhs.(*Struct)
   655  	if !ok {
   656  		return false
   657  	}
   658  	if len(v.fields) != len(vv.fields) {
   659  		return false
   660  	}
   661  	for i := range v.fields {
   662  		if v.fields[i].Name != vv.fields[i].Name {
   663  			return false
   664  		}
   665  		if !v.fields[i].T.equalsTo(vv.fields[i].T) {
   666  			return false
   667  		}
   668  	}
   669  
   670  	return true
   671  }
   672  
   673  func (v *Struct) ToYDB(a *allocator.Allocator) *Ydb.Type {
   674  	t := a.Type()
   675  
   676  	typeStruct := a.TypeStruct()
   677  
   678  	typeStruct.StructType = a.Struct()
   679  
   680  	for i := range v.fields {
   681  		structMember := a.StructMember()
   682  		structMember.Name = v.fields[i].Name
   683  		structMember.Type = v.fields[i].T.ToYDB(a)
   684  		typeStruct.StructType.Members = append(
   685  			typeStruct.StructType.GetMembers(),
   686  			structMember,
   687  		)
   688  	}
   689  
   690  	t.Type = typeStruct
   691  
   692  	return t
   693  }
   694  
   695  func NewStruct(fields ...StructField) (v *Struct) {
   696  	return &Struct{
   697  		fields: fields,
   698  	}
   699  }
   700  
   701  func StructFields(ms []*Ydb.StructMember) []StructField {
   702  	fs := make([]StructField, len(ms))
   703  	for i, m := range ms {
   704  		fs[i] = StructField{
   705  			Name: m.GetName(),
   706  			T:    TypeFromYDB(m.GetType()),
   707  		}
   708  	}
   709  
   710  	return fs
   711  }
   712  
   713  type Tuple struct {
   714  	innerTypes []Type
   715  }
   716  
   717  func (v *Tuple) InnerTypes() []Type {
   718  	return v.innerTypes
   719  }
   720  
   721  func (v *Tuple) ItemType(i int) Type {
   722  	return v.innerTypes[i]
   723  }
   724  
   725  func (v *Tuple) String() string {
   726  	return v.Yql()
   727  }
   728  
   729  func (v *Tuple) Yql() string {
   730  	buffer := xstring.Buffer()
   731  	defer buffer.Free()
   732  	buffer.WriteString("Tuple<")
   733  	for i, t := range v.innerTypes {
   734  		if i > 0 {
   735  			buffer.WriteByte(',')
   736  		}
   737  		buffer.WriteString(t.Yql())
   738  	}
   739  	buffer.WriteByte('>')
   740  
   741  	return buffer.String()
   742  }
   743  
   744  func (v *Tuple) equalsTo(rhs Type) bool {
   745  	vv, ok := rhs.(*Tuple)
   746  	if !ok {
   747  		return false
   748  	}
   749  	if len(v.innerTypes) != len(vv.innerTypes) {
   750  		return false
   751  	}
   752  	for i := range v.innerTypes {
   753  		if !v.innerTypes[i].equalsTo(vv.innerTypes[i]) {
   754  			return false
   755  		}
   756  	}
   757  
   758  	return true
   759  }
   760  
   761  func (v *Tuple) ToYDB(a *allocator.Allocator) *Ydb.Type {
   762  	var items []Type
   763  	if v != nil {
   764  		items = v.innerTypes
   765  	}
   766  	t := a.Type()
   767  
   768  	typeTuple := a.TypeTuple()
   769  
   770  	typeTuple.TupleType = a.Tuple()
   771  
   772  	for _, vv := range items {
   773  		typeTuple.TupleType.Elements = append(typeTuple.TupleType.GetElements(), vv.ToYDB(a))
   774  	}
   775  
   776  	t.Type = typeTuple
   777  
   778  	return t
   779  }
   780  
   781  func NewTuple(items ...Type) (v *Tuple) {
   782  	return &Tuple{
   783  		innerTypes: items,
   784  	}
   785  }
   786  
   787  type VariantStruct struct {
   788  	*Struct
   789  }
   790  
   791  func (v *VariantStruct) Yql() string {
   792  	buffer := xstring.Buffer()
   793  	defer buffer.Free()
   794  	buffer.WriteString("Variant<")
   795  	for i := range v.fields {
   796  		if i > 0 {
   797  			buffer.WriteByte(',')
   798  		}
   799  		buffer.WriteString("'" + v.fields[i].Name + "'")
   800  		buffer.WriteByte(':')
   801  		buffer.WriteString(v.fields[i].T.Yql())
   802  	}
   803  	buffer.WriteByte('>')
   804  
   805  	return buffer.String()
   806  }
   807  
   808  func (v *VariantStruct) equalsTo(rhs Type) bool {
   809  	switch t := rhs.(type) {
   810  	case *VariantStruct:
   811  		return v.Struct.equalsTo(t.Struct)
   812  	case *Struct:
   813  		return v.Struct.equalsTo(t)
   814  	default:
   815  		return false
   816  	}
   817  }
   818  
   819  func (v *VariantStruct) ToYDB(a *allocator.Allocator) *Ydb.Type {
   820  	t := a.Type()
   821  
   822  	typeVariant := a.TypeVariant()
   823  
   824  	typeVariant.VariantType = a.Variant()
   825  
   826  	structItems := a.VariantStructItems()
   827  
   828  	val, ok := v.Struct.ToYDB(a).GetType().(*Ydb.Type_StructType)
   829  	if !ok {
   830  		panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb.Type_StructType", val))
   831  	}
   832  	structItems.StructItems = val.StructType
   833  
   834  	typeVariant.VariantType.Type = structItems
   835  
   836  	t.Type = typeVariant
   837  
   838  	return t
   839  }
   840  
   841  func NewVariantStruct(fields ...StructField) *VariantStruct {
   842  	return &VariantStruct{
   843  		Struct: NewStruct(fields...),
   844  	}
   845  }
   846  
   847  type VariantTuple struct {
   848  	*Tuple
   849  }
   850  
   851  func (v *VariantTuple) Yql() string {
   852  	buffer := xstring.Buffer()
   853  	defer buffer.Free()
   854  	buffer.WriteString("Variant<")
   855  	for i, t := range v.innerTypes {
   856  		if i > 0 {
   857  			buffer.WriteByte(',')
   858  		}
   859  		buffer.WriteString(t.Yql())
   860  	}
   861  	buffer.WriteByte('>')
   862  
   863  	return buffer.String()
   864  }
   865  
   866  func (v *VariantTuple) equalsTo(rhs Type) bool {
   867  	switch t := rhs.(type) {
   868  	case *VariantTuple:
   869  		return v.Tuple.equalsTo(t.Tuple)
   870  	case *Tuple:
   871  		return v.Tuple.equalsTo(t)
   872  	default:
   873  		return false
   874  	}
   875  }
   876  
   877  func (v *VariantTuple) ToYDB(a *allocator.Allocator) *Ydb.Type {
   878  	t := a.Type()
   879  
   880  	typeVariant := a.TypeVariant()
   881  
   882  	typeVariant.VariantType = a.Variant()
   883  
   884  	tupleItems := a.VariantTupleItems()
   885  
   886  	val, ok := v.Tuple.ToYDB(a).GetType().(*Ydb.Type_TupleType)
   887  	if !ok {
   888  		panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb.Type_TupleType", val))
   889  	}
   890  	tupleItems.TupleItems = val.TupleType
   891  
   892  	typeVariant.VariantType.Type = tupleItems
   893  
   894  	t.Type = typeVariant
   895  
   896  	return t
   897  }
   898  
   899  func NewVariantTuple(items ...Type) *VariantTuple {
   900  	return &VariantTuple{
   901  		Tuple: NewTuple(items...),
   902  	}
   903  }
   904  
   905  type Void struct{}
   906  
   907  func (v Void) String() string {
   908  	return v.Yql()
   909  }
   910  
   911  func (v Void) Yql() string {
   912  	return "Void"
   913  }
   914  
   915  var _voidType = &Ydb.Type{
   916  	Type: &Ydb.Type_VoidType{},
   917  }
   918  
   919  func (v Void) equalsTo(rhs Type) bool {
   920  	_, ok := rhs.(Void)
   921  
   922  	return ok
   923  }
   924  
   925  func (Void) ToYDB(*allocator.Allocator) *Ydb.Type {
   926  	return _voidType
   927  }
   928  
   929  func NewVoid() Void {
   930  	return Void{}
   931  }
   932  
   933  type Null struct{}
   934  
   935  func (v Null) String() string {
   936  	return v.Yql()
   937  }
   938  
   939  func (v Null) Yql() string {
   940  	return "Null"
   941  }
   942  
   943  var _nullType = &Ydb.Type{
   944  	Type: &Ydb.Type_NullType{},
   945  }
   946  
   947  func (v Null) equalsTo(rhs Type) bool {
   948  	_, ok := rhs.(Null)
   949  
   950  	return ok
   951  }
   952  
   953  func (Null) ToYDB(*allocator.Allocator) *Ydb.Type {
   954  	return _nullType
   955  }
   956  
   957  func NewNull() Null {
   958  	return Null{}
   959  }