forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
picker.go
36 lines (31 loc) · 711 Bytes
/
picker.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
package pick
import (
"github.com/compose/transporter/function"
"github.com/compose/transporter/log"
"github.com/compose/transporter/message"
)
var (
_ function.Function = &picker{}
)
func init() {
function.Add(
"pick",
func() function.Function {
return &picker{}
},
)
}
type picker struct {
Fields []string `json:"fields"`
}
func (p *picker) Apply(msg message.Msg) (message.Msg, error) {
log.With("msg", msg).Debugln("picking...")
pluckedMsg := map[string]interface{}{}
for _, k := range p.Fields {
if v, ok := msg.Data().AsMap()[k]; ok {
pluckedMsg[k] = v
}
}
log.With("msg", pluckedMsg).Debugln("...picked")
return message.From(msg.OP(), msg.Namespace(), pluckedMsg), nil
}