forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprettify.go
More file actions
42 lines (35 loc) · 730 Bytes
/
prettify.go
File metadata and controls
42 lines (35 loc) · 730 Bytes
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
package pretty
import (
"encoding/json"
"strings"
"github.com/compose/mejson"
"github.com/compose/transporter/function"
"github.com/compose/transporter/log"
"github.com/compose/transporter/message"
)
const (
defaultIndent = 2
)
var (
defaultPrettifier = &prettify{Spaces: defaultIndent}
)
func init() {
function.Add(
"pretty",
func() function.Function {
return defaultPrettifier
},
)
}
type prettify struct {
Spaces int `json:"spaces"`
}
func (p *prettify) Apply(msg message.Msg) (message.Msg, error) {
d, _ := mejson.Unmarshal(msg.Data())
b, _ := json.Marshal(d)
if p.Spaces > 0 {
b, _ = json.MarshalIndent(d, "", strings.Repeat(" ", p.Spaces))
}
log.Infof("\n%s", string(b))
return msg, nil
}