Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.20.2
v0.21.0
49 changes: 47 additions & 2 deletions cmd/command/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (s *Service) generate(ctx context.Context, options *options.Options) error
if _, err := s.loadPlugin(ctx, options); err != nil {
return err
}
if ruleOption.EffectiveEngine() == "shape" && options.Generate.Operation != "get" {
return fmt.Errorf("shape engine currently supports gen get only")
}
if options.Generate.Operation == "get" {
return s.generateGet(ctx, options)
}
Expand Down Expand Up @@ -144,8 +147,50 @@ func (s *Service) generateGet(ctx context.Context, opts *options.Options) (err e
if err = s.translate(ctx, opts); err != nil {
return err
}
if err = s.persistRepository(ctx); err != nil {
return err
if opts.Rule().EffectiveEngine() != options.EngineShape {
if err = s.persistRepository(ctx); err != nil {
return err
}
}

if opts.Rule().EffectiveEngine() == options.EngineShape {
componentURL := url.Join(translate.Repository.RepositoryURL, "Datly", "routes")
datlySrv, err := datly.New(ctx, repository.WithComponentURL(componentURL))
if err != nil {
return err
}
for i, source := range sources {
translate.Rule.Index = i
sourceText, loadErr := translate.Rule.LoadSource(ctx, s.fs, source)
if loadErr != nil {
return loadErr
}
method, uri := parseShapeRulePath(sourceText, translate.Rule.RuleName(), translate.Repository.APIPrefix)
key := uri
if !strings.EqualFold(method, "GET") {
key = method + ":" + uri
}
aComponent, compErr := datlySrv.Component(ctx, key)
if compErr != nil {
return compErr
}
_, sourceName := path.Split(url.Path(source))
sourceName = trimExt(sourceName)
var embeds = map[string]string{}
var namedResources []string
if repo := opts.Repository(); repo != nil && len(repo.SubstitutesURL) > 0 {
namedResources = append(namedResources, repo.SubstitutesURL...)
}
code := aComponent.GenerateOutputCode(ctx, defComp, true, embeds, namedResources...)
destURL := path.Join(translate.Rule.ModuleLocation, translate.Rule.ModulePrefix, sourceName+".go")
if err = s.fs.Upload(ctx, destURL, file.DefaultFileOsMode, strings.NewReader(code)); err != nil {
return err
}
if err = s.persistEmbeds(ctx, translate.Rule.ModuleLocation, translate.Rule.ModulePrefix, embeds, aComponent); err != nil {
return err
}
}
return nil
}

for i, resource := range s.translator.Repository.Resource {
Expand Down
2 changes: 1 addition & 1 deletion cmd/command/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (s *Service) reportPluginIssue(ctx context.Context, destURL string) error {
if fixBuilder.Len() > 0 {
fmt.Printf("[FIXME]: to address pulugin dependency run the following:\n")
}
fmt.Printf(fixBuilder.String())
fmt.Print(fixBuilder.String())
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/command/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (s *Service) Translate(ctx context.Context, opts *options.Options) (err err
if err = s.translate(ctx, opts); err != nil {
return err
}
if opts.Rule().EffectiveEngine() == options.EngineShape {
return nil
}
return s.persistRepository(ctx)
}

Expand All @@ -49,6 +52,9 @@ func (s *Service) persistRepository(ctx context.Context) error {
}

func (s *Service) translate(ctx context.Context, opts *options.Options) error {
if opts.Rule().EffectiveEngine() == options.EngineShape {
return s.translateShape(ctx, opts)
}
if err := s.ensureTranslator(opts); err != nil {
return fmt.Errorf("failed to create translator: %v", err)
}
Expand Down
197 changes: 197 additions & 0 deletions cmd/command/translate_shape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package command

import (
"context"
"encoding/json"
"fmt"
"path"
"path/filepath"
"strings"

"github.com/viant/afs/file"
"github.com/viant/afs/url"
"github.com/viant/datly/cmd/options"
"github.com/viant/datly/repository"
"github.com/viant/datly/repository/contract"
"github.com/viant/datly/repository/shape"
shapeCompile "github.com/viant/datly/repository/shape/compile"
shapeLoad "github.com/viant/datly/repository/shape/load"
datlyservice "github.com/viant/datly/service"
"github.com/viant/datly/shared"
"github.com/viant/datly/view"
"gopkg.in/yaml.v3"
)

func (s *Service) translateShape(ctx context.Context, opts *options.Options) error {
rule := opts.Rule()
compiler := shapeCompile.New()
loader := shapeLoad.New()
for rule.Index = 0; rule.Index < len(rule.Source); rule.Index++ {
sourceURL := rule.SourceURL()
_, name := url.Split(sourceURL, file.Scheme)
fmt.Printf("translating %v (shape)\n", name)
dql, err := rule.LoadSource(ctx, s.fs, sourceURL)
if err != nil {
return err
}
dql = strings.TrimSpace(dql)
if dql == "" {
return fmt.Errorf("source %s was empty", sourceURL)
}
shapeSource := &shape.Source{
Name: strings.TrimSuffix(name, path.Ext(name)),
Path: url.Path(sourceURL),
DQL: dql,
Connector: strings.TrimSpace(rule.Connector),
}
planResult, err := compiler.Compile(ctx, shapeSource)
if err != nil {
return fmt.Errorf("failed to compile %s: %w", sourceURL, err)
}
componentArtifact, err := loader.LoadComponent(ctx, planResult)
if err != nil {
return fmt.Errorf("failed to load %s: %w", sourceURL, err)
}
component, ok := componentArtifact.Component.(*shapeLoad.Component)
if !ok || component == nil {
return fmt.Errorf("unexpected component artifact for %s", sourceURL)
}
if err = s.persistShapeRoute(ctx, opts, sourceURL, dql, componentArtifact.Resource, component); err != nil {
return err
}
}
paths := url.Join(opts.Repository().RepositoryURL, "Datly", "routes", "paths.yaml")
if ok, _ := s.fs.Exists(ctx, paths); ok {
_ = s.fs.Delete(ctx, paths)
}
return nil
}

type shapeRuleFile struct {
Resource *view.Resource `yaml:"Resource,omitempty"`
Routes []*repository.Component `yaml:"Routes,omitempty"`
TypeContext any `yaml:"TypeContext,omitempty"`
}

func (s *Service) persistShapeRoute(ctx context.Context, opts *options.Options, sourceURL, dql string, resource *view.Resource, component *shapeLoad.Component) error {
rule := opts.Rule()
routeYAML, routeRoot, relDir, stem, err := routePathForShape(rule, opts.Repository().RepositoryURL, sourceURL)
if err != nil {
return err
}
if resource != nil {
for _, item := range resource.Views {
if item == nil || item.Template == nil {
continue
}
if strings.TrimSpace(item.Template.Source) == "" {
continue
}
sqlRel := strings.TrimSpace(item.Template.SourceURL)
if sqlRel == "" {
sqlRel = path.Join(stem, item.Name+".sql")
}
sqlDest := path.Join(routeRoot, relDir, filepath.ToSlash(sqlRel))
if err = s.fs.Upload(ctx, sqlDest, file.DefaultFileOsMode, strings.NewReader(item.Template.Source)); err != nil {
return fmt.Errorf("failed to persist sql %s: %w", sqlDest, err)
}
item.Template.SourceURL = sqlRel
}
}
rootView := ""
if component != nil {
rootView = strings.TrimSpace(component.RootView)
}
if rootView == "" && resource != nil && len(resource.Views) > 0 && resource.Views[0] != nil {
rootView = resource.Views[0].Name
}
method, uri := parseShapeRulePath(dql, rule.RuleName(), opts.Repository().APIPrefix)
route := &repository.Component{
Path: contract.Path{
Method: method,
URI: uri,
},
Contract: contract.Contract{
Service: serviceForMethod(method),
},
View: &view.View{Reference: shared.Reference{Ref: rootView}},
}
if component != nil {
route.TypeContext = component.TypeContext
if component.Directives != nil && component.Directives.MCP != nil {
route.Name = strings.TrimSpace(component.Directives.MCP.Name)
route.Description = strings.TrimSpace(component.Directives.MCP.Description)
route.DescriptionURI = strings.TrimSpace(component.Directives.MCP.DescriptionPath)
}
}
payload := &shapeRuleFile{
Resource: resource,
Routes: []*repository.Component{route},
}
if component != nil && component.TypeContext != nil {
payload.TypeContext = component.TypeContext
}
data, err := yaml.Marshal(payload)
if err != nil {
return err
}
if err = s.fs.Upload(ctx, routeYAML, file.DefaultFileOsMode, strings.NewReader(string(data))); err != nil {
return fmt.Errorf("failed to persist route yaml %s: %w", routeYAML, err)
}
return nil
}

func routePathForShape(rule *options.Rule, repoURL, sourceURL string) (routeYAML string, routeRoot string, relDir string, stem string, err error) {
sourcePath := filepath.Clean(url.Path(sourceURL))
basePath := filepath.Clean(rule.BaseRuleURL())
relative, relErr := filepath.Rel(basePath, sourcePath)
if relErr != nil || strings.HasPrefix(relative, "..") {
relative = filepath.Base(sourcePath)
}
relative = filepath.ToSlash(relative)
relDir = filepath.ToSlash(path.Dir(relative))
if relDir == "." {
relDir = ""
}
stem = strings.TrimSuffix(path.Base(relative), path.Ext(relative))
routeRoot = url.Join(repoURL, "Datly", "routes")
routeYAML = url.Join(routeRoot, relDir, stem+".yaml")
return routeYAML, routeRoot, relDir, stem, nil
}

type shapeRuleHeader struct {
Method string `json:"Method"`
URI string `json:"URI"`
}

func parseShapeRulePath(dql, ruleName, apiPrefix string) (string, string) {
method := "GET"
uri := "/" + strings.Trim(strings.TrimSpace(ruleName), "/")
if prefix := strings.TrimSpace(apiPrefix); prefix != "" {
uri = strings.TrimRight(prefix, "/") + uri
}
start := strings.Index(dql, "/*")
end := strings.Index(dql, "*/")
if start != -1 && end > start+2 {
raw := strings.TrimSpace(dql[start+2 : end])
if strings.HasPrefix(raw, "{") && strings.HasSuffix(raw, "}") {
header := &shapeRuleHeader{}
if err := json.Unmarshal([]byte(raw), header); err == nil {
if candidate := strings.TrimSpace(strings.ToUpper(header.Method)); candidate != "" {
method = candidate
}
if candidate := strings.TrimSpace(header.URI); candidate != "" {
uri = candidate
}
}
}
}
return method, uri
}

func serviceForMethod(method string) datlyservice.Type {
if strings.EqualFold(method, "GET") {
return datlyservice.TypeReader
}
return datlyservice.TypeExecutor
}
30 changes: 30 additions & 0 deletions cmd/command/translate_shape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package command

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/viant/datly/cmd/options"
)

func TestParseShapeRulePath(t *testing.T) {
method, uri := parseShapeRulePath(`/* {"Method":"POST","URI":"/v1/api/orders"} */ SELECT 1`, "orders", "/v1/api")
assert.Equal(t, "POST", method)
assert.Equal(t, "/v1/api/orders", uri)

method, uri = parseShapeRulePath(`SELECT 1`, "orders", "/v1/api")
assert.Equal(t, "GET", method)
assert.Equal(t, "/v1/api/orders", uri)
}

func TestRoutePathForShape(t *testing.T) {
rule := &options.Rule{Project: "/repo", Source: []string{"/repo/dql/platform/campaign/post.dql"}}
routeYAML, routeRoot, relDir, stem, err := routePathForShape(rule, "/repo/dev", "/repo/dql/platform/campaign/post.dql")
require.NoError(t, err)
assert.Equal(t, "/repo/dev/Datly/routes/platform/campaign/post.yaml", routeYAML)
assert.Equal(t, "/repo/dev/Datly/routes", routeRoot)
assert.Equal(t, filepath.ToSlash("platform/campaign"), relDir)
assert.Equal(t, "post", stem)
}
34 changes: 34 additions & 0 deletions cmd/options/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package options

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/viant/datly/internal/testutil/sqlnormalizer"
"github.com/viant/sqlparser"
)

func parserOption() sqlparser.Option {
return sqlparser.WithErrorHandler(nil)
}

func TestRule_NormalizeSQL(t *testing.T) {
for _, testCase := range sqlnormalizer.Cases() {
t.Run(testCase.Name, func(t *testing.T) {
rule := &Rule{Generated: testCase.Generated}
actual := rule.NormalizeSQL(testCase.SQL, parserOption)
require.Equal(t, testCase.Expect, actual)
})
}
}

func TestMapper_Map(t *testing.T) {
m := mapper{"a": "A"}
require.Equal(t, "A", m.Map("a"))
require.Equal(t, "b", m.Map("b"))
}

func TestNormalizeName(t *testing.T) {
require.Equal(t, "UserAlias", normalizeName("user_alias"))
require.Equal(t, "UserAlias", normalizeName("UserAlias"))
}
16 changes: 16 additions & 0 deletions cmd/options/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Rule struct {
Name string `short:"n" long:"name" description:"rule name"`
ModulePrefix string `short:"u" long:"namespace" description:"rule uri/namespace" default:"dev" `
Source []string `short:"s" long:"src" description:"source"`
Engine string `long:"engine" description:"translation engine" choice:"legacy" choice:"shape"`
Packages []string `short:"g" long:"pkg" description:"entity package"`
Output []string
Index int
Expand All @@ -33,6 +34,21 @@ type Rule struct {
IncludePredicates bool `short:"K" long:"inclPred" description:"generate predicate code" `
}

const (
EngineLegacy = "legacy"
EngineShape = "shape"
)

func (r *Rule) EffectiveEngine() string {
engine := strings.ToLower(strings.TrimSpace(r.Engine))
switch engine {
case EngineShape:
return EngineShape
default:
return EngineLegacy
}
}

// Module returns go module
func (r *Rule) Module() (*modfile.Module, error) {
if r.module != nil {
Expand Down
Loading
Loading