Skip to content

Commit

Permalink
draft list of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Mar 15, 2024
1 parent 25b9a06 commit 9d25b13
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 59 deletions.
12 changes: 12 additions & 0 deletions internal/params/builder.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package params

import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
)

type (
Builder struct {
params Parameters
}
)

func (b Builder) append(name string, value value.Value) {
b.params = append(b.params, &Parameter{
parent: b,
name: name,
value: value,
})
}

func (b Builder) Build() *Parameters {
return &b.params
}
Expand Down
130 changes: 73 additions & 57 deletions internal/params/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,166 +7,182 @@ import (
)

type (
list struct {
parent Builder
listParent interface {
append(name string, value value.Value)
}
list[T listParent] struct {

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.21.x, 24.1)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.21.x, 23.3)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.22.x, 23.3)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.22.x, 24.1)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, ubuntu)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, ubuntu)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, macOS)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, macOS)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, windows)

instantiation cycle:

Check failure on line 13 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, windows)

instantiation cycle:
parent T
name string
values []value.Value
}
listItem struct {
parent *list
listItemParent interface {
appendItem(value value.Value)
}
listItem[T listItemParent] struct {
parent T
}
)

func (l *list) Add() *listItem {
return &listItem{
func (l *list[T]) appendItem(value value.Value) {
l.values = append(l.values, value)
}

func (l *list[T]) append(name string, value value.Value) {
l.parent.append(name, value)
}

func (l *list[T]) Add() *listItem[*list[T]] {
return &listItem[*list[T]]{
parent: l,
}
}

func (l *list) AddItems(items ...value.Value) *list {
func (l *list[T]) AddItems(items ...value.Value) *list[T] {
l.values = append(l.values, items...)

return l
}

func (l *list) EndList() Builder {
l.parent.params = append(l.parent.params, &Parameter{
parent: l.parent,
name: l.name,
value: value.ListValue(l.values...),
})
func (l *list[T]) BeginList() *list[*list[T]] {
return &list[*list[T]]{

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.21.x, 24.1)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.21.x, 23.3)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.22.x, 23.3)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / integration (1.22.x, 24.1)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, ubuntu)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, ubuntu)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, macOS)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, macOS)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.22.x, windows)

T instantiated as *list[T]

Check failure on line 47 in internal/params/list.go

View workflow job for this annotation

GitHub Actions / unit (1.21.x, windows)

T instantiated as *list[T]
parent: l,
}
}

func (l *list[T]) EndList() T {
l.parent.append(l.name, value.ListValue(l.values...))

return l.parent
}

