diff --git a/cmd/command/generate.go b/cmd/command/generate.go index 913fdad1..2dd5f411 100644 --- a/cmd/command/generate.go +++ b/cmd/command/generate.go @@ -16,7 +16,7 @@ import ( "github.com/viant/datly/internal/plugin" "github.com/viant/datly/internal/translator" "github.com/viant/datly/repository" - "github.com/viant/datly/service/executor/handler" + "github.com/viant/datly/repository/handler" "github.com/viant/datly/view/extension" "github.com/viant/datly/view/state" "github.com/viant/tagly/format/text" diff --git a/e2e/debug_gen/datly.go b/e2e/debug_gen/datly.go index 64ed13cd..6ded53b7 100644 --- a/e2e/debug_gen/datly.go +++ b/e2e/debug_gen/datly.go @@ -63,7 +63,7 @@ func main() { baseDir := filepath.Join(toolbox.CallerDirectory(3), "..") fmt.Printf("base: %v\n", baseDir) - caseName := "029_generate_put_one_many" + caseName := "050_redirect" caseFolder := filepath.Join(baseDir, "local/regression/cases/", caseName) gen, err := loadGen(caseFolder, caseName) if err != nil { diff --git a/e2e/local/regression/cases/001_one_to_many/vendor_list.dql b/e2e/local/regression/cases/001_one_to_many/vendor_list.dql index 26ba4f54..34c90135 100644 --- a/e2e/local/regression/cases/001_one_to_many/vendor_list.dql +++ b/e2e/local/regression/cases/001_one_to_many/vendor_list.dql @@ -3,9 +3,8 @@ #set( $_ = $VendorName(form/name).Optional().WithPredicate(0, 'contains', 't', 'NAME')) -#set( $_ = $Fields<[]string>(query/xx).Optional().QuerySelector('vendor')) +#set( $_ = $Fields<[]string>(query/fields).Optional().QuerySelector('vendor')) #set( $_ = $Page(query/page).Optional().QuerySelector('vendor')) -#set( $_ = $Fields<[]string>(query/yy).Optional().QuerySelector('products')) SELECT vendor.*, diff --git a/e2e/local/regression/cases/050_router_redirection/expect.json b/e2e/local/regression/cases/050_router_redirection/expect.json deleted file mode 100644 index c83b372a..00000000 --- a/e2e/local/regression/cases/050_router_redirection/expect.json +++ /dev/null @@ -1,32 +0,0 @@ -[ - { - "@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" - } -] \ No newline at end of file diff --git a/e2e/local/regression/cases/050_router_redirection/gen.json b/e2e/local/regression/cases/050_router_redirection/gen.json deleted file mode 100644 index 168e7641..00000000 --- a/e2e/local/regression/cases/050_router_redirection/gen.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "Name": "$tagId", - "URL": "$path/router_redirection.sql", - "Args": "" -} \ No newline at end of file diff --git a/e2e/local/regression/cases/050_router_redirection/router_redirection.sql b/e2e/local/regression/cases/050_router_redirection/router_redirection.sql deleted file mode 100644 index ad753b23..00000000 --- a/e2e/local/regression/cases/050_router_redirection/router_redirection.sql +++ /dev/null @@ -1,5 +0,0 @@ -/* {"URI":"composition/xxx"} */ - -#set( $_ = $Http(query/fields).Value('id,name,bar')) -#set( $_ = $Http(http/).Redirect("/foo-redirected")) - diff --git a/e2e/local/regression/cases/050_router_redirection/skip.txt b/e2e/local/regression/cases/050_router_redirection/skip.txt deleted file mode 100644 index 08e421c6..00000000 --- a/e2e/local/regression/cases/050_router_redirection/skip.txt +++ /dev/null @@ -1 +0,0 @@ -to be refactored with handler style \ No newline at end of file diff --git a/e2e/local/regression/cases/050_router_redirection/test.yaml b/e2e/local/regression/cases/050_router_redirection/test.yaml deleted file mode 100644 index 40054828..00000000 --- a/e2e/local/regression/cases/050_router_redirection/test.yaml +++ /dev/null @@ -1,12 +0,0 @@ -init: - parentPath: $parent.path -pipeline: - - test: - action: http/runner:send - requests: - - Method: GET - URL: http://127.0.0.1:8080/v1/api/dev/redirect/foo?redirectTo=true - Expect: - Code: 200 - JSONBody: $LoadJSON('${parentPath}/expect.json') diff --git a/gateway/router/handler.go b/gateway/router/handler.go index 7328805f..f8fd12a2 100644 --- a/gateway/router/handler.go +++ b/gateway/router/handler.go @@ -356,7 +356,7 @@ func (r *Handler) handleComponent(ctx context.Context, request *http.Request, aC //TODO merge with Path settings unmarshal := aComponent.UnmarshalFunc(request) locatorOptions := append(aComponent.LocatorOptions(request, hstate.NewForm(), unmarshal)) - aSession := session.New(aComponent.View, session.WithLocatorOptions(locatorOptions...)) + aSession := session.New(aComponent.View, session.WithLocatorOptions(locatorOptions...), session.WithRegistry(r.registry)) err := aSession.InitKinds(state.KindComponent, state.KindHeader, state.KindRequestBody, state.KindForm, state.KindQuery) if err != nil { return nil, err diff --git a/go.mod b/go.mod index 250667dd..ea97f8c9 100644 --- a/go.mod +++ b/go.mod @@ -131,6 +131,7 @@ require ( go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect go.opentelemetry.io/otel/trace v1.24.0 // indirect + golang.org/x/crypto v0.22.0 // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.19.0 // indirect diff --git a/go.sum b/go.sum index b766d6fb..f0f46c13 100644 --- a/go.sum +++ b/go.sum @@ -1135,18 +1135,17 @@ github.com/viant/velty v0.2.1-0.20230927172116-ba56497b5c85 h1:zKk+6hqUipkJXCPCH github.com/viant/velty v0.2.1-0.20230927172116-ba56497b5c85/go.mod h1:Q/UXviI2Nli8WROEpYd/BELMCSvnulQeyNrbPmMiS/Y= github.com/viant/x v0.3.0 h1:/3A0z/uySGxMo6ixH90VAcdjI00w5e3REC1zg5hzhJA= github.com/viant/x v0.3.0/go.mod h1:54jP3qV+nnQdNDaWxEwGTAAzCu9sx9er9htiwTW/Mcw= -github.com/viant/xdatly v0.5.4-0.20240923185204-11a0855c8bb5 h1:BK8UCvls5COU6y24/gLS/YUd8oWyjSVJ+9eOlx9iUsQ= -github.com/viant/xdatly v0.5.4-0.20240923185204-11a0855c8bb5/go.mod h1:YwNS31k5r1Ldw5s7wmQXUKaalN6BWcfX9qePO/b/ilc= github.com/viant/xdatly v0.5.4-0.20241003184238-0d8a69ec651f h1:D3Uu0TCB5ZevIqioFGPXhAjHTyh5t23I4y2isuGT/KU= github.com/viant/xdatly v0.5.4-0.20241003184238-0d8a69ec651f/go.mod h1:YwNS31k5r1Ldw5s7wmQXUKaalN6BWcfX9qePO/b/ilc= github.com/viant/xdatly/extension v0.0.0-20231013204918-ecf3c2edf259 h1:9Yry3PUBDzc4rWacOYvAq/TKrTV0agvMF0gwm2gaoHI= github.com/viant/xdatly/extension v0.0.0-20231013204918-ecf3c2edf259/go.mod h1:fb8YgbVadk8X5ZLz49LWGzWmQlZd7Y/I5wE0ru44bIo= +github.com/viant/xdatly/handler v0.0.0-20241003184238-0d8a69ec651f h1:HGc7Qe48ICl3ldP7aTvwQAhmVt8oxGo577teu/mIt7w= +github.com/viant/xdatly/handler v0.0.0-20241003184238-0d8a69ec651f/go.mod h1:bBa479sCB55VymMrXFccluUFkf6oRI77xhtnWIcL8K0= github.com/viant/xdatly/types/core v0.0.0-20240109065401-9758ebacb4bb h1:X4emK6TIR6IXiFlQz9wEdCi5RJMG3dg3e8+VLQ2zhnM= github.com/viant/xdatly/types/core v0.0.0-20240109065401-9758ebacb4bb/go.mod h1:LJN2m8xJjtYNCvyvNrVanJwvzj8+hYCuPswL8H4qRG0= github.com/viant/xdatly/types/custom v0.0.0-20240801144911-4c2bfca4c23a h1:jecH7mH63gj1zJwD18SdvSHM9Ttr9FEOnhHkYfkCNkI= github.com/viant/xdatly/types/custom v0.0.0-20240801144911-4c2bfca4c23a/go.mod h1:s0oJuKw3+AIT8RTyr+7+6nk+kBZhN/N4QWAQyYvvlqY= -github.com/viant/xlsy v0.3.0 h1:j/cADAd41XcxfAGSyPUZmEOHbVe2dcjdVI6JiTKA6ws= -github.com/viant/xlsy v0.3.0/go.mod h1:RajfF9HkL/PfIxRCvZSubpNlpdMUNDKYZp8C1o3vF4Q= +github.com/viant/xlsy v0.3.1 h1:KwA7PxOTVg+ns4CCPOdfNy5aEA9OUlIByUbuNC9ju0s= github.com/viant/xlsy v0.3.1/go.mod h1:RajfF9HkL/PfIxRCvZSubpNlpdMUNDKYZp8C1o3vF4Q= github.com/viant/xmlify v0.1.1-0.20231127181625-8a6b48ceea12 h1:j7I0D1M5lvmc5dxXzSyx99GSfYiilrSc1hnmFFL+jrg= github.com/viant/xmlify v0.1.1-0.20231127181625-8a6b48ceea12/go.mod h1:w25+umH6nthlQ8ACT3K2/YJOLlbTXKLQXkdqFs6ky9s= diff --git a/internal/translator/resource.go b/internal/translator/resource.go index 859c6340..d1158ff3 100644 --- a/internal/translator/resource.go +++ b/internal/translator/resource.go @@ -269,19 +269,20 @@ func (r *Resource) ExtractDeclared(dSQL *string) (err error) { if err != nil { return err } - for _, item := range r.Declarations.Items { - r.RawSQL = strings.Replace(r.RawSQL, item.Raw, "", 1) - } - - if index := strings.Index(r.RawSQL, "$Nop("); index != -1 { - offset := index + len("$Nop(") - if end := strings.Index(r.RawSQL[offset:], ")"); end != -1 { - noOp := r.RawSQL[offset : offset+end] - r.RawSQL = r.RawSQL[offset+end+1:] - r.Declarations.SQL = strings.Replace(r.Declarations.SQL, "$Nop("+noOp+")", "", 1) - } - } - r.RawSQL = strings.TrimSpace(r.RawSQL) + //for _, item := range r.Declarations.Items { + // r.RawSQL = strings.Replace(r.RawSQL, item.Raw, "", 1) + //} + + // + //if index := strings.Index(r.RawSQL, "$Nop("); index != -1 { + // offset := index + len("$Nop(") + // if end := strings.Index(r.RawSQL[offset:], ")"); end != -1 { + // noOp := r.RawSQL[offset : offset+end] + // // r.RawSQL = r.RawSQL[offset+end+1:] + // r.Declarations.SQL = strings.Replace(r.Declarations.SQL, "$Nop("+noOp+")", "", 1) + // } + //} + //r.RawSQL = strings.TrimSpace(r.RawSQL) r.State.Append(r.Declarations.State...) r.appendPathVariableParams() diff --git a/internal/translator/rule.go b/internal/translator/rule.go index 5e3f5a15..2da479d5 100644 --- a/internal/translator/rule.go +++ b/internal/translator/rule.go @@ -12,9 +12,9 @@ import ( "github.com/viant/datly/repository/async" "github.com/viant/datly/repository/content" "github.com/viant/datly/repository/contract" + "github.com/viant/datly/repository/handler" dpath "github.com/viant/datly/repository/path" "github.com/viant/datly/service" - "github.com/viant/datly/service/executor/handler" "github.com/viant/datly/shared" "github.com/viant/datly/view" "github.com/viant/datly/view/state" diff --git a/repository/component.go b/repository/component.go index 9c54feaf..722e11c9 100644 --- a/repository/component.go +++ b/repository/component.go @@ -14,9 +14,9 @@ import ( "github.com/viant/datly/repository/codegen" "github.com/viant/datly/repository/content" "github.com/viant/datly/repository/contract" + "github.com/viant/datly/repository/handler" "github.com/viant/datly/repository/version" "github.com/viant/datly/service" - "github.com/viant/datly/service/executor/handler" "github.com/viant/datly/shared" "github.com/viant/datly/utils/formatter" "github.com/viant/datly/utils/types" diff --git a/service/executor/handler/handler.go b/repository/handler/handler.go similarity index 100% rename from service/executor/handler/handler.go rename to repository/handler/handler.go diff --git a/repository/locator/component/dispatcher/disptacher.go b/repository/locator/component/dispatcher/disptacher.go index 68a55fd0..8303b8ff 100644 --- a/repository/locator/component/dispatcher/disptacher.go +++ b/repository/locator/component/dispatcher/disptacher.go @@ -42,7 +42,7 @@ func (d *Dispatcher) Dispatch(ctx context.Context, path *contract.Path, opts ... options = append(options, locator.WithHeaders(cOptions.Header)) } - aSession := session.New(aComponent.View, session.WithLocatorOptions(options...)) + aSession := session.New(aComponent.View, session.WithLocatorOptions(options...), session.WithRegistry(d.registry)) ctx = aSession.Context(ctx, true) value, err := d.service.Operate(ctx, aSession, aComponent) return value, err diff --git a/service/executor/handler/executor.go b/service/executor/handler/executor.go index d1583db1..7fceac61 100644 --- a/service/executor/handler/executor.go +++ b/service/executor/handler/executor.go @@ -4,14 +4,17 @@ import ( "context" "database/sql" "fmt" + "github.com/viant/datly/repository/contract" executor "github.com/viant/datly/service/executor" expand "github.com/viant/datly/service/executor/expand" "github.com/viant/datly/service/executor/extension" session "github.com/viant/datly/service/session" "github.com/viant/datly/view" + "github.com/viant/datly/view/state" "github.com/viant/xdatly/handler" http2 "github.com/viant/xdatly/handler/http" "github.com/viant/xdatly/handler/sqlx" + hstate "github.com/viant/xdatly/handler/state" "github.com/viant/xdatly/handler/validator" "net/http" ) @@ -87,24 +90,27 @@ func (e *Executor) HandlerSession(ctx context.Context, opts ...Option) (*extensi if e.handlerSession != nil { return e.handlerSession, nil } + sess := e.newSession(e.session, opts...) + e.handlerSession = sess + return sess, nil +} +func (e *Executor) newSession(aSession *session.Session, opts ...Option) *extension.Session { var options = e.options.Clone(opts) e.session.Apply(session.WithTypes(options.Types...)) e.session.Apply(session.WithEmbeddedFS(options.embedFS)) - res := e.view.GetResource() sess := extension.NewSession( extension.WithTemplateFlush(func(ctx context.Context) error { return e.Execute(ctx) }), - extension.WithStater(e.session), + extension.WithStater(aSession), extension.WithRedirect(e.redirect), extension.WithSql(e.newSqlService), extension.WithHttp(e.newHttp), extension.WithMessageBus(res.MessageBuses), ) - e.handlerSession = sess - return sess, nil + return sess } func (e *Executor) newValidator() *validator.Service { @@ -191,15 +197,25 @@ func (e *Executor) txStarted(tx *sql.Tx) { } func (e *Executor) redirect(ctx context.Context, route *http2.Route) (handler.Session, error) { - //TODO reimplement it - return nil, fmt.Errorf("not yey supported") - //match, err := e.route.match(ctx, route) - //if err != nil { - // return nil, err - //} - // - //newExecutor := NewExecutor(match, e.request, e.response) - //return newExecutor.HandlerSession(ctx) + registry := e.session.Registry() + if registry == nil { + return nil, fmt.Errorf("registry was empty") + } + aComponent, err := registry.Lookup(ctx, contract.NewPath(route.Method, route.URL)) + if err != nil { + return nil, err + } + request, _ := http.NewRequest(route.Method, route.URL, nil) + unmarshal := aComponent.UnmarshalFunc(request) + locatorOptions := append(aComponent.LocatorOptions(request, hstate.NewForm(), unmarshal)) + aSession := session.New(aComponent.View, session.WithLocatorOptions(locatorOptions...), session.WithRegistry(registry)) + err = aSession.InitKinds(state.KindComponent, state.KindHeader, state.KindRequestBody, state.KindForm, state.KindQuery) + if err != nil { + return nil, err + } + ctx = aSession.Context(ctx, true) + anExecutor := NewExecutor(aComponent.View, aSession) + return anExecutor.NewHandlerSession(ctx) } func (e *Executor) newHttp() http2.Http { diff --git a/service/executor/handler/locator/handler.go b/service/executor/handler/locator/handler.go index 452d5807..577e099f 100644 --- a/service/executor/handler/locator/handler.go +++ b/service/executor/handler/locator/handler.go @@ -3,7 +3,8 @@ package locator import ( "context" "fmt" - "github.com/viant/datly/service/executor/handler" + "github.com/viant/datly/repository/handler" + ehandler "github.com/viant/datly/service/executor/handler" "github.com/viant/datly/service/session" "github.com/viant/datly/view" "github.com/viant/datly/view/extension" @@ -45,10 +46,9 @@ func (v *Handler) Value(ctx context.Context, name string) (interface{}, bool, er aView.Connector = resource.Connectors[0] } aSession := session.Context(ctx) + anExecutor := ehandler.NewExecutor(aView, aSession) - anExecutor := handler.NewExecutor(aView, aSession) - - handlerSession, err := anExecutor.NewHandlerSession(ctx, handler.WithTypes(v.types...)) + handlerSession, err := anExecutor.NewHandlerSession(ctx, ehandler.WithTypes(v.types...)) if err != nil { return nil, false, fmt.Errorf("failed to create handler session: %w", err) } diff --git a/service/session/option.go b/service/session/option.go index c16a15a4..4719377c 100644 --- a/service/session/option.go +++ b/service/session/option.go @@ -2,6 +2,7 @@ package session import ( "embed" + "github.com/viant/datly/repository" "github.com/viant/datly/view" "github.com/viant/datly/view/state" "github.com/viant/datly/view/state/kind/locator" @@ -19,6 +20,7 @@ type ( locatorOpt *locator.Options codecOptions []codec.Option types []*state.Type + registry *repository.Registry indirectState bool reportNotAssignable *bool scope string @@ -27,6 +29,10 @@ type ( Option func(o *Options) ) +func (o *Options) Registry() *repository.Registry { + return o.registry +} + func (o *Options) HasInputParameters() bool { if o.locatorOpt == nil { return false @@ -130,3 +136,9 @@ func WithEmbeddedFS(fs *embed.FS) Option { s.embeddedFS = fs } } + +func WithRegistry(registry *repository.Registry) Option { + return func(s *Options) { + s.registry = registry + } +}