Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Aug 30, 2024
2 parents 4c04063 + 7708254 commit 84cfd4f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 37 deletions.
6 changes: 4 additions & 2 deletions gateway/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ func (r *Handler) writeResponse(ctx context.Context, request *http.Request, writ
for key, value := range aResponse.Headers() {
writer.Header().Add(key, value[0])
}

compressed, ok := aResponse.(response.Compressed)
if ok && compressed.CompressionType() != "" {
writer.Header().Add(acontent.Encoding, compressed.CompressionType())
}
statusCode := http.StatusOK
if aResponse.StatusCode() > 0 {
statusCode = aResponse.StatusCode()
Expand Down Expand Up @@ -290,7 +293,6 @@ func (r *Handler) PreSign(ctx context.Context, viewName string, aResponse respon
return nil, fmt.Errorf("response is not compressed")
}
compressionType := compressed.CompressionType()

if compressionType != "" {
kv = append(kv, acontent.Encoding, compressionType)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/translator/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ import (
"github.com/viant/datly/view/state"
"github.com/viant/sqlparser"
"reflect"
"strconv"
"strings"
)

// TODO introduce function abstraction for datly -h list funciton, with validation signtaure description
func (n *Viewlets) applySettingFunctions(column *sqlparser.Column, namespace string) (bool, error) {
funcName, funcArgs := extractFunction(column)
var err error
switch funcName {
case "compress_above_size":
if len(funcArgs) == 1 {
if n.compressionSizeKb, err = strconv.Atoi(funcArgs[0]); err != nil {
return false, fmt.Errorf("invalid compression size: %v, %w", funcArgs[0], err)
}
}
return true, nil
}
if funcName == "" {
return false, nil
}
Expand Down
57 changes: 33 additions & 24 deletions internal/translator/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ type (
RequestBody *BodyConfig `json:",omitempty"`
ResponseBody *ResponseBodyConfig `json:",omitempty"`

TypeSrc *parser.TypeImport `json:",omitempty"`
Package string `json:",omitempty"`
Router *RouterConfig `json:",omitempty" yaml:",omitempty"`
DataFormat string `json:",omitempty"`
TabularJSON *content.TabularJSONConfig `json:",omitempty"`
XML *content.XMLConfig `json:",omitempty"`
Type string `json:",omitempty"`
HandlerArgs []string `json:",omitempty"`
InputType string `json:",omitempty"`
OutputType string `json:",omitempty"`
With []string `json:",omitempty"`
Include []string `json:",omitempty"`
TypeSrc *parser.TypeImport `json:",omitempty"`
Package string `json:",omitempty"`
Router *RouterConfig `json:",omitempty" yaml:",omitempty"`
DataFormat string `json:",omitempty"`
TabularJSON *content.TabularJSONConfig `json:",omitempty"`
XML *content.XMLConfig `json:",omitempty"`
Type string `json:",omitempty"`
HandlerArgs []string `json:",omitempty"`
InputType string `json:",omitempty"`
OutputType string `json:",omitempty"`
CompressAboveSize int `json:",omitempty"`
With []string `json:",omitempty"`
Include []string `json:",omitempty"`
indexNamespaces
IsGeneratation bool
XMLUnmarshalType string `json:",omitempty"`
Expand Down Expand Up @@ -133,19 +134,21 @@ func (r *Rule) applyGeneratorOutputSetting() {
func (r *Rule) DSQLSetting() interface{} {

return struct {
URI string
Method string
ResponseBody *ResponseBodyConfig `json:",omitempty"`
Type string `json:",omitempty"`
InputType string `json:",omitempty"`
OutputType string `json:",omitempty"`
URI string
Method string
ResponseBody *ResponseBodyConfig `json:",omitempty"`
Type string `json:",omitempty"`
InputType string `json:",omitempty"`
OutputType string `json:",omitempty"`
CompressAboveSize int `json:",omitempty"`
}{
URI: r.URI,
Method: r.Method,
ResponseBody: r.ResponseBody,
Type: r.Type,
InputType: r.InputType,
OutputType: r.OutputType,
URI: r.URI,
Method: r.Method,
ResponseBody: r.ResponseBody,
Type: r.Type,
InputType: r.InputType,
OutputType: r.OutputType,
CompressAboveSize: r.CompressAboveSize,
}
}

Expand Down Expand Up @@ -374,6 +377,12 @@ func (r *Rule) applyShortHands() {
}

}

if r.CompressAboveSize > 0 {
r.Compression = &dpath.Compression{
MinSizeKb: r.CompressAboveSize,
}
}
if r.Route.Output.Field != "" {
r.Route.Output.Style = contract.ComprehensiveStyle
}
Expand Down
24 changes: 15 additions & 9 deletions internal/translator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/viant/datly/internal/setter"
"github.com/viant/datly/internal/translator/parser"
signature "github.com/viant/datly/repository/contract/signature"
"github.com/viant/datly/repository/path"
"github.com/viant/datly/service"
"github.com/viant/datly/shared"
"github.com/viant/datly/view"
Expand All @@ -26,7 +27,7 @@ import (
"golang.org/x/mod/modfile"
"gopkg.in/yaml.v3"
"net/http"
"path"
spath "path"
"reflect"
"strings"
"time"
Expand All @@ -40,7 +41,7 @@ type Service struct {
}

func (s *Service) InitSignature(ctx context.Context, rule *options.Rule) (err error) {
prefix := path.Join(s.Repository.Config.APIPrefix, rule.ModulePrefix)
prefix := spath.Join(s.Repository.Config.APIPrefix, rule.ModulePrefix)
if s.signature, err = signature.New(ctx, prefix, s.Repository.Config.RouteURL); err != nil {
return err
}
Expand Down Expand Up @@ -333,7 +334,12 @@ func (s *Service) persistRouterRule(ctx context.Context, resource *Resource, ser

}
if !strings.HasPrefix(resource.Rule.Route.URI, resource.repository.APIPrefix) {
resource.Rule.Route.URI = path.Join(resource.repository.APIPrefix, resource.rule.ModulePrefix, resource.Rule.Route.URI)
resource.Rule.Route.URI = spath.Join(resource.repository.APIPrefix, resource.rule.ModulePrefix, resource.Rule.Route.URI)
}
if resource.Rule.Viewlets.compressionSizeKb > 0 {
resource.Rule.Compression = &path.Compression{
MinSizeKb: resource.Rule.Viewlets.compressionSizeKb,
}
}
aState, err := resource.State.Compact(resource.rule.ModuleLocation)
if err != nil {
Expand Down Expand Up @@ -512,7 +518,7 @@ func (s *Service) buildRouterResource(ctx context.Context, resource *Resource) (

if resource.repository.ConstURL != "" {
_, name := url.Split(resource.repository.ConstURL, file.Scheme)
if ext := path.Ext(name); ext != "" {
if ext := spath.Ext(name); ext != "" {
name = name[:len(name)-len(ext)]
}
resource.Rule.With = append(resource.Rule.With, name)
Expand Down Expand Up @@ -562,7 +568,7 @@ func (s *Service) adjustModulePackage(resource *Resource) {
goMod, err := s.fs.DownloadWithURL(context.Background(), url.Join(modLocation, "go.mod"))
if err != nil {
parent, prefix = url.Split(modLocation, file.Scheme)
moduleURI = path.Join(prefix, moduleURI)
moduleURI = spath.Join(prefix, moduleURI)
goMod, _ = s.fs.DownloadWithURL(context.Background(), url.Join(parent, "go.mod"))
}

Expand All @@ -571,7 +577,7 @@ func (s *Service) adjustModulePackage(resource *Resource) {
}

modFile, _ := modfile.Parse("", goMod, nil)
modulePath := path.Join(modFile.Module.Mod.Path, moduleURI)
modulePath := spath.Join(modFile.Module.Mod.Path, moduleURI)
for _, aType := range resource.Resource.Types {
if aType.ModulePath != "" || aType.Package == "" {
continue
Expand Down Expand Up @@ -649,11 +655,11 @@ func (s *Service) updateComponentType(ctx context.Context, resource *Resource, p
}

func (s *Service) adjustModLocation(ctx context.Context, location string) string {
if ok, _ := s.fs.Exists(ctx, path.Join(location, "go.mod")); ok {
if ok, _ := s.fs.Exists(ctx, spath.Join(location, "go.mod")); ok {
return location
}
parent, _ := path.Split(location)
if ok, _ := s.fs.Exists(ctx, path.Join(parent, "go.mod")); ok {
parent, _ := spath.Split(location)
if ok, _ := s.fs.Exists(ctx, spath.Join(parent, "go.mod")); ok {
return parent
}
return location
Expand Down
5 changes: 3 additions & 2 deletions internal/translator/viewlets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

type Viewlets struct {
registry map[string]*Viewlet
keys []string
registry map[string]*Viewlet
keys []string
compressionSizeKb int
}

func (n *Viewlets) Lookup(name string) *Viewlet {
Expand Down

0 comments on commit 84cfd4f

Please sign in to comment.