Skip to content

Commit

Permalink
Merge pull request #42 from viant/fix_template_paramaters
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
terryzhaobj committed May 17, 2024
2 parents 112589c + c05345d commit e50e672
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
11 changes: 9 additions & 2 deletions repository/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,20 @@ func WithContract(inputType, outputType reflect.Type, embedFs *embed.FS) Compone
outputType = reflect.TypeOf(struct{}{})
}
c.embedFs = embedFs
sType, err := state.NewType(state.WithResource(c.View.Resource()), state.WithFS(embedFs), state.WithSchema(state.NewSchema(inputType)))
resource := c.View.Resource()
sType, err := state.NewType(state.WithResource(resource), state.WithFS(embedFs), state.WithSchema(state.NewSchema(inputType)))
if err != nil {
return err
}
c.Contract.Input.Type = *sType
if err := c.Contract.Input.Type.Init(); err != err {
return fmt.Errorf("failed to initalize input: %w", err)
}
if len(c.Contract.Input.Type.Parameters) > 0 {
for i := range c.Contract.Input.Type.Parameters {
resource.AppendParameter(c.Contract.Input.Type.Parameters[i])
}
}
c.Contract.Output.Type = state.Type{Schema: state.NewSchema(outputType)}
if err := c.Contract.Output.Type.Init(state.WithFS(embedFs)); err != err {
return fmt.Errorf("failed to initalize output: %w", err)
Expand Down Expand Up @@ -420,8 +426,9 @@ func WithContract(inputType, outputType reflect.Type, embedFs *embed.FS) Compone
vOptions = append(vOptions, view.WithBatchSize(aTag.View.Batch))
}

vOptions = append(vOptions, view.WithFS(c.embedFs))
}
vOptions = append(vOptions, view.WithFS(c.embedFs))
vOptions = append(vOptions, view.WithResource(resource))
aView := view.NewView(viewName, table, vOptions...)
c.View = aView
return nil
Expand Down
6 changes: 5 additions & 1 deletion repository/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ func WithJWTVerifier(aVerifier *verifier.Config) Option {

func WithResource(resource *view.Resource) ComponentOption {
return func(c *Component) error {
c.View.SetResource(resource)
if c.View.GetResource() == nil {
c.View.SetResource(&view.Resource{})
}
res := c.View.GetResource()
res.MergeFrom(resource, c.types)
return nil
}
}
Expand Down
3 changes: 3 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ func (s *Service) AddComponent(ctx context.Context, component *repository.Compon
}
}
aView := component.View
if res := aView.GetResource(); res != nil {
components.Resource = res
}
if aView.Name != "" { //swap with ref view
components.Resource.Views = append(components.Resource.Views, aView)
component.View = view.NewRefView(aView.Name)
Expand Down
1 change: 1 addition & 0 deletions view/extension/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func InitRegistry() {
PredicateDuration: NewDurationPredicate(),
PredicateCriteriaIn: NewInCriteriaPredicate(),
PredicateCriteriaNotIn: NewNotInCriteriaPredicate(),
PredicateWhenPresent: NewWhenPresent(),
},
},
Docs: docs.New(),
Expand Down
24 changes: 21 additions & 3 deletions view/extension/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
PredicateCriteriaNotIn = "not_in_criteria"
PredicateBetween = "between"
PredicateDuration = "duration"
PredicateWhenPresent = "when_present"
)

type (
Expand All @@ -52,9 +53,9 @@ type (

PredicateConfig struct {
Parent string
Name string `yaml:"Name"`
Group int `yaml:"NormalizeObject"`
Ensure bool
Name string `yaml:"Name"`
Group int `yaml:"Group"`
Ensure bool `yaml:"Ensure"`
Args []string `yaml:"Args"`
}

Expand Down Expand Up @@ -422,6 +423,10 @@ func NewNotInCriteriaPredicate() *Predicate {
return newInPredicate(PredicateCriteriaNotIn, true, false, false)
}

func NewWhenPresent() *Predicate {
return newWhenPredicate(PredicateWhenPresent)
}

func newExistsPredicate(name string, withCriteria bool, negated bool) *Predicate {
args := []*predicate.NamedArgument{
{
Expand Down Expand Up @@ -501,3 +506,16 @@ func (p *PredicateHandlerFactory) New(lookupType xreflect.LookupType, args ...st

return valueHandler, nil
}

func newWhenPredicate(name string) *Predicate {
condition := `#if($HasFilterValue) ${Criterion} #end`
return &Predicate{
Template: &predicate.Template{
Name: name,
Source: " " + condition,
Args: []*predicate.NamedArgument{
{Name: "Criterion", Position: 0},
},
},
}
}
7 changes: 4 additions & 3 deletions view/state/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func BuildPredicate(aTag *tags.Tag, param *Parameter) {
for _, pTag := range aTag.Predicates {
pTag.Init(param.Name)
param.Predicates = append(param.Predicates, &extension.PredicateConfig{
Group: pTag.Group,
Name: pTag.Name,
Args: pTag.Arguments})
Group: pTag.Group,
Name: pTag.Name,
Args: pTag.Arguments,
Ensure: pTag.Ensure})
}
}
9 changes: 9 additions & 0 deletions view/tags/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Predicate struct {
ExcludeName string
Filter string
Arguments []string
Ensure bool
}

func (p *Predicate) Init(name string) {
Expand Down Expand Up @@ -64,6 +65,10 @@ func (t *Tag) updatedPredicate(key string, value string) (err error) {
if tag.Group, err = strconv.Atoi(strings.TrimSpace(value)); err != nil {
return fmt.Errorf("invalid predicate group: %s %w", value, err)
}
case "ensure":
if tag.Ensure, err = strconv.ParseBool(strings.TrimSpace(value)); err != nil {
return fmt.Errorf("invalid predicate ensure: %s %w", value, err)
}
default:
if value != "" {
return fmt.Errorf("invalid argument %s", value)
Expand All @@ -79,6 +84,10 @@ func (p *Predicate) Tag() *tags.Tag {
builder.WriteString(",")
builder.WriteString("group=")
builder.WriteString(strconv.Itoa(p.Group))
if p.Ensure {
builder.WriteString(",")
builder.WriteString("ensure=true")
}
appendNonEmpty(builder, "filter", p.Filter)
for _, arg := range p.Arguments {
builder.WriteString(",")
Expand Down
12 changes: 12 additions & 0 deletions view/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (t *Template) Init(ctx context.Context, resource *Resource, view *View) err
t.updateSource(view)

t.isTemplate = t.Source != view.Name && t.Source != view.Table
//todo try allocate parameters based on group
if len(t.Parameters) == 0 {

if strings.Contains(t.Source, "${predicate.Builder()") {
for i, candidate := range resource.Parameters {
if len(candidate.Predicates) > 0 {
t.Parameters = append(t.Parameters, state.NewRefParameter(resource.Parameters[i].Name))
}
}

}
}

if err = t.initTypes(ctx, resource); err != nil {
return err
Expand Down

0 comments on commit e50e672

Please sign in to comment.