Skip to content

Commit

Permalink
feat: process.Replace allow replacing with nothing (#42)
Browse files Browse the repository at this point in the history
This loosens input checks for the replace processor
to omitting a "New" value effectively deleting target
substrings.
  • Loading branch information
shellcromancer committed Dec 8, 2022
1 parent d10c653 commit 7aeeb44
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions process/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (p Replace) ApplyBatch(ctx context.Context, capsules []config.Capsule) ([]c
// Apply processes encapsulated data with the Replace processor.
func (p Replace) Apply(ctx context.Context, capsule config.Capsule) (config.Capsule, error) {
// error early if required options are missing
if p.Options.Old == "" || p.Options.New == "" {
return capsule, fmt.Errorf("process replace: options %+v: %v", p.Options, errMissingRequiredOptions)
if p.Options.Old == "" {
return capsule, fmt.Errorf("process replace: options %+v: %w", p.Options, errMissingRequiredOptions)
}

// default to replace all
Expand Down
41 changes: 41 additions & 0 deletions process/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package process
import (
"bytes"
"context"
"errors"
"testing"

"github.com/brexhq/substation/config"
Expand All @@ -29,6 +30,20 @@ var replaceTests = []struct {
[]byte(`{"foo":"baz"}`),
nil,
},
{
"json delete",
Replace{
Options: ReplaceOptions{
Old: "z",
New: "",
},
InputKey: "foo",
OutputKey: "foo",
},
[]byte(`{"foo":"fizz"}`),
[]byte(`{"foo":"fi"}`),
nil,
},
{
"data",
Replace{
Expand All @@ -41,6 +56,29 @@ var replaceTests = []struct {
[]byte(`baz`),
nil,
},
{
"data delete",
Replace{
Options: ReplaceOptions{
Old: "r",
New: "",
},
},
[]byte(`bar`),
[]byte(`ba`),
nil,
},
{
"data",
Replace{
Options: ReplaceOptions{
New: "z",
},
},
[]byte(`bar`),
[]byte(`baz`),
errMissingRequiredOptions,
},
}

func TestReplace(t *testing.T) {
Expand All @@ -52,6 +90,9 @@ func TestReplace(t *testing.T) {

result, err := test.proc.Apply(ctx, capsule)
if err != nil {
if errors.Is(err, test.err) {
continue
}
t.Error(err)
}

Expand Down

0 comments on commit 7aeeb44

Please sign in to comment.