Skip to content

Commit e4db064

Browse files
authored
Merge pull request #1090 from size12/params-builder-types-support
params builder support (json + json document + yson + uuid)
2 parents d1f6e5b + 56613fb commit e4db064

File tree

8 files changed

+540
-0
lines changed

8 files changed

+540
-0
lines changed

internal/params/builder_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,85 @@ func TestBuilder(t *testing.T) {
371371
},
372372
},
373373
},
374+
{
375+
name: xtest.CurrentFileLine(),
376+
builder: Builder{}.
377+
Param("$x").JSON(`{"a": 1,"b": "B"}`).Build(),
378+
params: map[string]*Ydb.TypedValue{
379+
"$x": {
380+
Type: &Ydb.Type{
381+
Type: &Ydb.Type_TypeId{
382+
TypeId: Ydb.Type_JSON,
383+
},
384+
},
385+
Value: &Ydb.Value{
386+
Value: &Ydb.Value_TextValue{
387+
TextValue: `{"a": 1,"b": "B"}`,
388+
},
389+
},
390+
},
391+
},
392+
},
393+
{
394+
name: xtest.CurrentFileLine(),
395+
builder: Builder{}.
396+
Param("$x").JSONDocument(`{"a": 1,"b": "B"}`).Build(),
397+
params: map[string]*Ydb.TypedValue{
398+
"$x": {
399+
Type: &Ydb.Type{
400+
Type: &Ydb.Type_TypeId{
401+
TypeId: Ydb.Type_JSON_DOCUMENT,
402+
},
403+
},
404+
Value: &Ydb.Value{
405+
Value: &Ydb.Value_TextValue{
406+
TextValue: `{"a": 1,"b": "B"}`,
407+
},
408+
},
409+
},
410+
},
411+
},
412+
{
413+
name: xtest.CurrentFileLine(),
414+
builder: Builder{}.
415+
Param("$x").YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
416+
params: map[string]*Ydb.TypedValue{
417+
"$x": {
418+
Type: &Ydb.Type{
419+
Type: &Ydb.Type_TypeId{
420+
TypeId: Ydb.Type_YSON,
421+
},
422+
},
423+
Value: &Ydb.Value{
424+
Value: &Ydb.Value_BytesValue{
425+
BytesValue: []byte(`[ 1; 2; 3; 4; 5 ]`),
426+
},
427+
},
428+
},
429+
},
430+
},
431+
{
432+
name: xtest.CurrentFileLine(),
433+
builder: Builder{}.
434+
Param("$x").
435+
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).
436+
Build(),
437+
params: map[string]*Ydb.TypedValue{
438+
"$x": {
439+
Type: &Ydb.Type{
440+
Type: &Ydb.Type_TypeId{
441+
TypeId: Ydb.Type_UUID,
442+
},
443+
},
444+
Value: &Ydb.Value{
445+
Value: &Ydb.Value_Low_128{
446+
Low_128: 651345242494996240,
447+
},
448+
High_128: 72623859790382856,
449+
},
450+
},
451+
},
452+
},
374453
} {
375454
t.Run(tt.name, func(t *testing.T) {
376455
a := allocator.New()

internal/params/list.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,27 @@ func (l *listItem) Interval(v time.Duration) *list {
140140

141141
return l.parent
142142
}
143+
144+
func (l *listItem) JSON(v string) *list {
145+
l.parent.values = append(l.parent.values, value.JSONValue(v))
146+
147+
return l.parent
148+
}
149+
150+
func (l *listItem) JSONDocument(v string) *list {
151+
l.parent.values = append(l.parent.values, value.JSONDocumentValue(v))
152+
153+
return l.parent
154+
}
155+
156+
func (l *listItem) YSON(v []byte) *list {
157+
l.parent.values = append(l.parent.values, value.YSONValue(v))
158+
159+
return l.parent
160+
}
161+
162+
func (l *listItem) UUID(v [16]byte) *list {
163+
l.parent.values = append(l.parent.values, value.UUIDValue(v))
164+
165+
return l.parent
166+
}

internal/params/list_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,120 @@ func TestList(t *testing.T) {
525525
},
526526
},
527527
},
528+
{
529+
name: xtest.CurrentFileLine(),
530+
builder: Builder{}.Param("$x").List().AddItem().JSON(`{"a": 1,"b": "B"}`).Build(),
531+
params: map[string]*Ydb.TypedValue{
532+
"$x": {
533+
Type: &Ydb.Type{
534+
Type: &Ydb.Type_ListType{
535+
ListType: &Ydb.ListType{
536+
Item: &Ydb.Type{
537+
Type: &Ydb.Type_TypeId{
538+
TypeId: Ydb.Type_JSON,
539+
},
540+
},
541+
},
542+
},
543+
},
544+
Value: &Ydb.Value{
545+
Items: []*Ydb.Value{
546+
{
547+
Value: &Ydb.Value_TextValue{
548+
TextValue: `{"a": 1,"b": "B"}`,
549+
},
550+
},
551+
},
552+
},
553+
},
554+
},
555+
},
556+
{
557+
name: xtest.CurrentFileLine(),
558+
builder: Builder{}.Param("$x").List().AddItem().JSONDocument(`{"a": 1,"b": "B"}`).Build(),
559+
params: map[string]*Ydb.TypedValue{
560+
"$x": {
561+
Type: &Ydb.Type{
562+
Type: &Ydb.Type_ListType{
563+
ListType: &Ydb.ListType{
564+
Item: &Ydb.Type{
565+
Type: &Ydb.Type_TypeId{
566+
TypeId: Ydb.Type_JSON_DOCUMENT,
567+
},
568+
},
569+
},
570+
},
571+
},
572+
Value: &Ydb.Value{
573+
Items: []*Ydb.Value{
574+
{
575+
Value: &Ydb.Value_TextValue{
576+
TextValue: `{"a": 1,"b": "B"}`,
577+
},
578+
},
579+
},
580+
},
581+
},
582+
},
583+
},
584+
{
585+
name: xtest.CurrentFileLine(),
586+
builder: Builder{}.Param("$x").List().AddItem().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
587+
params: map[string]*Ydb.TypedValue{
588+
"$x": {
589+
Type: &Ydb.Type{
590+
Type: &Ydb.Type_ListType{
591+
ListType: &Ydb.ListType{
592+
Item: &Ydb.Type{
593+
Type: &Ydb.Type_TypeId{
594+
TypeId: Ydb.Type_YSON,
595+
},
596+
},
597+
},
598+
},
599+
},
600+
Value: &Ydb.Value{
601+
Items: []*Ydb.Value{
602+
{
603+
Value: &Ydb.Value_BytesValue{
604+
BytesValue: []byte(`[ 1; 2; 3; 4; 5 ]`),
605+
},
606+
},
607+
},
608+
},
609+
},
610+
},
611+
},
612+
{
613+
name: xtest.CurrentFileLine(),
614+
builder: Builder{}.Param("$x").List().AddItem().
615+
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).Build(),
616+
params: map[string]*Ydb.TypedValue{
617+
"$x": {
618+
Type: &Ydb.Type{
619+
Type: &Ydb.Type_ListType{
620+
ListType: &Ydb.ListType{
621+
Item: &Ydb.Type{
622+
Type: &Ydb.Type_TypeId{
623+
TypeId: Ydb.Type_UUID,
624+
},
625+
},
626+
},
627+
},
628+
},
629+
Value: &Ydb.Value{
630+
Items: []*Ydb.Value{
631+
{
632+
Value: &Ydb.Value_Low_128{
633+
Low_128: 651345242494996240,
634+
},
635+
High_128: 72623859790382856,
636+
},
637+
},
638+
},
639+
},
640+
},
641+
},
528642
} {
529643
t.Run(tt.name, func(t *testing.T) {
530644
a := allocator.New()

internal/params/optional.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,27 @@ func (p *optional) Interval(v time.Duration) *optionalBuilder {
134134

135135
return &optionalBuilder{opt: p}
136136
}
137+
138+
func (p *optional) JSON(v string) *optionalBuilder {
139+
p.value = value.JSONValue(v)
140+
141+
return &optionalBuilder{opt: p}
142+
}
143+
144+
func (p *optional) JSONDocument(v string) *optionalBuilder {
145+
p.value = value.JSONDocumentValue(v)
146+
147+
return &optionalBuilder{opt: p}
148+
}
149+
150+
func (p *optional) YSON(v []byte) *optionalBuilder {
151+
p.value = value.YSONValue(v)
152+
153+
return &optionalBuilder{opt: p}
154+
}
155+
156+
func (p *optional) UUID(v [16]byte) *optionalBuilder {
157+
p.value = value.UUIDValue(v)
158+
159+
return &optionalBuilder{opt: p}
160+
}

internal/params/optional_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,106 @@ func TestOptional(t *testing.T) {
453453
},
454454
},
455455
},
456+
{
457+
name: xtest.CurrentFileLine(),
458+
builder: Builder{}.Param("$x").Optional().JSON(`{"a": 1,"b": "B"}`).Build(),
459+
params: map[string]*Ydb.TypedValue{
460+
"$x": {
461+
Type: &Ydb.Type{
462+
Type: &Ydb.Type_OptionalType{
463+
OptionalType: &Ydb.OptionalType{
464+
Item: &Ydb.Type{
465+
Type: &Ydb.Type_TypeId{
466+
TypeId: Ydb.Type_JSON,
467+
},
468+
},
469+
},
470+
},
471+
},
472+
Value: &Ydb.Value{
473+
Value: &Ydb.Value_TextValue{
474+
TextValue: `{"a": 1,"b": "B"}`,
475+
},
476+
},
477+
},
478+
},
479+
},
480+
{
481+
name: xtest.CurrentFileLine(),
482+
builder: Builder{}.Param("$x").Optional().JSONDocument(`{"a": 1,"b": "B"}`).Build(),
483+
params: map[string]*Ydb.TypedValue{
484+
"$x": {
485+
Type: &Ydb.Type{
486+
Type: &Ydb.Type_OptionalType{
487+
OptionalType: &Ydb.OptionalType{
488+
Item: &Ydb.Type{
489+
Type: &Ydb.Type_TypeId{
490+
TypeId: Ydb.Type_JSON_DOCUMENT,
491+
},
492+
},
493+
},
494+
},
495+
},
496+
Value: &Ydb.Value{
497+
Value: &Ydb.Value_TextValue{
498+
TextValue: `{"a": 1,"b": "B"}`,
499+
},
500+
},
501+
},
502+
},
503+
},
504+
{
505+
name: xtest.CurrentFileLine(),
506+
builder: Builder{}.Param("$x").Optional().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
507+
params: map[string]*Ydb.TypedValue{
508+
"$x": {
509+
Type: &Ydb.Type{
510+
Type: &Ydb.Type_OptionalType{
511+
OptionalType: &Ydb.OptionalType{
512+
Item: &Ydb.Type{
513+
Type: &Ydb.Type_TypeId{
514+
TypeId: Ydb.Type_YSON,
515+
},
516+
},
517+
},
518+
},
519+
},
520+
Value: &Ydb.Value{
521+
Value: &Ydb.Value_BytesValue{
522+
BytesValue: []byte(`[ 1; 2; 3; 4; 5 ]`),
523+
},
524+
},
525+
},
526+
},
527+
},
528+
{
529+
name: xtest.CurrentFileLine(),
530+
builder: Builder{}.Param("$x").
531+
Optional().
532+
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).
533+
Build(),
534+
params: map[string]*Ydb.TypedValue{
535+
"$x": {
536+
Type: &Ydb.Type{
537+
Type: &Ydb.Type_OptionalType{
538+
OptionalType: &Ydb.OptionalType{
539+
Item: &Ydb.Type{
540+
Type: &Ydb.Type_TypeId{
541+
TypeId: Ydb.Type_UUID,
542+
},
543+
},
544+
},
545+
},
546+
},
547+
Value: &Ydb.Value{
548+
Value: &Ydb.Value_Low_128{
549+
Low_128: 651345242494996240,
550+
},
551+
High_128: 72623859790382856,
552+
},
553+
},
554+
},
555+
},
456556
} {
457557
t.Run(tt.name, func(t *testing.T) {
458558
a := allocator.New()

0 commit comments

Comments
 (0)