Skip to content

Commit

Permalink
fix(enrich)!: enrich processor not working (#390)
Browse files Browse the repository at this point in the history
* fix(enrich): enrich processor not working

* test(plugin): add test for nil config)
  • Loading branch information
StewartJingga authored Aug 5, 2022
1 parent d5ba47c commit 03b5f96
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions plugins/base_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions plugins/base_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
13 changes: 9 additions & 4 deletions plugins/processors/enrich/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 03b5f96

Please sign in to comment.