Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] Remove helper.OutputIDs type #14543

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/stanza/operator/helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {

type helpersConfig struct {
BasicConfig `mapstructure:",squash"`
Writer WriterConfig `mapstructure:"writer"`
Time TimeParser `mapstructure:"time"`
Severity SeverityConfig `mapstructure:"severity"`
Scope ScopeNameParser `mapstructure:"scope"`
Expand All @@ -36,6 +37,7 @@ type helpersConfig struct {
func newHelpersConfig() *helpersConfig {
return &helpersConfig{
BasicConfig: NewBasicConfig(helpersTestType, helpersTestType),
Writer: NewWriterConfig(helpersTestType, helpersTestType),
Time: NewTimeParser(),
Severity: NewSeverityConfig(),
Scope: NewScopeNameParser(),
Expand Down
13 changes: 13 additions & 0 deletions pkg/stanza/operator/helper/testdata/writer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
string:
type: helpers_test
writer:
output: test
slice:
type: helpers_test
writer:
output: [test1, test2]
invalid:
type: helpers_test
writer:
output:
foo: bar
70 changes: 3 additions & 67 deletions pkg/stanza/operator/helper/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package helper // import "github.com/open-telemetry/opentelemetry-collector-cont

import (
"context"
"encoding/json"
"fmt"

"go.uber.org/zap"
Expand All @@ -34,8 +33,8 @@ func NewWriterConfig(operatorID, operatorType string) WriterConfig {

// WriterConfig is the configuration of a writer operator.
type WriterConfig struct {
BasicConfig `mapstructure:",squash" yaml:",inline"`
OutputIDs OutputIDs `mapstructure:"output" json:"output" yaml:"output"`
BasicConfig `mapstructure:",squash"`
OutputIDs []string `mapstructure:"output"`
}

// Build will build a writer operator from the config.
Expand All @@ -54,7 +53,7 @@ func (c WriterConfig) Build(logger *zap.SugaredLogger) (WriterOperator, error) {
// WriterOperator is an operator that can write to other operators.
type WriterOperator struct {
BasicOperator
OutputIDs OutputIDs
OutputIDs []string
OutputOperators []operator.Operator
}

Expand Down Expand Up @@ -119,66 +118,3 @@ func (w *WriterOperator) findOperator(operators []operator.Operator, operatorID
}
return nil, false
}

// OutputIDs is a collection of operator IDs used as outputs.
type OutputIDs []string

// UnmarshalJSON will unmarshal a string or array of strings to OutputIDs.
func (o *OutputIDs) UnmarshalJSON(bytes []byte) error {
var value interface{}
err := json.Unmarshal(bytes, &value)
if err != nil {
return err
}

ids, err := NewOutputIDsFromInterface(value)
if err != nil {
return err
}

*o = ids
return nil
}

// UnmarshalYAML will unmarshal a string or array of strings to OutputIDs.
func (o *OutputIDs) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value interface{}
err := unmarshal(&value)
if err != nil {
return err
}

ids, err := NewOutputIDsFromInterface(value)
if err != nil {
return err
}

*o = ids
return nil
}

// NewOutputIDsFromInterface creates a new OutputIDs object from an interface
func NewOutputIDsFromInterface(value interface{}) (OutputIDs, error) {
if str, ok := value.(string); ok {
return OutputIDs{str}, nil
}

if array, ok := value.([]interface{}); ok {
return NewOutputIDsFromArray(array)
}

return nil, fmt.Errorf("value is not of type string or string array")
}

// NewOutputIDsFromArray creates a new OutputIDs object from an array
func NewOutputIDsFromArray(array []interface{}) (OutputIDs, error) {
ids := OutputIDs{}
for _, rawValue := range array {
strValue, ok := rawValue.(string)
if !ok {
return nil, fmt.Errorf("value in array is not of type string")
}
ids = append(ids, strValue)
}
return ids, nil
}
119 changes: 35 additions & 84 deletions pkg/stanza/operator/helper/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ package helper

import (
"context"
"encoding/json"
"path/filepath"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
)

Expand All @@ -40,7 +40,7 @@ func TestWriterConfigMissingOutput(t *testing.T) {

func TestWriterConfigValidBuild(t *testing.T) {
config := WriterConfig{
OutputIDs: OutputIDs{"output"},
OutputIDs: []string{"output"},
BasicConfig: BasicConfig{
OperatorType: "testtype",
},
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestWriterSetOutputsMissing(t *testing.T) {
output1 := &testutil.Operator{}
output1.On("ID").Return("output1")
writer := WriterOperator{
OutputIDs: OutputIDs{"output2"},
OutputIDs: []string{"output2"},
}

err := writer.SetOutputs([]operator.Operator{output1})
Expand All @@ -105,7 +105,7 @@ func TestWriterSetOutputsInvalid(t *testing.T) {
output1.On("ID").Return("output1")
output1.On("CanProcess").Return(false)
writer := WriterOperator{
OutputIDs: OutputIDs{"output1"},
OutputIDs: []string{"output1"},
}

err := writer.SetOutputs([]operator.Operator{output1})
Expand All @@ -121,90 +121,41 @@ func TestWriterSetOutputsValid(t *testing.T) {
output2.On("ID").Return("output2")
output2.On("CanProcess").Return(true)
writer := WriterOperator{
OutputIDs: OutputIDs{"output1", "output2"},
OutputIDs: []string{"output1", "output2"},
}

err := writer.SetOutputs([]operator.Operator{output1, output2})
require.NoError(t, err)
require.Equal(t, []operator.Operator{output1, output2}, writer.Outputs())
}

func TestUnmarshalJSONString(t *testing.T) {
bytes := []byte("{\"output\":\"test\"}")
var config WriterConfig
err := json.Unmarshal(bytes, &config)
require.NoError(t, err)
require.Equal(t, OutputIDs{"test"}, config.OutputIDs)
}

func TestUnmarshalJSONArray(t *testing.T) {
bytes := []byte("{\"output\":[\"test1\",\"test2\"]}")
var config WriterConfig
err := json.Unmarshal(bytes, &config)
require.NoError(t, err)
require.Equal(t, OutputIDs{"test1", "test2"}, config.OutputIDs)
}

func TestUnmarshalJSONInvalidValue(t *testing.T) {
bytes := []byte("..")
var config WriterConfig
err := json.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid character")
}

func TestUnmarshalJSONInvalidString(t *testing.T) {
bytes := []byte("{\"output\": true}")
var config WriterConfig
err := json.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "value is not of type string or string array")
}

func TestUnmarshalJSONInvalidArray(t *testing.T) {
bytes := []byte("{\"output\":[\"test1\", true]}")
var config WriterConfig
err := json.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "value in array is not of type string")
}

func TestUnmarshalYAMLString(t *testing.T) {
bytes := []byte("output: test")
var config WriterConfig
err := yaml.Unmarshal(bytes, &config)
require.NoError(t, err)
require.Equal(t, OutputIDs{"test"}, config.OutputIDs)
}

func TestUnmarshalYAMLArray(t *testing.T) {
bytes := []byte("output: [test1, test2]")
var config WriterConfig
err := yaml.Unmarshal(bytes, &config)
require.NoError(t, err)
require.Equal(t, OutputIDs{"test1", "test2"}, config.OutputIDs)
}

func TestUnmarshalYAMLInvalidValue(t *testing.T) {
bytes := []byte("..")
var config WriterConfig
err := yaml.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "cannot unmarshal")
}

func TestUnmarshalYAMLInvalidString(t *testing.T) {
bytes := []byte("output: true")
var config WriterConfig
err := yaml.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "value is not of type string or string array")
}

func TestUnmarshalYAMLInvalidArray(t *testing.T) {
bytes := []byte("output: [test1, true]")
var config WriterConfig
err := yaml.Unmarshal(bytes, &config)
require.Error(t, err)
require.Contains(t, err.Error(), "value in array is not of type string")
func TestUnmarshalWriterConfig(t *testing.T) {
operatortest.ConfigUnmarshalTests{
DefaultConfig: newHelpersConfig(),
TestsFile: filepath.Join(".", "testdata", "writer.yaml"),
Tests: []operatortest.ConfigUnmarshalTest{
{
Name: "string",
Expect: func() *helpersConfig {
c := newHelpersConfig()
c.Writer = NewWriterConfig(helpersTestType, helpersTestType)
c.Writer.OutputIDs = []string{"test"}
return c
}(),
},
{
Name: "slice",
Expect: func() *helpersConfig {
c := newHelpersConfig()
c.Writer = NewWriterConfig(helpersTestType, helpersTestType)
c.Writer.OutputIDs = []string{"test1", "test2"}
return c
}(),
},
{
Name: "invalid",
ExpectErr: true,
},
},
}.Run(t)
}
10 changes: 5 additions & 5 deletions pkg/stanza/operator/transformer/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func NewConfigWithID(operatorID string) *Config {
// Config is the configuration of a router operator
type Config struct {
helper.BasicConfig `mapstructure:",squash" yaml:",inline"`
Routes []*RouteConfig `mapstructure:"routes" json:"routes" yaml:"routes"`
Default helper.OutputIDs `mapstructure:"default" json:"default" yaml:"default"`
Routes []*RouteConfig `mapstructure:"routes" json:"routes" yaml:"routes"`
Default []string `mapstructure:"default" json:"default" yaml:"default"`
}

// RouteConfig is the configuration of a route on a router operator
type RouteConfig struct {
helper.AttributerConfig `mapstructure:",squash" yaml:",inline"`
Expression string `mapstructure:"expr" json:"expr" yaml:"expr"`
OutputIDs helper.OutputIDs `mapstructure:"output" json:"output" yaml:"output"`
Expression string `mapstructure:"expr" json:"expr" yaml:"expr"`
OutputIDs []string `mapstructure:"output" json:"output" yaml:"output"`
}

// Build will build a router operator from the supplied configuration
Expand Down Expand Up @@ -110,7 +110,7 @@ type Transformer struct {
type Route struct {
helper.Attributer
Expression *vm.Program
OutputIDs helper.OutputIDs
OutputIDs []string
OutputOperators []operator.Operator
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/transformer/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTransformer(t *testing.T) {
name string
input *entry.Entry
routes []*RouteConfig
defaultOutput helper.OutputIDs
defaultOutput []string
expectedCounts map[string]int
expectedAttributes map[string]interface{}
}{
Expand Down