-
Notifications
You must be signed in to change notification settings - Fork 206
/
encoding.go
116 lines (100 loc) · 2.93 KB
/
encoding.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package encoding
import (
"bytes"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"get.porter.sh/porter/pkg"
"github.com/carolynvs/aferox"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)
const (
Yaml = "yaml"
Json = "json"
Toml = "toml"
)
// MarshalFile encodes the specified struct to a file.
// Supported file extensions are: yaml, yml, json, and toml.
func MarshalFile(fs aferox.Aferox, path string, in interface{}) error {
format := strings.TrimPrefix(filepath.Ext(path), ".")
data, err := Marshal(format, in)
if err != nil {
return err
}
return fs.WriteFile(path, data, pkg.FileModeWritable)
}
// MarshalYaml converts the input to yaml.
func MarshalYaml(in interface{}) ([]byte, error) {
return Marshal(Yaml, in)
}
// MarshalJson converts the input struct to json.
func MarshalJson(in interface{}) ([]byte, error) {
return Marshal(Json, in)
}
// MarshalToml converts the input to toml.
func MarshalToml(in interface{}) ([]byte, error) {
return Marshal(Toml, in)
}
// Marshal a struct to the specified format.
// Supported formats are: yaml, json, and toml.
func Marshal(format string, in interface{}) (data []byte, err error) {
switch format {
case "json":
data, err = json.MarshalIndent(in, "", " ")
case "yaml", "yml":
w := bytes.Buffer{}
encoder := yaml.NewEncoder(&w)
encoder.SetIndent(2)
err = encoder.Encode(in)
data = w.Bytes()
case "toml":
data, err = toml.Marshal(in)
default:
return nil, newUnsupportedFormatError(format)
}
if err != nil {
return nil, fmt.Errorf("error marshaling to %s: %w", format, err)
}
return data, nil
}
// Unmarshal from the specified file into a struct.
// Supported file extensions are: yaml, yml, json, and toml.
func UnmarshalFile(fs aferox.Aferox, path string, out interface{}) error {
data, err := fs.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file %s: %w", path, err)
}
format := strings.TrimPrefix(filepath.Ext(path), ".")
return Unmarshal(format, data, out)
}
// UnmarshalYaml converts the input yaml to a struct.
func UnmarshalYaml(data []byte, out interface{}) error {
return Unmarshal(Yaml, data, out)
}
// UnmarshalJson converts the input json to a struct.
func UnmarshalJson(data []byte, out interface{}) error {
return Unmarshal(Json, data, out)
}
// UnmarshalToml converts the input toml to a struct.
func UnmarshalToml(data []byte, out interface{}) error {
return Unmarshal(Toml, data, out)
}
// Unmarshal from the specified format into a struct.
// Supported formats are: yaml, json, and toml.
func Unmarshal(format string, data []byte, out interface{}) error {
switch format {
case "json":
return json.Unmarshal(data, out)
case "yaml", "yml":
return yaml.Unmarshal(data, out)
case "toml":
return toml.Unmarshal(data, out)
default:
return newUnsupportedFormatError(format)
}
}
func newUnsupportedFormatError(format string) error {
return fmt.Errorf("unsupported format %s. Supported formats are: yaml, json and toml", format)
}