Skip to content

Commit 64d1e1e

Browse files
committed
dict impl
1 parent b518441 commit 64d1e1e

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed

internal/params/dict.go

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
package params
2+
3+
import (
4+
"time"
5+
6+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
7+
)
8+
9+
type (
10+
dict struct {
11+
parent Builder
12+
name string
13+
values []value.DictValueField
14+
}
15+
dictPair struct {
16+
parent *dict
17+
keyValue value.Value
18+
}
19+
dictValue struct {
20+
pair *dictPair
21+
}
22+
)
23+
24+
func (d *dict) Pair() *dictPair {
25+
return &dictPair{
26+
parent: d,
27+
}
28+
}
29+
30+
func (d *dict) AddPairs(pairs ...value.DictValueField) *dict {
31+
d.values = append(d.values, pairs...)
32+
33+
return d
34+
}
35+
36+
func (d *dictPair) Text(v string) *dictValue {
37+
d.keyValue = value.TextValue(v)
38+
39+
return &dictValue{
40+
pair: d,
41+
}
42+
}
43+
44+
func (d *dictPair) Bytes(v []byte) *dictValue {
45+
d.keyValue = value.BytesValue(v)
46+
47+
return &dictValue{
48+
pair: d,
49+
}
50+
}
51+
52+
func (d *dictPair) Bool(v bool) *dictValue {
53+
d.keyValue = value.BoolValue(v)
54+
55+
return &dictValue{
56+
pair: d,
57+
}
58+
}
59+
60+
func (d *dictPair) Uint64(v uint64) *dictValue {
61+
d.keyValue = value.Uint64Value(v)
62+
63+
return &dictValue{
64+
pair: d,
65+
}
66+
}
67+
68+
func (d *dictPair) Int64(v int64) *dictValue {
69+
d.keyValue = value.Int64Value(v)
70+
71+
return &dictValue{
72+
pair: d,
73+
}
74+
}
75+
76+
func (d *dictPair) Uint32(v uint32) *dictValue {
77+
d.keyValue = value.Uint32Value(v)
78+
79+
return &dictValue{
80+
pair: d,
81+
}
82+
}
83+
84+
func (d *dictPair) Int32(v int32) *dictValue {
85+
d.keyValue = value.Int32Value(v)
86+
87+
return &dictValue{
88+
pair: d,
89+
}
90+
}
91+
92+
func (d *dictPair) Uint16(v uint16) *dictValue {
93+
d.keyValue = value.Uint16Value(v)
94+
95+
return &dictValue{
96+
pair: d,
97+
}
98+
}
99+
100+
func (d *dictPair) Int16(v int16) *dictValue {
101+
d.keyValue = value.Int16Value(v)
102+
103+
return &dictValue{
104+
pair: d,
105+
}
106+
}
107+
108+
func (d *dictPair) Uint8(v uint8) *dictValue {
109+
d.keyValue = value.Uint8Value(v)
110+
111+
return &dictValue{
112+
pair: d,
113+
}
114+
}
115+
116+
func (d *dictPair) Int8(v int8) *dictValue {
117+
d.keyValue = value.Int8Value(v)
118+
119+
return &dictValue{
120+
pair: d,
121+
}
122+
}
123+
124+
func (d *dictPair) Float(v float32) *dictValue {
125+
d.keyValue = value.FloatValue(v)
126+
127+
return &dictValue{
128+
pair: d,
129+
}
130+
}
131+
132+
func (d *dictPair) Double(v float64) *dictValue {
133+
d.keyValue = value.DoubleValue(v)
134+
135+
return &dictValue{
136+
pair: d,
137+
}
138+
}
139+
140+
func (d *dictPair) Decimal(v [16]byte, precision, scale uint32) *dictValue {
141+
d.keyValue = value.DecimalValue(v, precision, scale)
142+
143+
return &dictValue{
144+
pair: d,
145+
}
146+
}
147+
148+
func (d *dictPair) Timestamp(v time.Time) *dictValue {
149+
d.keyValue = value.TimestampValueFromTime(v)
150+
151+
return &dictValue{
152+
pair: d,
153+
}
154+
}
155+
156+
func (d *dictPair) Date(v time.Time) *dictValue {
157+
d.keyValue = value.DateValueFromTime(v)
158+
159+
return &dictValue{
160+
pair: d,
161+
}
162+
}
163+
164+
func (d *dictPair) Datetime(v time.Time) *dictValue {
165+
d.keyValue = value.DatetimeValueFromTime(v)
166+
167+
return &dictValue{
168+
pair: d,
169+
}
170+
}
171+
172+
func (d *dictPair) Interval(v time.Duration) *dictValue {
173+
d.keyValue = value.IntervalValueFromDuration(v)
174+
175+
return &dictValue{
176+
pair: d,
177+
}
178+
}
179+
180+
func (d *dictPair) JSON(v string) *dictValue {
181+
d.keyValue = value.JSONValue(v)
182+
183+
return &dictValue{
184+
pair: d,
185+
}
186+
}
187+
188+
func (d *dictPair) JSONDocument(v string) *dictValue {
189+
d.keyValue = value.JSONDocumentValue(v)
190+
191+
return &dictValue{
192+
pair: d,
193+
}
194+
}
195+
196+
func (d *dictPair) YSON(v []byte) *dictValue {
197+
d.keyValue = value.YSONValue(v)
198+
199+
return &dictValue{
200+
pair: d,
201+
}
202+
}
203+
204+
func (d *dictPair) UUID(v [16]byte) *dictValue {
205+
d.keyValue = value.UUIDValue(v)
206+
207+
return &dictValue{
208+
pair: d,
209+
}
210+
}
211+
212+
func (d *dictValue) Uint64(v uint64) *dict {
213+
d.pair.parent.values = append(d.pair.parent.values, value.DictValueField{
214+
K: d.pair.keyValue,
215+
V: value.Uint64Value(v),
216+
})
217+
218+
return d.pair.parent
219+
}
220+
221+
func (d *dict) Build() Builder {
222+
d.parent.params = append(d.parent.params, &Parameter{
223+
parent: d.parent,
224+
name: d.name,
225+
value: value.DictValue(d.values...),
226+
})
227+
228+
return d.parent
229+
}

internal/params/dict_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package params
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
10+
)
11+
12+
func TestDict(t *testing.T) {
13+
for _, tt := range []struct {
14+
name string
15+
builder Builder
16+
params map[string]*Ydb.TypedValue
17+
}{
18+
{
19+
name: xtest.CurrentFileLine(),
20+
builder: Builder{}.Param("$x").Dict().Pair().Int32(2).Uint64(21).Build(),
21+
params: map[string]*Ydb.TypedValue{},
22+
},
23+
} {
24+
t.Run(tt.name, func(t *testing.T) {
25+
a := allocator.New()
26+
defer a.Free()
27+
params := tt.builder.Build().ToYDB(a)
28+
require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
29+
})
30+
}
31+
}

internal/params/parameters.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ func (p *Parameter) Set() *set {
116116
}
117117
}
118118

119+
func (p *Parameter) Dict() *dict {
120+
return &dict{
121+
parent: p.parent,
122+
name: p.name,
123+
}
124+
}
125+
119126
func (p *Parameter) Text(v string) Builder {
120127
p.value = value.TextValue(v)
121128
p.parent.params = append(p.parent.params, p)

0 commit comments

Comments
 (0)