forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_format.go
100 lines (84 loc) · 2.13 KB
/
data_format.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
96
97
98
99
100
package command
import (
"bytes"
"fmt"
"io"
"text/template"
"github.com/hashicorp/go-msgpack/codec"
)
var (
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)
//DataFormatter is a transformer of the data.
type DataFormatter interface {
// TransformData should return transformed string data.
TransformData(interface{}) (string, error)
}
// DataFormat returns the data formatter specified format.
func DataFormat(format, tmpl string) (DataFormatter, error) {
switch format {
case "json":
if len(tmpl) > 0 {
return nil, fmt.Errorf("json format does not support template option.")
}
return &JSONFormat{}, nil
case "template":
return &TemplateFormat{tmpl}, nil
}
return nil, fmt.Errorf("Unsupported format is specified.")
}
type JSONFormat struct {
}
// TransformData returns JSON format string data.
func (p *JSONFormat) TransformData(data interface{}) (string, error) {
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, jsonHandlePretty)
err := enc.Encode(data)
if err != nil {
return "", err
}
return buf.String(), nil
}
type TemplateFormat struct {
tmpl string
}
// TransformData returns template format string data.
func (p *TemplateFormat) TransformData(data interface{}) (string, error) {
var out io.Writer = new(bytes.Buffer)
if len(p.tmpl) == 0 {
return "", fmt.Errorf("template needs to be specified the golang templates.")
}
t, err := template.New("format").Parse(p.tmpl)
if err != nil {
return "", err
}
err = t.Execute(out, data)
if err != nil {
return "", err
}
return fmt.Sprint(out), nil
}
func Format(json bool, template string, data interface{}) (string, error) {
var format string
if json && len(template) > 0 {
return "", fmt.Errorf("Both json and template formatting are not allowed")
} else if json {
format = "json"
} else if len(template) > 0 {
format = "template"
} else {
return "", fmt.Errorf("no formatting option given")
}
f, err := DataFormat(format, template)
if err != nil {
return "", err
}
out, err := f.TransformData(data)
if err != nil {
return "", fmt.Errorf("Error formatting the data: %s", err)
}
return out, nil
}