func (l *listItem) Text(v string) *list {
l.parent.values = append(l.parent.values, value.TextValue(v))
func (l listItem[T]) Text(v string) T {
l.parent.appendItem(value.TextValue(v))

return l.parent
}

func (l *listItem) Bytes(v []byte) *list {
l.parent.values = append(l.parent.values, value.BytesValue(v))
func (l listItem[T]) Bytes(v []byte) T {
l.parent.appendItem(value.BytesValue(v))

return l.parent
}

func (l *listItem) Bool(v bool) *list {
l.parent.values = append(l.parent.values, value.BoolValue(v))
func (l listItem[T]) Bool(v bool) T {
l.parent.appendItem(value.BoolValue(v))

return l.parent
}

func (l *listItem) Uint64(v uint64) *list {
l.parent.values = append(l.parent.values, value.Uint64Value(v))
func (l listItem[T]) Uint64(v uint64) T {
l.parent.appendItem(value.Uint64Value(v))

return l.parent
}

func (l *listItem) Int64(v int64) *list {
l.parent.values = append(l.parent.values, value.Int64Value(v))
func (l listItem[T]) Int64(v int64) T {
l.parent.appendItem(value.Int64Value(v))

return l.parent
}

func (l *listItem) Uint32(v uint32) *list {
l.parent.values = append(l.parent.values, value.Uint32Value(v))
func (l listItem[T]) Uint32(v uint32) T {
l.parent.appendItem(value.Uint32Value(v))

return l.parent
}

func (l *listItem) Int32(v int32) *list {
l.parent.values = append(l.parent.values, value.Int32Value(v))
func (l listItem[T]) Int32(v int32) T {
l.parent.appendItem(value.Int32Value(v))

return l.parent
}

func (l *listItem) Uint16(v uint16) *list {
l.parent.values = append(l.parent.values, value.Uint16Value(v))
func (l listItem[T]) Uint16(v uint16) T {
l.parent.appendItem(value.Uint16Value(v))

return l.parent
}

func (l *listItem) Int16(v int16) *list {
l.parent.values = append(l.parent.values, value.Int16Value(v))
func (l listItem[T]) Int16(v int16) T {
l.parent.appendItem(value.Int16Value(v))

return l.parent
}

func (l *listItem) Uint8(v uint8) *list {
l.parent.values = append(l.parent.values, value.Uint8Value(v))
func (l listItem[T]) Uint8(v uint8) T {
l.parent.appendItem(value.Uint8Value(v))

return l.parent
}

func (l *listItem) Int8(v int8) *list {
l.parent.values = append(l.parent.values, value.Int8Value(v))
func (l listItem[T]) Int8(v int8) T {
l.parent.appendItem(value.Int8Value(v))

return l.parent
}

func (l *listItem) Float(v float32) *list {
l.parent.values = append(l.parent.values, value.FloatValue(v))
func (l listItem[T]) Float(v float32) T {
l.parent.appendItem(value.FloatValue(v))

return l.parent
}

func (l *listItem) Double(v float64) *list {
l.parent.values = append(l.parent.values, value.DoubleValue(v))
func (l listItem[T]) Double(v float64) T {
l.parent.appendItem(value.DoubleValue(v))

return l.parent
}

func (l *listItem) Decimal(v [16]byte, precision, scale uint32) *list {
l.parent.values = append(l.parent.values, value.DecimalValue(v, precision, scale))
func (l listItem[T]) Decimal(v [16]byte, precision, scale uint32) T {
l.parent.appendItem(value.DecimalValue(v, precision, scale))

return l.parent
}

func (l *listItem) Timestamp(v time.Time) *list {
l.parent.values = append(l.parent.values, value.TimestampValueFromTime(v))
func (l listItem[T]) Timestamp(v time.Time) T {
l.parent.appendItem(value.TimestampValueFromTime(v))

return l.parent
}

func (l *listItem) Date(v time.Time) *list {
l.parent.values = append(l.parent.values, value.DateValueFromTime(v))
func (l listItem[T]) Date(v time.Time) T {
l.parent.appendItem(value.DateValueFromTime(v))

return l.parent
}

func (l *listItem) Datetime(v time.Time) *list {
l.parent.values = append(l.parent.values, value.DatetimeValueFromTime(v))
func (l listItem[T]) Datetime(v time.Time) T {
l.parent.appendItem(value.DatetimeValueFromTime(v))

return l.parent
}

func (l *listItem) Interval(v time.Duration) *list {
l.parent.values = append(l.parent.values, value.IntervalValueFromDuration(v))
func (l listItem[T]) Interval(v time.Duration) T {
l.parent.appendItem(value.IntervalValueFromDuration(v))

return l.parent
}

func (l *listItem) JSON(v string) *list {
l.parent.values = append(l.parent.values, value.JSONValue(v))
func (l listItem[T]) JSON(v string) T {
l.parent.appendItem(value.JSONValue(v))

return l.parent
}

func (l *listItem) JSONDocument(v string) *list {
l.parent.values = append(l.parent.values, value.JSONDocumentValue(v))
func (l listItem[T]) JSONDocument(v string) T {
l.parent.appendItem(value.JSONDocumentValue(v))

return l.parent
}

func (l *listItem) YSON(v []byte) *list {
l.parent.values = append(l.parent.values, value.YSONValue(v))
func (l listItem[T]) YSON(v []byte) T {
l.parent.appendItem(value.YSONValue(v))

return l.parent
}

func (l *listItem) UUID(v [16]byte) *list {
l.parent.values = append(l.parent.values, value.UUIDValue(v))
func (l listItem[T]) UUID(v [16]byte) T {
l.parent.appendItem(value.UUIDValue(v))

return l.parent
}
38 changes: 38 additions & 0 deletions internal/params/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ func TestList(t *testing.T) {
},
},
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").BeginList().BeginList().BeginList().Add().Uint64(123).EndList().EndList().EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Type: &Ydb.Type_ListType{
ListType: &Ydb.ListType{
Item: &Ydb.Type{
Type: &Ydb.Type_ListType{
ListType: &Ydb.ListType{
Item: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UINT64,
},
},
},
},
},
},
},
},
Value: &Ydb.Value{
Items: []*Ydb.Value{
{
Items: []*Ydb.Value{
{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 123,
},
},
},
},
},
},
},
},
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").BeginList().Add().Int64(123).EndList(),
Expand Down
4 changes: 2 additions & 2 deletions internal/params/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (p *Parameter) BeginOptional() *optional {
}
}

func (p *Parameter) BeginList() *list {
return &list{
func (p *Parameter) BeginList() *list[Builder] {
return &list[Builder]{
parent: p.parent,
name: p.name,
}
Expand Down

0 comments on commit 9d25b13

Please sign in to comment.