Skip to content

Commit

Permalink
added external params source
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Jul 25, 2023
1 parent 6e5caa5 commit be8f5ad
Show file tree
Hide file tree
Showing 24 changed files with 260 additions and 101 deletions.
6 changes: 3 additions & 3 deletions cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ func (s *Builder) loadSQL(ctx context.Context, builder *routeBuilder, location s
}

func (s *Builder) parseDSQL(ctx context.Context, builder *routeBuilder, SQL []byte) (err error) {
if envURL := s.options.EnvURL; envURL != "" {
envContent, err := s.fs.DownloadWithURL(ctx, envURL)
if embedURL := s.options.EmbedURL; embedURL != "" {
envContent, err := s.fs.DownloadWithURL(ctx, embedURL)
if err != nil {
return err
}
Expand All @@ -955,7 +955,7 @@ func (s *Builder) parseDSQL(ctx context.Context, builder *routeBuilder, SQL []by
}

env := rdata.NewMap()
env.SetValue("env", aMap)
env.SetValue("Embed", aMap)

templateContent := env.ExpandWithoutUDF(string(SQL))
SQL = []byte(templateContent)
Expand Down
10 changes: 7 additions & 3 deletions cmd/command/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (s *Service) ensureTranslator(opts *options.Options) error {
if s.translator != nil {
return nil
}
aTranslator := translator.New(translator.NewConfig(opts.Repository()))
aTranslator := translator.New(translator.NewConfig(opts.Repository()), s.fs)
err := aTranslator.Init(context.Background())
if err == nil {
s.translator = aTranslator
Expand Down Expand Up @@ -47,10 +47,14 @@ func (s *Service) translate(ctx context.Context, opts *options.Options) error {
}
rule := opts.Rule()
for rule.Index = 0; rule.Index < len(rule.Source); rule.Index++ {
dSQL, err := opts.Rule().LoadSource(ctx, s.fs)
currRule := opts.Rule()
sourceURL := currRule.SourceURL()

dSQL, err := currRule.LoadSource(ctx, s.fs, sourceURL)
if err != nil {
return err
}

if err = s.translateDSQL(ctx, rule, dSQL); err != nil {
return err
}
Expand All @@ -63,7 +67,7 @@ func (s *Service) translateDSQL(ctx context.Context, rule *options.Rule, dSQL st
return err
}
if err := s.translator.Translate(ctx, rule, dSQL); err != nil {
fmt.Printf("failed to translate: %v: %w", err)
fmt.Printf("failed to translate: %v", err)
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type (
ConstURL string `long:"constURL" description:"path where const files are stored"`
Legacy bool `short:"l"`
cache *view.Cache
EnvURL string `long:"envURL" description:"environment url, expands template before processing"`
EmbedURL string `long:"embedURL" description:"embed url, expands template before processing"`
}

Package struct {
Expand Down
4 changes: 2 additions & 2 deletions cmd/options/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func (r *Rule) SourceURL() string {
return r.Source[r.Index]
}

func (r *Rule) LoadSource(ctx context.Context, fs afs.Service) (string, error) {
data, err := fs.DownloadWithURL(ctx, r.SourceURL())
func (r *Rule) LoadSource(ctx context.Context, fs afs.Service, URL string) (string, error) {
data, err := fs.DownloadWithURL(ctx, URL)
if err != nil {
return "", err
}
Expand Down
13 changes: 8 additions & 5 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/viant/scy/auth/jwt"
"github.com/viant/sqlx/types"
_ "github.com/viant/xdatly/extension" //go mod level placeholder replacement
"github.com/viant/xdatly/predicate"
"github.com/viant/xdatly/types/core"
_ "github.com/viant/xdatly/types/custom"
"github.com/viant/xreflect"
Expand Down Expand Up @@ -44,11 +45,13 @@ var Config = &Registry{
&VeltyCriteriaFactory{},
&CriteriaBuilderFactory{},
),
Predicates: PredicateRegistry{
PredicateEqual: NewEqualPredicate(),
PredicateNotEqual: NewNotEqualPredicate(),
PredicateNotIn: NewNotInPredicate(),
PredicateIn: NewInPredicate(),
Predicates: &PredicateRegistry{
registry: map[string]*predicate.Template{
PredicateEqual: NewEqualPredicate(),
PredicateNotEqual: NewNotEqualPredicate(),
PredicateNotIn: NewNotInPredicate(),
PredicateIn: NewInPredicate(),
},
},
}

Expand Down
41 changes: 28 additions & 13 deletions config/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const PredicateIn = "in"
const PredicateNotIn = "not_in"

type (
PredicateRegistry map[string]*predicate.Template
PredicateConfig struct {
PredicateRegistry struct {
parent *PredicateRegistry
registry map[string]*predicate.Template
}
PredicateConfig struct {
Parent string
Name string
Context int
Expand All @@ -25,22 +28,34 @@ type (
}
)

func (r PredicateRegistry) Lookup(name string) (*predicate.Template, error) {
result, ok := r[name]
if !ok {
return nil, fmt.Errorf("not found template %v", name)
func (r *PredicateRegistry) Lookup(name string) (*predicate.Template, error) {
result, ok := r.registry[name]
if ok {
return result, nil
}

if r.parent != nil {
return r.parent.Lookup(name)
}

return result, nil
return nil, fmt.Errorf("not found template %v", name)
}

func (r PredicateRegistry) Clone() PredicateRegistry {
result := PredicateRegistry{}
for key, template := range r {
result[key] = template
}
func (r *PredicateRegistry) Scope() *PredicateRegistry {
registry := NewPredicates()
registry.parent = r
return registry
}

func (r *PredicateRegistry) Add(template *predicate.Template) {
r.registry[template.Name] = template
}

return result
func NewPredicates() *PredicateRegistry {
return &PredicateRegistry{
parent: nil,
registry: map[string]*predicate.Template{},
}
}

func NewEqualPredicate() *predicate.Template {
Expand Down
4 changes: 2 additions & 2 deletions config/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type Registry struct {
sync.Mutex
Types *xreflect.Types
Codecs CodecsRegistry
Predicates PredicateRegistry
Predicates *PredicateRegistry
}

func NewRegistry() *Registry {
return &Registry{
Mutex: sync.Mutex{},
Types: xreflect.NewTypes(xreflect.WithRegistry(Config.Types)),
Codecs: CodecsRegistry{},
Predicates: PredicateRegistry{},
Predicates: NewPredicates(),
}
}

Expand Down
27 changes: 0 additions & 27 deletions e2e/local/regression/cases/062_predicates/expect_2.txt

This file was deleted.

36 changes: 18 additions & 18 deletions e2e/local/regression/cases/062_predicates/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ pipeline:
test:
action: http/runner:send
requests:
# - 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/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/dev/vendors-predicate?ID=1&UserCreated=19273&Name=Vendor%202&AccountID=100
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* {
"URI":"vendors-custom-predicate",
"Method": "GET",
"Include": ["./parameters.sql"]
} */


SELECT vendor.*
FROM (SELECT *
FROM VENDOR t
WHERE 1 = 1 ${predicate.Builder().CombineOr(
$predicate.Ctx(0, "AND"),
$predicate.Ctx(1, "OR" )
).Build("AND")}
) vendor
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": 1,
"name": "Vendor 1"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
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/063_external_parameters/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
}
]
5 changes: 5 additions & 0 deletions e2e/local/regression/cases/063_external_parameters/gen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "$tagId",
"URL": "$path/custom_predicate.sql",
"Args": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#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<string>(query/Name).WithPredicate(1, "equal", "t", "NAME").Optional())
#set($_ = $AccountID<int>(query/AccountID).WithPredicate(1, "equal", "t", "ACCOUNT_ID").Optional())
31 changes: 31 additions & 0 deletions e2e/local/regression/cases/063_external_parameters/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
init:
parentPath: $parent.path
pipeline:

test:
action: http/runner:send
requests:
- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/vendors-custom-predicate?ID=1
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect.json')

- Method: GET
URL: http://127.0.0.1:8080/v1/api/dev/vendors-custom-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-custom-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/dev/vendors-custom-predicate?ID=1&UserCreated=19273&Name=Vendor%202&AccountID=100
Expect:
Code: 200
JSONBody: $LoadJSON('${parentPath}/expect4.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..062
range: 1..063
template:
checkSkip:
action: nop
Expand Down
Loading

0 comments on commit be8f5ad

Please sign in to comment.