forked from raystack/meteor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor.go
95 lines (78 loc) · 2.03 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package enrich
import (
"context"
_ "embed"
"github.com/goto/meteor/models"
v1beta2 "github.com/goto/meteor/models/gotocompany/assets/v1beta2"
"github.com/goto/meteor/plugins"
"github.com/goto/meteor/registry"
"github.com/goto/meteor/utils"
"github.com/goto/salt/log"
)
//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 Config
logger log.Logger
}
var sampleConfig = `
# Enrichment configuration
# attributes:
# fieldA: valueA
# fieldB: valueB`
var info = plugins.Info{
Description: "Append custom fields to records",
SampleConfig: sampleConfig,
Summary: summary,
Tags: []string{"processor", "transform"},
}
// New create a new processor
func New(logger log.Logger) *Processor {
p := &Processor{
logger: logger,
}
p.BasePlugin = plugins.NewBasePlugin(info, &p.config)
return p
}
// Process processes the data
func (p *Processor) Init(ctx context.Context, config plugins.Config) error {
return p.BasePlugin.Init(ctx, config)
}
// Process processes the data
func (p *Processor) Process(_ context.Context, src models.Record) (models.Record, error) {
result, err := p.process(src)
if err != nil {
return models.Record{}, err
}
return models.NewRecord(result), nil
}
func (p *Processor) process(record models.Record) (*v1beta2.Asset, error) {
data := record.Data()
p.logger.Debug("enriching record", "record", data.Urn)
customProps := utils.GetAttributes(data)
// update custom properties using value from config
for key, value := range p.config.Attributes {
stringVal, ok := value.(string)
if ok {
customProps[key] = stringVal
}
}
// save custom properties
result, err := utils.SetAttributes(data, customProps)
if err != nil {
return data, err
}
return result, nil
}
func init() {
if err := registry.Processors.Register("enrich", func() plugins.Processor {
return New(plugins.GetLog())
}); err != nil {
return
}
}