-
Notifications
You must be signed in to change notification settings - Fork 14
/
transformer.go
85 lines (73 loc) · 3.03 KB
/
transformer.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
package sync
import (
"strings"
"github.com/charmbracelet/log"
"github.com/inovex/CalendarSync/internal/config"
"github.com/inovex/CalendarSync/internal/models"
"github.com/inovex/CalendarSync/internal/transformation"
)
// Transformer applies a well-defined transformation to an event. Multiple transformers may be concatenated.
type Transformer interface {
NamedComponent
// Transform applies the Transformer logic to the event and returns the changed Event.
Transform(source models.Event, sink models.Event) (models.Event, error)
}
// TransformEvent will transform the given event by applying every transformer given.
// The final transformed event is returned.
func TransformEvent(event models.Event, transformers ...Transformer) models.Event {
transformedEvent := models.NewSyncEvent(event)
for i := 0; i < len(transformers); i++ {
transformedEvent, _ = transformers[i].Transform(event, transformedEvent)
}
return transformedEvent
}
var (
// transformerConfigMapping maps "name" values from the config to a default object of the matching Transformer.
transformerConfigMapping = map[string]Transformer{
"ReplaceTitle": &transformation.ReplaceTitle{NewTitle: "[CalendarSync Event]"},
"PrefixTitle": &transformation.PrefixTitle{Prefix: ""},
"KeepTitle": &transformation.KeepTitle{},
"KeepMeetingLink": &transformation.KeepMeetingLink{},
"KeepDescription": &transformation.KeepDescription{},
"KeepLocation": &transformation.KeepLocation{},
"KeepAttendees": &transformation.KeepAttendees{UseEmailAsDisplayName: false},
"KeepReminders": &transformation.KeepReminders{},
}
// this is the order of the transformers in which they get evaluated
// from first transformer to last
transformerOrder = []string{
"KeepAttendees",
"KeepLocation",
"KeepReminders",
"KeepDescription",
"KeepMeetingLink",
"KeepTitle",
"PrefixTitle",
"ReplaceTitle",
}
)
// TransformerFactory can build all configured transformers from the config file
func TransformerFactory(configuredTransformers []config.Transformer) (loadedTransformers []Transformer) {
for _, configuredTransformer := range configuredTransformers {
if _, nameExists := transformerConfigMapping[configuredTransformer.Name]; !nameExists {
log.Warnf("unknown transformer: %s, skipping...", configuredTransformer.Name)
continue
}
// load the default Transformer for the configured name and initialize it based on the config
transformerDefault := transformerConfigMapping[configuredTransformer.Name]
loadedTransformers = append(loadedTransformers, TransformerFromConfig(transformerDefault, configuredTransformer.Config))
}
var sortedAndLoadedTransformer []Transformer
for _, name := range transformerOrder {
for _, v := range loadedTransformers {
if strings.EqualFold(name, v.Name()) {
sortedAndLoadedTransformer = append(sortedAndLoadedTransformer, v)
}
}
}
return sortedAndLoadedTransformer
}
func TransformerFromConfig(transformer Transformer, config config.CustomMap) Transformer {
autoConfigure(transformer, config)
return transformer
}