Skip to content

Commit

Permalink
added predicate testcase, updated velty version
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Jul 19, 2023
1 parent fb44c42 commit 95cc428
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 92 deletions.
7 changes: 4 additions & 3 deletions cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/viant/toolbox/format"
"github.com/viant/velty/ast/expr"
"github.com/viant/velty/parser"
"github.com/viant/xdatly/predicate"
"github.com/viant/xreflect"
"go/ast"
goFormat "go/format"
Expand Down Expand Up @@ -2060,9 +2061,9 @@ func (s *Builder) readParamConfigs(cfg *option.ParameterConfig, cursor *parsly.C
return err
}

var namedArgs []*config.NamedArg
var namedArgs []*predicate.NamedArgument
for pos, argName := range args[2:] {
namedArgs = append(namedArgs, &config.NamedArg{
namedArgs = append(namedArgs, &predicate.NamedArgument{
Position: pos,
Name: argName,
})
Expand Down Expand Up @@ -2295,7 +2296,7 @@ func (s *Builder) extractArgs(content string) []string {
result = append(result, strings.TrimSpace(arg))
default:
if cursor.Pos < len(cursor.Input) {
arg := strings.TrimSpace(string(cursor.Input[cursor.Pos:]))
arg := strings.Trim(strings.TrimSpace(string(cursor.Input[cursor.Pos:])), `"'`)
if len(arg) != 0 {
result = append(result, arg)
}
Expand Down
13 changes: 11 additions & 2 deletions config/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type (
Parent string
Name string
Context int
Args []*NamedArg
Args []*predicate.NamedArgument
}

NamedArg struct {
Expand All @@ -32,6 +32,15 @@ func (r PredicateRegistry) Lookup(name string) (*predicate.Template, error) {
return result, nil
}

func (r PredicateRegistry) Clone() PredicateRegistry {
result := PredicateRegistry{}
for key, template := range r {
result[key] = template
}

return result
}

func NewEqualPredicate() *predicate.Template {
return equalityCheckPredicate(PredicateEqual, true)
}
Expand All @@ -44,7 +53,7 @@ func equalityCheckPredicate(name string, equal bool) *predicate.Template {

return &predicate.Template{
Name: name,
Source: " $Alias.$ColumnName " + negation + "= $criteria.AppendBinding($FilterValue)",
Source: " ${Alias}.${ColumnName} " + negation + "= $criteria.AppendBinding($FilterValue)",
Args: []*predicate.NamedArgument{
{
Name: "Alias",
Expand Down
28 changes: 1 addition & 27 deletions e2e/local/regression/cases/062_predicates/expect.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
[
{
"@indexBy@": "id"
},
{
"id": 1,
"name": "Vendor 1",
"products": [
{
"@indexBy@": "id"
},
{
"id": 1,
"name": "V1 Product 1",
"userCreated": 1
},
{
"id": 2,
"name": "V1 Product 2",
"userCreated": 1
}
]
},
{
"id": 2,
"name": "Vendor 2"
},
{
"id": 3,
"name": "Vendor 3"
"name": "Vendor 1"
}
]
1 change: 1 addition & 0 deletions e2e/local/regression/cases/062_predicates/expect2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
7 changes: 7 additions & 0 deletions e2e/local/regression/cases/062_predicates/expect3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"id": 2,
"name": "Vendor 2",
"userUpdated": 0
}
]
17 changes: 17 additions & 0 deletions e2e/local/regression/cases/062_predicates/expect4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": 1,
"name": "Vendor 1",
"accountId": 100
},
{
"id": 2,
"name": "Vendor 2",
"accountId": 101
},
{
"id": 3,
"name": "Vendor 3",
"accountId": 100
}
]
1 change: 0 additions & 1 deletion e2e/local/regression/cases/062_predicates/skip.txt

This file was deleted.

28 changes: 20 additions & 8 deletions e2e/local/regression/cases/062_predicates/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ pipeline:
test:
action: http/runner:send
requests:
- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/vendors/
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect.json')

# - Method: GET
# URL: http://127.0.0.1:8080/v1/api/dev/vendors-predicate?ID=1
# Expect:
# Code: 200
# JSONBody: $LoadJSON('${parentPath}/expect.json')
#
# - Method: GET
# URL: http://127.0.0.1:8080/v1/api/dev/vendors-predicate?ID=1&UserCreated=19273
# Expect:
# Code: 200
# JSONBody: $LoadJSON('${parentPath}/expect2.json')
#
#
# - Method: GET
# URL: http://127.0.0.1:8080/v1/api/dev/vendors-predicate?ID=1&UserCreated=19273&Name=Vendor%202
# Expect:
# Code: 200
# JSONBody: $LoadJSON('${parentPath}/expect3.json')

- Method: GET
URL: http://127.0.0.1:8080/v1/api/meta/struct/dev/vendors/
URL: http://127.0.0.1:8080/v1/api/dev/vendors-predicate?ID=1&UserCreated=19273&Name=Vendor%202&AccountID=100
Expect:
Code: 200
Body: $Cat('${parentPath}/expect_2.txt')
JSONBody: $LoadJSON('${parentPath}/expect4.json')
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

#set($_ = $ID<int>(query/ID).WithPredicate(0, "equal", "t", "ID").Optional())
#set($_ = $UserCreated<int>(query/UserCreated).WithPredicate(0, "equal", "t", "USER_CREATED").Optional())
#set($_ = $Name<int>(query/Name).WithPredicate(1, "equal", "t", "NAME").Optional())
#set($_ = $Name<string>(query/Name).WithPredicate(1, "equal", "t", "NAME").Optional())
#set($_ = $AccountID<int>(query/AccountID).WithPredicate(1, "equal", "t", "ACCOUNT_ID").Optional())

SELECT vendor.*
FROM (SELECT *
FROM VENDOR t
WHERE 1 = 1
${predicate.Builder().Or($predicate.Ctx(0, "AND"), $predicate.Ctx(1, "OR" )).Build("AND")}
${predicate.Builder().CombineOr(
$predicate.Ctx(0, "AND"),
$predicate.Ctx(1, "OR" )
).Build("AND")}
) vendor
2 changes: 1 addition & 1 deletion e2e/local/regression/regression.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipeline:
'[]gen': '@gen'

subPath: 'cases/${index}_*'
range: 62..062
range: 1..062
template:
checkSkip:
action: nop
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/viant/sqlx v0.9.0
github.com/viant/structql v0.4.1
github.com/viant/toolbox v0.34.6-0.20221112031702-3e7cdde7f888
github.com/viant/velty v0.2.0
github.com/viant/velty v0.2.1-0.20230718013203-d4e43dfe0ea9
github.com/viant/xdatly/types/custom v0.0.0-20230309034540-231985618fc7
github.com/viant/xreflect v0.3.2-0.20230703205132-5d95452045da
github.com/viant/xunsafe v0.8.4
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down Expand Up @@ -180,9 +183,11 @@ github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/googleapis/gax-go/v2 v2.8.0 h1:UBtEZqx1bjXtOQ5BVTkuYghXrr3N4V123VKJK67vJZc=
github.com/googleapis/gax-go/v2 v2.8.0/go.mod h1:4orTrqY6hXxxaUL4LHIPl6lGo8vAE38/qKbhSAKP6QI=
github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down Expand Up @@ -332,8 +337,6 @@ github.com/viant/structql v0.4.1/go.mod h1:gHebhXqfhtTpehk9VyeDq6D1rO87hv62AgG4p
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/viant/toolbox v0.34.6-0.20221112031702-3e7cdde7f888 h1:iQ9ehV+Qev9s/L4eXFFaw3zvZVid+xTT5fW3G3ldEdk=
github.com/viant/toolbox v0.34.6-0.20221112031702-3e7cdde7f888/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/viant/velty v0.2.0 h1:JzfqeEva8MN0yaGq5ipOJuVbhniJh4+bILlh1qTc8Lo=
github.com/viant/velty v0.2.0/go.mod h1:C9LNTFXg7DR8B9+tcY2STVqiRQasDr8AZxMhy/lIpOU=
github.com/viant/xdatly v0.3.1-0.20230713223438-282037388a67 h1:0T2BRrpDt2t1SqDNZBzUNMll0v66ZQAHtzNL3EnZnPY=
github.com/viant/xdatly v0.3.1-0.20230713223438-282037388a67/go.mod h1:VElB75XaMgWXTSAqbwpDSlZQeavN8yvqWBnAPTyCfzI=
github.com/viant/xdatly/extension v0.0.0-20230323215422-3e5c3147f0e6 h1:smo1mQEneoMzqnC0ThQpGZmx0gDywu7GFUmjab9rVYM=
Expand Down
18 changes: 10 additions & 8 deletions router/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (p *RequestParams) init(request *http.Request, route *Route) (string, error
return "", nil
}

func (p *RequestParams) queryParam(name string, defaultValue string) string {
func (p *RequestParams) queryParam(name string) (string, bool) {
values, ok := p.queryIndex[name]
if !ok {
return defaultValue
return "", ok
}

return values[0]
return values[0], true
}

func (p *RequestParams) pathVariable(name string, defaultValue string) string {
Expand Down Expand Up @@ -154,7 +154,8 @@ func (p *RequestParams) outputFormat(route *Route) string {
}

func (p *RequestParams) outputQueryFormat(route *Route) string {
format := strings.ToLower(p.queryParam(FormatQuery, ""))
param, _ := p.queryParam(FormatQuery)
format := strings.ToLower(param)
if format == "" {
format = route.Output.DataFormat
}
Expand Down Expand Up @@ -279,9 +280,10 @@ func (p *RequestParams) ExtractHttpParam(ctx context.Context, param *view.Parame
func (p *RequestParams) extractHttpParam(ctx context.Context, param *view.Parameter, options []interface{}) (interface{}, error) {
switch param.In.Kind {
case view.KindPath:
return p.convert(ctx, p.pathVariable(param.In.Name, ""), param, options...)
return p.convert(true, p.pathVariable(param.In.Name, ""), param, options...)
case view.KindQuery:
return p.convert(ctx, p.queryParam(param.In.Name, ""), param, options...)
pValue, ok := p.queryParam(param.In.Name)
return p.convert(ok, pValue, param, options...)
case view.KindRequestBody:
body, err := p.paramRequestBody(ctx, param, options...)
if err != nil {
Expand All @@ -290,9 +292,9 @@ func (p *RequestParams) extractHttpParam(ctx context.Context, param *view.Parame

return body, nil
case view.KindHeader:
return p.convert(ctx, p.header(param.In.Name), param, options...)
return p.convert(true, p.header(param.In.Name), param, options...)
case view.KindCookie:
return p.convert(ctx, p.cookie(param.In.Name), param, options...)
return p.convert(true, p.cookie(param.In.Name), param, options...)
}

return nil, fmt.Errorf("unsupported param kind %v", param.In.Kind)
Expand Down
2 changes: 1 addition & 1 deletion router/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (r *Resource) Init(ctx context.Context) error {
columnsCache = r.ColumnsCache.Items
}

if err := r.Resource.Init(ctx, r.Resource.TypeRegistry(), r._visitors, columnsCache, transforms); err != nil {
if err := r.Resource.Init(ctx, r.Resource.TypeRegistry(), r._visitors, columnsCache, transforms, config.Config.Predicates); err != nil {
return err
}

Expand Down
14 changes: 11 additions & 3 deletions router/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (b *paramStateBuilder) populateOrderBy(ctx context.Context, selector *view.
func (b *paramStateBuilder) orderByValue(ctx context.Context, details *ViewDetails, selector *view.Selector) (string, error) {
param := details.View.Selector.OrderByParam
value, err := b.extractParamValue(ctx, param, details, selector)
if err != nil {
if err != nil || value == nil {
return "", err
}

Expand Down Expand Up @@ -373,6 +373,10 @@ func (b *paramStateBuilder) offsetValue(ctx context.Context, details *ViewDetail
}

func asInt(value interface{}, param *view.Parameter) (int, error) {
if value == nil {
return 0, nil
}

if actual, ok := value.(int); ok {
return actual, nil
}
Expand Down Expand Up @@ -404,7 +408,7 @@ func (b *paramStateBuilder) populateFields(ctx context.Context, selector *view.S
func (b *paramStateBuilder) fieldRawValue(ctx context.Context, details *ViewDetails, selector *view.Selector) (string, int32, error) {
param := details.View.Selector.FieldsParam
paramValue, err := b.extractParamValue(ctx, param, details, selector)
if err != nil {
if err != nil || paramValue == nil {
return "", ValuesSeparator, err
}

Expand Down Expand Up @@ -456,11 +460,15 @@ func (b *paramStateBuilder) extractParamValueWithOptions(ctx context.Context, pa
return transformIfNeeded(ctx, param, value, options...)
}

func (p *RequestParams) convert(ctx context.Context, raw string, param *view.Parameter, options ...interface{}) (interface{}, error) {
func (p *RequestParams) convert(isSpecified bool, raw string, param *view.Parameter, options ...interface{}) (interface{}, error) {
if raw == "" && param.IsRequired() {
return nil, requiredParamErr(param)
}

if !isSpecified {
return nil, nil
}

dateFormat := p.route.DateFormat
if param.DateFormat != "" {
dateFormat = param.DateFormat
Expand Down
Loading

0 comments on commit 95cc428

Please sign in to comment.