Skip to content

Commit

Permalink
added param group
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Jul 29, 2023
1 parent dfbe7c6 commit 64f5486
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 23 deletions.
1 change: 1 addition & 0 deletions config/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
Parent string
Name string
Context int
Ensure bool
Args []string
}

Expand Down
21 changes: 21 additions & 0 deletions e2e/local/regression/cases/065_params_group/expect1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{"@indexBy@": "id"},
{
"id": 1,
"name": "BAR - 1",
"price": 1.1234567801234567,
"tax": 0.0112345675
},
{
"id": 2,
"name": "BAR - 2",
"price": 2.2345678012345678,
"tax": 0.022345679
},
{
"id": 3,
"name": "BAR - 3",
"price": 3.345678012345678,
"tax": 0.03345678
}
]
8 changes: 8 additions & 0 deletions e2e/local/regression/cases/065_params_group/expect2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"id": 3,
"name": "BAR - 3",
"price": 3.345678012345678,
"tax": 0.03345678
}
]
1 change: 1 addition & 0 deletions e2e/local/regression/cases/065_params_group/expect3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
5 changes: 5 additions & 0 deletions e2e/local/regression/cases/065_params_group/gen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "$tagId",
"URL": "$path/params_group.sql",
"Args": ""
}
22 changes: 22 additions & 0 deletions e2e/local/regression/cases/065_params_group/params_group.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* {
"URI":"params-group",
"Method": "GET",
"Include": ["./templates.yaml"]
} */


#set($_ = $PriceMin<int>(query/priceMin).WithTag('velty:"names=PriceMin|ValueMin"').Optional())
#set($_ = $PriceMax<int>(query/priceMax).WithTag('velty:"names=PriceMax|ValueMax"').Optional())
#set($_ = $IDMin<int>(query/idMin).WithTag('velty:"names=IDMin|ValueMin"').Optional())
#set($_ = $IDMax<int>(query/idMax).WithTag('velty:"names=IDMax|ValueMax"').Optional())
#set($_ = $PriceRange<?>(group/PriceMin,PriceMax).WithPredicate(0, "custom_range", "t", "PRICE").Optional())
#set($_ = $IDRange<?>(group/IDMin,IDMax).WithPredicate(0, "custom_range", "t", "ID").Optional())

SELECT bar.*
FROM (SELECT *
FROM BAR t
WHERE 1 = 1 ${predicate.Builder().CombineOr(
$predicate.Ctx(0, "AND"),
$predicate.Ctx(1, "OR" )
).Build("AND")}
) bar
22 changes: 22 additions & 0 deletions e2e/local/regression/cases/065_params_group/templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Templates:
- Name: "custom_range"
Args:
- Name: "Alias"
Position: 0
- Name: "ColumnName"
Position: 1
Source: |
${Alias}.${ColumnName} BETWEEN
#if($FilterValue.Has.ValueMin)
$criteria.AppendBinding($FilterValue.ValueMin)
#else
0
#end
AND
#if($FilterValue.Has.ValueMax)
$criteria.AppendBinding($FilterValue.ValueMax)
#else
10000
#end
24 changes: 24 additions & 0 deletions e2e/local/regression/cases/065_params_group/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
init:
parentPath: $parent.path
pipeline:

test:
action: http/runner:send
requests:
- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/params-group
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect1.json')

- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/params-group?priceMin=3&priceMax=4
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect2.json')

- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/params-group?priceMin=3&priceMax=4&idMin=4&idMax=5
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect3.json')
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: 1..064
range: 1..065
template:
checkSkip:
action: nop
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
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.20230723180303-1f248c397c5c
github.com/viant/xunsafe v0.8.4
github.com/viant/xunsafe v0.9.0
golang.org/x/mod v0.11.0
golang.org/x/oauth2 v0.7.0
google.golang.org/api v0.114.0
Expand All @@ -57,7 +57,7 @@ require (
github.com/aws/aws-sdk-go v1.44.12
github.com/aws/aws-sdk-go-v2/config v1.18.3
github.com/aws/aws-sdk-go-v2/service/s3 v1.33.1
github.com/viant/structology v0.2.0
github.com/viant/structology v0.3.0
github.com/viant/xdatly v0.3.1-0.20230713223438-282037388a67
github.com/viant/xdatly/extension v0.0.0-20230323215422-3e5c3147f0e6
github.com/viant/xdatly/handler v0.0.0-20230713223438-282037388a67
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ github.com/viant/sqlparser v0.5.1-0.20230722142201-917df90501d3 h1:V7MKp8G6Duoin
github.com/viant/sqlparser v0.5.1-0.20230722142201-917df90501d3/go.mod h1:DqBBJRmfeVVycmCl10uVPzewKVHNLw0sZoPTM2WnRxY=
github.com/viant/sqlx v0.9.0 h1:o/QmTwSkhq63HEyEo9EaVKgVtWhfkW7VScKc0MYOl8s=
github.com/viant/sqlx v0.9.0/go.mod h1:MY0J244wdIMXAkHiDnkCUrihLOKxG6Zeia0ajqJKbbY=
github.com/viant/structology v0.2.0 h1:mntwzS1Xq08u+lLsHO4CvnfIwIrVjX/1PLaW7jyriPs=
github.com/viant/structology v0.2.0/go.mod h1:zW7KLMl3U1wcBARkrTWtf8AdSKewrJHgnlv5W3Ust+I=
github.com/viant/structology v0.3.0 h1:IOqhk9udrnIakUyFciw8M4lOP1U8UFMfpTwbcs7thfM=
github.com/viant/structology v0.3.0/go.mod h1:XFqJE56ynkF+tLqWIlVjGrvYHTZjiqEIaV6Pu0Gid3c=
github.com/viant/structql v0.4.1 h1:eQab1M5eTGFTSLSLIwRMpz/C5IKzixRskon2V8qhtRM=
github.com/viant/structql v0.4.1/go.mod h1:gHebhXqfhtTpehk9VyeDq6D1rO87hv62AgG4p/e91UY=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
Expand All @@ -346,8 +346,8 @@ github.com/viant/xdatly/types/custom v0.0.0-20230309034540-231985618fc7 h1:lQZR7
github.com/viant/xdatly/types/custom v0.0.0-20230309034540-231985618fc7/go.mod h1:NVeS+s/uf2dj/M2QlLdY5KXoci0KPMbD9n02Y0m5hBw=
github.com/viant/xreflect v0.3.2-0.20230723180303-1f248c397c5c h1:bGZZ4b+My5t5A095/OPKUzd7aVq7hDWa/KrQ4SE3jAc=
github.com/viant/xreflect v0.3.2-0.20230723180303-1f248c397c5c/go.mod h1:uflXFHcw4TQXgYJvTQ7Akf4SAzXYPCVi8NGZgsVlwmA=
github.com/viant/xunsafe v0.8.4 h1:+z8MwRh4WB7Ktv2ubJa0KZkjAfqG6RX0sxM7Gxx+l24=
github.com/viant/xunsafe v0.8.4/go.mod h1:V3RCwtqpbNPznhmHysyAOpsyuSVkIYWo1Ewip7qb9/s=
github.com/viant/xunsafe v0.9.0 h1:pyvHiGjYqwVNwbIfUTvO8W960S6zvM/gxP4iRyAl6BE=
github.com/viant/xunsafe v0.9.0/go.mod h1:V3RCwtqpbNPznhmHysyAOpsyuSVkIYWo1Ewip7qb9/s=
github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v0.0.0-20221210110428-332342483e3f h1:wihIB0V/mGpVYrL8I7n/WxVqWnP07CBXZ5uCgxUP1tI=
Expand Down
26 changes: 26 additions & 0 deletions internal/inference/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,32 @@ func (s *State) SetLiterals(target interface{}) *types.Accessors {
return accessors
}

func (s *State) Group() State {
newState := make(State, 0, len(*s))
groupIndex := map[string][]*Parameter{}
groupParams := s.FilterByKind(view.KindGroup)
for _, param := range groupParams {
baseParams := strings.Split(param.In.Name, ",")
for _, baseParam := range baseParams {
groupIndex[baseParam] = append(groupIndex[baseParam], param)
}
}

for _, parameter := range *s {
parameters, ok := groupIndex[parameter.Name]
if !ok {
newState.Append(parameter)
continue
}

for _, parent := range parameters {
parent.Group = append(parent.Group, &parameter.Parameter)
}
}

return newState
}

// NewState creates a state from state go struct
func NewState(modulePath, dataType string, types *xreflect.Types) (State, error) {
baseDir := modulePath
Expand Down
31 changes: 22 additions & 9 deletions internal/translator/parser/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,13 @@ func (s *Declarations) parseShorthands(declaration *Declaration, cursor *parsly.
required := false
declaration.Required = &required
case "WithPredicate":
if len(args) < 2 {
return fmt.Errorf("expected WithPredicate to have at least 2 args, but got %v", len(args))
if err := s.appendPredicate(declaration, args, false); err != nil {
return err
}
ctx, err := strconv.Atoi(args[0])
if err != nil {
case "EnsurePredicate":
if err := s.appendPredicate(declaration, args, true); err != nil {
return err
}
declaration.Predicates = append(declaration.Predicates, &config.PredicateConfig{
Name: args[1],
Context: ctx,
Args: args[2:],
})
case "UtilParam":
//deprecated
}
Expand All @@ -245,6 +240,24 @@ func (s *Declarations) parseShorthands(declaration *Declaration, cursor *parsly.
return nil
}

func (s *Declarations) appendPredicate(declaration *Declaration, args []string, ensure bool) error {
if len(args) < 2 {
return fmt.Errorf("expected WithPredicate to have at least 2 args, but got %v", len(args))
}

ctx, err := strconv.Atoi(args[0])
if err != nil {
return err
}
declaration.Predicates = append(declaration.Predicates, &config.PredicateConfig{
Name: args[1],
Context: ctx,
Args: args[2:],
Ensure: ensure,
})
return nil
}

func extractArgs(content string) []string {
result := make([]string, 0)
cursor := parsly.NewCursor("", []byte(strings.Trim(content, `"`)), 0)
Expand Down
4 changes: 4 additions & 0 deletions internal/translator/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (r *Resource) ExtractDeclared(dSQL *string) (err error) {
return err
}
r.State.Append(r.Declarations.State...)
if len(r.State.FilterByKind(view.KindGroup)) > 0 {
r.State = r.State.Group()
}

r.Rule.Route.Transforms = r.Declarations.Transforms
if err := parser.ExtractParameterHints(r.Declarations.SQL, &r.State); err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions router/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/viant/datly/converter"
"github.com/viant/datly/reader"
"github.com/viant/datly/router/criteria"
"github.com/viant/datly/utils/types"
"github.com/viant/datly/view"
"github.com/viant/structology"
"github.com/viant/toolbox/format"
"github.com/viant/xdatly/handler/parameter"
"github.com/viant/xunsafe"
Expand Down Expand Up @@ -448,6 +450,8 @@ func (b *paramStateBuilder) extractParamValueWithOptions(ctx context.Context, pa
return param.Const, nil
case view.KindRequest:
return b.params.request, nil
case view.KindGroup:
return b.groupParam(ctx, param, parentView)
}

return b.params.extractHttpParam(ctx, param, options)
Expand Down Expand Up @@ -734,6 +738,31 @@ func (b *paramStateBuilder) Value(ctx context.Context, paramName string) (interf
return b.extractParamValueWithOptions(ctx, lookup, nil)
}

func (b *paramStateBuilder) groupParam(ctx context.Context, param *view.Parameter, parentView *view.View) (interface{}, error) {
var state *structology.State
var value interface{}

for _, p := range param.Group {
pValue, err := b.extractParamValueWithOptions(ctx, p, parentView)
if err != nil {
return nil, err
}

if pValue != nil {
if value == nil {
value = types.NewValue(param.Schema.Type())
state = param.NewState(value)
}

if err := state.SetValue(p.Name, pValue); err != nil {
return nil, err
}
}
}

return value, nil
}

func transformIfNeeded(ctx context.Context, param *view.Parameter, value interface{}, options ...interface{}) (interface{}, error) {
if param.Output == nil {
return value, nil
Expand Down
5 changes: 4 additions & 1 deletion template/expand/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
StateAccessor func(state interface{}, statePtr unsafe.Pointer) (interface{}, error)
HasAccessor func(has interface{}, hasPtr unsafe.Pointer) (bool, error)
Expander func(*Context, interface{}) (*parameter.Criteria, error)
Ensure bool
}

PredicateBuilder struct {
Expand Down Expand Up @@ -123,7 +124,7 @@ func (p *Predicate) expand(ctx int, operator string) (string, error) {
continue
}

if p.hasPtr != nil {
if p.hasPtr != nil && !predicateConfig.Ensure {
value, err := predicateConfig.HasAccessor(p.has, p.hasPtr)
if err != nil {
return "", err
Expand Down Expand Up @@ -154,7 +155,9 @@ func (p *Predicate) expand(ctx int, operator string) (string, error) {
result.WriteString(" ")
}

result.WriteByte('(')
result.WriteString(criteria.Query)
result.WriteByte(')')
accArgs = append(accArgs, criteria.Args...)
}

Expand Down
5 changes: 1 addition & 4 deletions template/expand/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ func (s *State) Init(templateState *est.State, predicates []*PredicateConfig, op
s.MessageBus = s.Session.MessageBus()
}

if len(predicates) > 0 {
s.Predicate = NewPredicate(s.Context, s.Parameters, s.ParametersHas, predicates)
}

s.Predicate = NewPredicate(s.Context, s.Parameters, s.ParametersHas, predicates)
s.State = templateState
}

Expand Down
15 changes: 14 additions & 1 deletion view/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package view
import (
"fmt"
"os"
"strings"
)

// Kind represents parameter location
Expand All @@ -23,12 +24,13 @@ const (
KindPredicate Kind = "predicate"
KindRequest Kind = "http_request"
KindState Kind = "state"
KindGroup Kind = "group"
)

// Validate checks if Kind is valid.
func (k Kind) Validate() error {
switch k {
case KindDataView, KindPath, KindQuery, KindHeader, KindCookie, KindRequestBody, KindEnvironment, KindLiteral, KindParam, KindRequest, KindState:
case KindDataView, KindPath, KindQuery, KindHeader, KindCookie, KindRequestBody, KindEnvironment, KindLiteral, KindParam, KindRequest, KindState, KindGroup:
return nil
}

Expand Down Expand Up @@ -71,6 +73,17 @@ type ParamName string
// Validate checks if ParamName is valid
func (p ParamName) Validate(kind Kind) error {
switch kind {
case KindGroup:
split := strings.Split(string(p), ",")
if len(split) == 0 {
return fmt.Errorf("param name can't be empty")
}

if len(split) == 1 {
return fmt.Errorf("param Group must contain at least 2 params")
}

return nil
case KindRequest, KindLiteral, KindRequestBody, KindState:
return nil
case KindDataView, KindPath, KindQuery, KindHeader, KindCookie, KindParam:
Expand Down
Loading

0 comments on commit 64f5486

Please sign in to comment.