Skip to content

Commit

Permalink
Fixed linting issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinit-chauhan committed Oct 16, 2022
1 parent 7b49c7c commit 020214a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
38 changes: 21 additions & 17 deletions libbeat/processors/actions/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import (
)

type appendProcessor struct {
config appendConfig
config appendProcessorConfig
logger *logp.Logger
}

type appendConfig struct {
type appendProcessorConfig struct {
Fields []string `config:"fields"`
TargetField string `config:"target_field"`
Values []interface{} `config:"values"`
Expand All @@ -48,24 +48,24 @@ type appendConfig struct {

func init() {
processors.RegisterPlugin("append_processor",
checks.ConfigChecked(NewAppend,
checks.ConfigChecked(NewAppendProcessor,
checks.RequireFields("target_field"),
),
)
jsprocessor.RegisterPlugin("Append", NewAppend)
jsprocessor.RegisterPlugin("AppendProcessor", NewAppendProcessor)
}

// NewAppend returns a new append_processor processor.
func NewAppend(c *conf.C) (processors.Processor, error) {
config := appendConfig{
// NewAppendProcessor returns a new append_processor processor.
func NewAppendProcessor(c *conf.C) (processors.Processor, error) {
config := appendProcessorConfig{
IgnoreMissing: false,
IgnoreEmptyValues: false,
FailOnError: true,
AllowDuplicate: true,
}
err := c.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("failed to unpack the configuration of append processor: %s", err)
return nil, fmt.Errorf("failed to unpack the configuration of append processor: %w", err)
}

f := &appendProcessor{
Expand All @@ -83,11 +83,13 @@ func (f *appendProcessor) Run(event *beat.Event) (*beat.Event, error) {

err := f.appendValues(f.config.TargetField, f.config.Fields, f.config.Values, event)
if err != nil {
errMsg := fmt.Errorf("failed to append fields in append_processor processor: %s", err)
errMsg := fmt.Errorf("failed to append fields in append_processor processor: %w", err)
f.logger.Debug(errMsg.Error())
if f.config.FailOnError {
event = backup
event.PutValue("error.message", errMsg.Error())
if _, err := event.PutValue("error.message", errMsg.Error()); err != nil {
return nil, fmt.Errorf("failed to append fields in append_processor processor: %w", err)
}
return event, err
}
}
Expand All @@ -112,11 +114,7 @@ func (f *appendProcessor) appendValues(target string, fields []string, values []
if f.config.IgnoreMissing && errors.Is(err, mapstr.ErrKeyNotFound) {
continue
}
return fmt.Errorf("could not fetch value for key: %s, Error: %s", field, err)
}

if f.config.IgnoreMissing && errors.Is(err, mapstr.ErrKeyNotFound) {
continue
return fmt.Errorf("could not fetch value for key: %s, Error: %w", field, err)
}

valArr, ok := val.([]interface{})
Expand All @@ -134,8 +132,14 @@ func (f *appendProcessor) appendValues(target string, fields []string, values []
arr = cleanEmptyValues(arr)
}

event.Delete(target)
event.PutValue(target, arr)
if err := event.Delete(target); err != nil && !errors.Is(err, mapstr.ErrKeyNotFound) {
return fmt.Errorf("unable to delete the target field %s due to error: %w", target, err)
}

if _, err := event.PutValue(target, arr); err != nil {
return fmt.Errorf("unable to put values in the target field %s due to error: %w", target, err)
}

return nil
}

Expand Down
20 changes: 10 additions & 10 deletions libbeat/processors/actions/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_cleanEmptyValues(t *testing.T) {

func Test_appendProcessor_appendValues(t *testing.T) {
type fields struct {
config appendConfig
config appendProcessorConfig
logger *logp.Logger
}
type args struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ func Test_appendProcessor_appendValues(t *testing.T) {
},
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"field"},
TargetField: "target",
},
Expand All @@ -114,7 +114,7 @@ func Test_appendProcessor_appendValues(t *testing.T) {
},
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"field"},
TargetField: "target",
},
Expand All @@ -135,7 +135,7 @@ func Test_appendProcessor_appendValues(t *testing.T) {
},
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
IgnoreEmptyValues: false,
IgnoreMissing: false,
AllowDuplicate: true,
Expand All @@ -158,7 +158,7 @@ func Test_appendProcessor_appendValues(t *testing.T) {
},
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
IgnoreEmptyValues: false,
IgnoreMissing: true,
AllowDuplicate: true,
Expand All @@ -183,7 +183,7 @@ func Test_appendProcessor_appendValues(t *testing.T) {

func Test_appendProcessor_Run(t *testing.T) {
type fields struct {
config appendConfig
config appendProcessorConfig
logger *logp.Logger
}
type args struct {
Expand All @@ -200,7 +200,7 @@ func Test_appendProcessor_Run(t *testing.T) {
description: "positive flow",
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"array-one", "array-two", "concrete-field"},
TargetField: "target",
Values: []interface{}{"value1", "value2"},
Expand Down Expand Up @@ -235,7 +235,7 @@ func Test_appendProcessor_Run(t *testing.T) {
description: "test for nested field",
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"array.one", "array.two", "concrete-field"},
TargetField: "target",
Values: []interface{}{"value1", "value2"},
Expand Down Expand Up @@ -274,7 +274,7 @@ func Test_appendProcessor_Run(t *testing.T) {
description: "remove empty values form output - 'ignore_empty_values: true'",
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"array-one", "array-two", "concrete-field"},
TargetField: "target",
Values: []interface{}{"value1", nil, "value2", "", nil},
Expand Down Expand Up @@ -309,7 +309,7 @@ func Test_appendProcessor_Run(t *testing.T) {
description: "append value of a missing field with 'ignore_missing: false'",
fields: fields{
logger: log,
config: appendConfig{
config: appendProcessorConfig{
Fields: []string{"missing-field"},
TargetField: "target",
IgnoreMissing: false,
Expand Down

0 comments on commit 020214a

Please sign in to comment.