/
export_outputFormat.go
58 lines (47 loc) · 1.27 KB
/
export_outputFormat.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
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/
package export
import (
"errors"
"strings"
)
////////////////////////////////////////////////////////////////////////
// OutputFormat enumeration support
// OutputFormat is the format to use for output file
type OutputFormat int
// local variables to map to / from the enumeration
var outputFormatStrings []string
var outputFormats []OutputFormat
// add string as new mapped enumeration
func ciota(s string) OutputFormat {
var result = OutputFormat(len(outputFormatStrings) - 1)
outputFormatStrings = append(outputFormatStrings, s)
outputFormats = append(outputFormats, result)
return result
}
// create the enumerations
var (
FormatUnset = ciota("")
FormatJSON = ciota("json")
FormatYAML = ciota("yaml")
)
// Convert OutputFormat to string
func (f OutputFormat) String() string {
return outputFormatStrings[int(f)]
}
// ToOutputFormat converts a string to an OutputFormat enum
func ToOutputFormat(s string) (OutputFormat, error) {
// sanity
if len(strings.TrimSpace(s)) == 0 {
return FormatUnset, errors.New("empty string")
}
// scan
for i, v := range outputFormatStrings {
if v == s {
return outputFormats[i], nil
}
}
// not found
return FormatUnset, errors.New("invalid value")
}