Skip to content

Commit

Permalink
Pipeline: rename writeTo to path in template operation
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Jul 20, 2024
1 parent 6045141 commit b23f7b2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pipeline/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,26 @@ func (ps *PatchOp) CloneWith(ctx ActionContext) Action {
}

func (ts *TemplateOp) String() string {
return fmt.Sprintf("Template[WriteTo=%s]", ts.WriteTo)
return fmt.Sprintf("Template[Path=%s]", ts.Path)
}

func (ts *TemplateOp) Do(ctx ActionContext) error {
if len(ts.Template) == 0 {
return ErrTemplateEmpty
}
if len(ts.WriteTo) == 0 {
return ErrWriteToEmpty
if len(ts.Path) == 0 {
return ErrPathEmpty
}
val, err := ctx.TemplateEngine().Render(ts.Template, map[string]interface{}{
"Data": ctx.Snapshot(),
})
ctx.Data().AddValueAt(ts.WriteTo, dom.LeafNode(val))
ctx.Data().AddValueAt(ts.Path, dom.LeafNode(val))
return err
}

func (ts *TemplateOp) CloneWith(ctx ActionContext) Action {
return &TemplateOp{
Template: ts.Template,
WriteTo: ctx.TemplateEngine().RenderLenient(ts.WriteTo, ctx.Snapshot()),
Path: ctx.TemplateEngine().RenderLenient(ts.Path, ctx.Snapshot()),
}
}
10 changes: 5 additions & 5 deletions pipeline/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestExecuteTemplateOp(t *testing.T) {
gd.AddValueAt("root.leaf1", dom.LeafNode(123456))
ts = TemplateOp{
Template: `{{ (mul .Data.root.leaf1 2) | quote }}`,
WriteTo: "result.x1",
Path: "result.x1",
}
assert.NoError(t, New(WithData(gd)).Execute(&ts))
assert.Equal(t, "\"246912\"", gd.Lookup("result.x1").(dom.Leaf).Value())
Expand All @@ -184,23 +184,23 @@ func TestExecuteTemplateOp(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, ErrTemplateEmpty, err)

// empty writeTo error
// empty path error
ts = TemplateOp{
Template: `TEST`,
}
err = New(WithData(gd)).Execute(&ts)
assert.Error(t, err)
assert.Equal(t, ErrWriteToEmpty, err)
assert.Equal(t, ErrPathEmpty, err)

ts = TemplateOp{
Template: `{{}}{{`,
WriteTo: "result",
Path: "result",
}
assert.Error(t, New(WithData(gd)).Execute(&ts))

ts = TemplateOp{
Template: `{{ invalid_func }}`,
WriteTo: "result",
Path: "result",
}
assert.Error(t, New(WithData(gd)).Execute(&ts))
}
2 changes: 1 addition & 1 deletion pipeline/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestExecute(t *testing.T) {
Operations: OpSpec{
Template: &TemplateOp{
Template: "{{ mul 1 2 3 4 5 6 }}",
WriteTo: "Results.Factorial",
Path: "Results.Factorial",
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions pipeline/opspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestOpSpecCloneWith(t *testing.T) {
Action: OpSpec{},
},
Template: &TemplateOp{
WriteTo: "{{ .Path }}",
Path: "{{ .Path }}",
},
Import: &ImportOp{
Path: "{{ .Path }}",
Expand All @@ -64,7 +64,7 @@ func TestOpSpecCloneWith(t *testing.T) {
assert.Equal(t, "root.sub2", a.Set.Path)
assert.Equal(t, "root.sub2", a.Import.Path)
assert.Equal(t, "/root/sub3", a.Patch.Path)
assert.Equal(t, "root.sub2", a.Template.WriteTo)
assert.Equal(t, "root.sub2", a.Template.Path)
assert.Equal(t, "root.sub2", a.Export.Path)
assert.Equal(t, "root.sub2", a.Env.Path)
}
4 changes: 2 additions & 2 deletions pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var (
ErrNoDataToSet = errors.New("no data to set")
ErrTemplateEmpty = errors.New("template cannot be empty")
ErrWriteToEmpty = errors.New("writeTo cannot be empty")
ErrPathEmpty = errors.New("path cannot be empty")
ErrNotContainer = errors.New("data element must be container when no path is provided")
)

Expand Down Expand Up @@ -246,5 +246,5 @@ type TemplateOp struct {
// template to render
Template string `yaml:"template"`
// path within global data tree where to set result at
WriteTo string `yaml:"writeTo"`
Path string `yaml:"path"`
}

0 comments on commit b23f7b2

Please sign in to comment.