-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
56 lines (44 loc) · 1.21 KB
/
format.go
File metadata and controls
56 lines (44 loc) · 1.21 KB
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
package gnfmt
import "fmt"
// Format sets available output formats
type Format int
const (
// FormatNone is for cases when format is not set yet.
FormatNone Format = iota
// CSV sets output to comma-separated values.
CSV
// CompactJSON sets output into one-liner JSON.
CompactJSON
// PrettyJSON sets output into easier to read JSON with new lines and
// indentations.
PrettyJSON
// TSV sets output to tab-separated values.
TSV
)
var formatStringMap = map[string]Format{
"csv": CSV, "compact": CompactJSON, "pretty": PrettyJSON, "tsv": TSV,
}
var formatMap = map[Format]string{
FormatNone: "",
CSV: "CSV",
CompactJSON: "compact JSON",
PrettyJSON: "pretty JSON",
TSV: "TSV",
}
// String representation of a format.
func (f Format) String() string {
return formatMap[f]
}
// NewFormat is a constructor that converts a string into a corresponding format.
// If string cannot be converted, the constructor returns an error and
// and FormatNone format.
func NewFormat(s string) (Format, error) {
if f, ok := formatStringMap[s]; ok {
return f, nil
}
err := fmt.Errorf(
"cannot convert '%s' to format, use 'csv', 'tsv', 'compact' or 'pretty' as input",
s,
)
return FormatNone, err
}