-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.go
66 lines (50 loc) · 1.22 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
package main
import (
"fmt"
"log"
"github.com/coreos/go-semver/semver"
"github.com/j-and-j-global/wordpress-parser"
)
var (
provenanceParserMap = PPVs{
PPV{wordpressParser.Parser{}, "wordpress", semver.Must(semver.NewVersion("1.0.0"))},
}
)
// PPV represents a Parser Provenance Version mapping
type PPV struct {
Parser Parser
Provenance string
Version *semver.Version
}
type PPVs []PPV
func (ppvs PPVs) Find(provenance, version string) (p Parser, err error) {
v, err := semver.NewVersion(version)
if err != nil {
return
}
for _, ppv := range ppvs {
if ppv.Provenance == provenance && ppv.Version.Equal(*v) {
p = ppv.Parser
return
}
}
err = fmt.Errorf("Could not find a parser for %q %s", provenance, version)
return
}
func (ppvs PPVs) TransformerLoop(input, output chan MessageWithEnvelope) (err error) {
for m := range input {
parser, err := ppvs.Find(m.Provenance, m.Version)
if err != nil {
log.Printf("Transformer Loop: %+v", err)
continue
}
out, err := parser.Parse([]byte(m.Message.Body))
if err != nil {
log.Printf("Transformer Loop: %+v", err)
continue
}
m.Message.Body = string(out)
output <- m
}
return fmt.Errorf("Transformer Loop: input channel closed")
}