Skip to content

Commit b95831e

Browse files
committed
json query builder
1 parent d1f6e5b commit b95831e

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

internal/params/builder_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,25 @@ 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+
},
374393
} {
375394
t.Run(tt.name, func(t *testing.T) {
376395
a := allocator.New()

internal/params/optional.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ 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+
}

internal/params/optional_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,30 @@ 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+
},
456480
} {
457481
t.Run(tt.name, func(t *testing.T) {
458482
a := allocator.New()

internal/params/parameters.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ func (p *Parameter) Interval(v time.Duration) Builder {
242242
return p.parent
243243
}
244244

245+
func (p *Parameter) Json(v string) Builder {
246+
p.value = value.JSONValue(v)
247+
p.parent.params = append(p.parent.params, p)
248+
249+
return p.parent
250+
}
251+
245252
func Declare(p *Parameter) string {
246253
return fmt.Sprintf(
247254
"DECLARE %s AS %s",

0 commit comments

Comments
 (0)