diff --git a/pipeline/actions.go b/pipeline/actions.go index 5317273..32a397d 100644 --- a/pipeline/actions.go +++ b/pipeline/actions.go @@ -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) } diff --git a/pipeline/actions_test.go b/pipeline/actions_test.go index 81d2434..09c2c8c 100644 --- a/pipeline/actions_test.go +++ b/pipeline/actions_test.go @@ -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 diff --git a/pipeline/set_op.go b/pipeline/set_op.go new file mode 100644 index 0000000..a6484e7 --- /dev/null +++ b/pipeline/set_op.go @@ -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()), + } +} diff --git a/pipeline/set_op_test.go b/pipeline/set_op_test.go new file mode 100644 index 0000000..d1787d8 --- /dev/null +++ b/pipeline/set_op_test.go @@ -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) +}