diff --git a/internal/value/builder.go b/internal/value/builder.go new file mode 100644 index 000000000..a63b47c32 --- /dev/null +++ b/internal/value/builder.go @@ -0,0 +1,98 @@ +package value + +type ( + Builder struct{} + builderList struct { + items []Value + } + builderTypedList[T any] struct { + parent *builderList + add func(T) Value + } +) + +func (b Builder) List() *builderList { + return &builderList{} +} + +func (b *builderList) Uint8() *builderTypedList[uint8] { + return &builderTypedList[uint8]{ + parent: b, + add: func(v uint8) Value { + return Uint8Value(v) + }, + } +} + +func (b *builderList) Uint16() *builderTypedList[uint16] { + return &builderTypedList[uint16]{ + parent: b, + add: func(v uint16) Value { + return Uint16Value(v) + }, + } +} + +func (b *builderList) Uint32() *builderTypedList[uint32] { + return &builderTypedList[uint32]{ + parent: b, + add: func(v uint32) Value { + return Uint32Value(v) + }, + } +} + +func (b *builderList) Uint64() *builderTypedList[uint64] { + return &builderTypedList[uint64]{ + parent: b, + add: func(v uint64) Value { + return Uint64Value(v) + }, + } +} + +func (b *builderList) Int8() *builderTypedList[int8] { + return &builderTypedList[int8]{ + parent: b, + add: func(v int8) Value { + return Int8Value(v) + }, + } +} + +func (b *builderList) Int16() *builderTypedList[int16] { + return &builderTypedList[int16]{ + parent: b, + add: func(v int16) Value { + return Int16Value(v) + }, + } +} + +func (b *builderList) Int32() *builderTypedList[int32] { + return &builderTypedList[int32]{ + parent: b, + add: func(v int32) Value { + return Int32Value(v) + }, + } +} + +func (b *builderList) Int64() *builderTypedList[int64] { + return &builderTypedList[int64]{ + parent: b, + add: func(v int64) Value { + return Int64Value(v) + }, + } +} + +func (b *builderTypedList[T]) Add(v T) *builderTypedList[T] { + b.parent.items = append(b.parent.items, b.add(v)) + + return b +} + +func (b *builderTypedList[T]) Build() Value { + return ListValue(b.parent.items...) +} diff --git a/internal/value/builder_test.go b/internal/value/builder_test.go new file mode 100644 index 000000000..c065c93b6 --- /dev/null +++ b/internal/value/builder_test.go @@ -0,0 +1,96 @@ +package value_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/ydb-platform/ydb-go-sdk/v3/internal/value" +) + +func TestBuilder(t *testing.T) { + t.Run("List", func(t *testing.T) { + for _, tt := range []struct { + name string + act value.Value + exp value.Value + }{ + { + name: "Uint8", + act: value.Builder{}.List().Uint8().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Uint8Value(1), + value.Uint8Value(2), + value.Uint8Value(3), + ), + }, + { + name: "Uint16", + act: value.Builder{}.List().Uint16().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Uint16Value(1), + value.Uint16Value(2), + value.Uint16Value(3), + ), + }, + { + name: "Uint32", + act: value.Builder{}.List().Uint32().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Uint32Value(1), + value.Uint32Value(2), + value.Uint32Value(3), + ), + }, + { + name: "Uint64", + act: value.Builder{}.List().Uint64().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Uint64Value(1), + value.Uint64Value(2), + value.Uint64Value(3), + ), + }, + { + name: "Int8", + act: value.Builder{}.List().Int8().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Int8Value(1), + value.Int8Value(2), + value.Int8Value(3), + ), + }, + { + name: "Int16", + act: value.Builder{}.List().Int16().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Int16Value(1), + value.Int16Value(2), + value.Int16Value(3), + ), + }, + { + name: "Int32", + act: value.Builder{}.List().Int32().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Int32Value(1), + value.Int32Value(2), + value.Int32Value(3), + ), + }, + { + name: "Int64", + act: value.Builder{}.List().Int64().Add(1).Add(2).Add(3).Build(), + exp: value.ListValue( + value.Int64Value(1), + value.Int64Value(2), + value.Int64Value(3), + ), + }, + } { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.exp, tt.act) + }) + } + }) +}