Skip to content

Commit

Permalink
Pipeline: Move Set opearation to dedicated files
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 15, 2024
1 parent c4bed07 commit d171946
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 59 deletions.
27 changes: 0 additions & 27 deletions pipeline/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,6 @@ func (ps *PatchOp) CloneWith(ctx ActionContext) Action {
}
}

func (sa *SetOp) String() string {
return fmt.Sprintf("Set[Path=%s]", sa.Path)
}

func (sa *SetOp) Do(ctx ActionContext) error {
gd := ctx.Data()
if sa.Data == nil {
return ErrNoDataToSet
}
data := ctx.Factory().FromMap(sa.Data)
if len(sa.Path) > 0 {
gd.AddValueAt(sa.Path, data)
} else {
for k, v := range data.Children() {
gd.AddValueAt(k, v)
}
}
return nil
}

func (sa *SetOp) CloneWith(ctx ActionContext) Action {
return &SetOp{
Data: sa.Data,
Path: ctx.TemplateEngine().RenderLenient(sa.Path, ctx.Snapshot()),
}
}

func (ts *TemplateOp) String() string {
return fmt.Sprintf("Template[WriteTo=%s]", ts.WriteTo)
}
Expand Down
32 changes: 0 additions & 32 deletions pipeline/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,38 +161,6 @@ func TestExecutePatchOp(t *testing.T) {
assert.Error(t, New(WithData(gd)).Execute(&ps))
}

func TestExecuteSetOp(t *testing.T) {
var (
ss SetOp
gd dom.ContainerBuilder
err error
)
ss = SetOp{
Data: map[string]interface{}{
"sub1": 123,
},
}
gd = b.Container()
assert.NoError(t, New(WithData(gd)).Execute(&ss))
assert.Equal(t, 123, gd.Lookup("sub1").(dom.Leaf).Value())

ss = SetOp{
Data: map[string]interface{}{
"sub1": 123,
},
Path: "sub0",
}
gd = b.Container()
assert.NoError(t, New(WithData(gd)).Execute(&ss))
assert.Equal(t, 123, gd.Lookup("sub0.sub1").(dom.Leaf).Value())
assert.Contains(t, ss.String(), "sub0")

ss = SetOp{}
err = New(WithData(gd)).Execute(&ss)
assert.Error(t, err)
assert.Equal(t, ErrNoDataToSet, err)
}

func TestExecuteTemplateOp(t *testing.T) {
var (
err error
Expand Down
48 changes: 48 additions & 0 deletions pipeline/set_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"fmt"
)

func (sa *SetOp) String() string {
return fmt.Sprintf("Set[Path=%s]", sa.Path)
}

func (sa *SetOp) Do(ctx ActionContext) error {
gd := ctx.Data()
if sa.Data == nil {
return ErrNoDataToSet
}
data := ctx.Factory().FromMap(sa.Data)
if len(sa.Path) > 0 {
gd.AddValueAt(sa.Path, data)
} else {
for k, v := range data.Children() {
gd.AddValueAt(k, v)
}
}
return nil
}

func (sa *SetOp) CloneWith(ctx ActionContext) Action {
return &SetOp{
Data: sa.Data,
Path: ctx.TemplateEngine().RenderLenient(sa.Path, ctx.Snapshot()),
}
}
55 changes: 55 additions & 0 deletions pipeline/set_op_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/stretchr/testify/assert"
"testing"
)

func TestExecuteSetOp(t *testing.T) {
var (
ss SetOp
gd dom.ContainerBuilder
err error
)
ss = SetOp{
Data: map[string]interface{}{
"sub1": 123,
},
}
gd = b.Container()
assert.NoError(t, New(WithData(gd)).Execute(&ss))
assert.Equal(t, 123, gd.Lookup("sub1").(dom.Leaf).Value())

ss = SetOp{
Data: map[string]interface{}{
"sub1": 123,
},
Path: "sub0",
}
gd = b.Container()
assert.NoError(t, New(WithData(gd)).Execute(&ss))
assert.Equal(t, 123, gd.Lookup("sub0.sub1").(dom.Leaf).Value())
assert.Contains(t, ss.String(), "sub0")

ss = SetOp{}
err = New(WithData(gd)).Execute(&ss)
assert.Error(t, err)
assert.Equal(t, ErrNoDataToSet, err)
}

0 comments on commit d171946

Please sign in to comment.