From 03b5f9694f32b45d2d79df13bbebc0921e2ee4d4 Mon Sep 17 00:00:00 2001 From: StewartJingga Date: Fri, 5 Aug 2022 13:25:07 +0700 Subject: [PATCH] fix(enrich)!: enrich processor not working (#390) * fix(enrich): enrich processor not working * test(plugin): add test for nil config) --- plugins/base_plugin.go | 4 ++++ plugins/base_plugin_test.go | 10 ++++++++++ plugins/processors/enrich/processor.go | 13 +++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/plugins/base_plugin.go b/plugins/base_plugin.go index 2446aed01..37979c9e7 100644 --- a/plugins/base_plugin.go +++ b/plugins/base_plugin.go @@ -25,6 +25,10 @@ func (p *BasePlugin) Info() Info { // Validate checks if the given options is valid for the plugin. func (p *BasePlugin) Validate(config Config) error { + if p.configRef == nil { + return nil + } + return buildConfig(config.RawConfig, p.configRef) } diff --git a/plugins/base_plugin_test.go b/plugins/base_plugin_test.go index 134b65b63..7cc7cdd04 100644 --- a/plugins/base_plugin_test.go +++ b/plugins/base_plugin_test.go @@ -37,6 +37,16 @@ func TestBasePluginInfo(t *testing.T) { } func TestBasePluginValidate(t *testing.T) { + t.Run("should not return error if config is nil", func(t *testing.T) { + basePlugin := plugins.NewBasePlugin(plugins.Info{}, nil) + err := basePlugin.Validate(plugins.Config{ + URNScope: "test-scope", + RawConfig: map[string]interface{}{}, + }) + + assert.NoError(t, err) + }) + t.Run("should return InvalidConfigError if config is invalid", func(t *testing.T) { invalidConfig := struct { FieldA string `validate:"required"` diff --git a/plugins/processors/enrich/processor.go b/plugins/processors/enrich/processor.go index 50a7b1266..ccf46c2e7 100644 --- a/plugins/processors/enrich/processor.go +++ b/plugins/processors/enrich/processor.go @@ -14,17 +14,22 @@ import ( //go:embed README.md var summary string +type Config struct { + Attributes map[string]interface{} `mapstructure:"attributes" validate:"required"` +} + // Processor work in a list of data type Processor struct { plugins.BasePlugin - config map[string]interface{} + config Config logger log.Logger } var sampleConfig = ` # Enrichment configuration - # fieldA: valueA - # fieldB: valueB` + # attributes: + # fieldA: valueA + # fieldB: valueB` var info = plugins.Info{ Description: "Append custom fields to records", @@ -68,7 +73,7 @@ func (p *Processor) process(record models.Record) (models.Metadata, error) { customProps := utils.GetCustomProperties(data) // update custom properties using value from config - for key, value := range p.config { + for key, value := range p.config.Attributes { stringVal, ok := value.(string) if ok { customProps[key] = stringVal