Skip to content

Commit

Permalink
fix: Process Jsonnet Errors (#11)
Browse files Browse the repository at this point in the history
* fix: foreach & process libsonnet

* test: multi-line JSON

* docs: add comments
  • Loading branch information
jshlbrd committed Aug 1, 2022
1 parent 6cab3f7 commit 9507c83
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions config/process.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
type: 'delete',
settings: {
condition: { operator: condition_operator, inspectors: condition_inspectors},
input: input,
input_key: input,
},
},
domain(input, output, _function,
Expand Down Expand Up @@ -101,7 +101,7 @@
type: 'expand',
settings: {
condition: { operator: condition_operator, inspectors: condition_inspectors},
input: input,
input_key: input,
},
},
flatten(input, output,
Expand Down Expand Up @@ -157,7 +157,7 @@
settings: {
options: { value: value },
condition: { operator: condition_operator, inspectors: condition_inspectors},
output: output,
output_key: output,
},
},
lambda(input, output, _function,
Expand Down
10 changes: 10 additions & 0 deletions internal/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ var getTests = []struct {
test: []byte(`{"foo":false}`),
expected: false,
},
{
name: "multi-line",
key: "foo",
test: []byte(`{
"foo":
"string"
}
`),
expected: "string",
},
}

func TestGetJson(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions process/for_each.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package process
import (
"context"
"fmt"
"strings"

"github.com/brexhq/substation/condition"
"github.com/brexhq/substation/internal/config"
Expand Down Expand Up @@ -102,17 +103,21 @@ func (p ForEach) Byte(ctx context.Context, data []byte) ([]byte, error) {
return nil, fmt.Errorf("byter settings %+v: %w", p, ProcessorInvalidSettings)
}

// processor settings loaded via Jsonnet may create invalid keys such as
// `foo.bar.` -- the trailing dot creates an invalid path, so it is trimmed
if _, ok := p.Options.Processor.Settings["input_key"]; ok {
p.Options.Processor.Settings["input_key"] = p.Options.Processor.Type + "." + p.Options.Processor.Settings["input_key"].(string)
} else {
p.Options.Processor.Settings["input_key"] = p.Options.Processor.Type
}
p.Options.Processor.Settings["input_key"] = strings.TrimSuffix(p.Options.Processor.Settings["input_key"].(string), ".")

if _, ok := p.Options.Processor.Settings["output_key"]; ok {
p.Options.Processor.Settings["output_key"] = p.Options.Processor.Type + "." + p.Options.Processor.Settings["output_key"].(string)
} else {
p.Options.Processor.Settings["output_key"] = p.Options.Processor.Type
}
p.Options.Processor.Settings["output_key"] = strings.TrimSuffix(p.Options.Processor.Settings["output_key"].(string), ".")

byter, err := ByterFactory(p.Options.Processor)
if err != nil {
Expand Down

0 comments on commit 9507c83

Please sign in to comment.