-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
145 lines (124 loc) · 3.38 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package utilz
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"gopkg.in/yaml.v2"
)
// TranscodeJSON converts the input to json, and then unmarshals it into the
// destination. The destination must be a pointer.
func TranscodeJSON(input interface{}, destinationPointer interface{}) error {
b, err := json.Marshal(input)
if err != nil {
return fmt.Errorf("error while marshaling input to json: %s", err)
}
err = json.Unmarshal(b, destinationPointer)
if err != nil {
return fmt.Errorf("error while unmarshaling json to destination: %s", err)
}
return nil
}
// TranscodeYAML converts the input to yaml, and then unmarshals it into the
// destination. The destination must be a pointer.
func TranscodeYAML(input interface{}, destinationPointer interface{}) error {
b, err := yaml.Marshal(input)
if err != nil {
return fmt.Errorf("error while marshaling input to yaml: %s", err)
}
err = yaml.Unmarshal(b, destinationPointer)
if err != nil {
return fmt.Errorf("error while unmarshaling yaml to destination: %s", err)
}
return nil
}
func LoadYaml(ptr interface{}, filepath string) error {
yamlFile, err := ioutil.ReadFile(filepath)
if err != nil {
return fmt.Errorf("error while reading config file from %q: %s", filepath, err)
}
err = yaml.Unmarshal(yamlFile, ptr)
if err != nil {
return fmt.Errorf("error while unmarshaling config file: %s", err)
}
return nil
}
func SaveAsYaml(v interface{}, filepath string) error {
file, err := os.OpenFile(filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error while OpenFile: %s", err)
}
defer file.Close()
err = file.Truncate(0)
if err != nil {
return fmt.Errorf("error while file.Truncate: %s", err)
}
_, err = file.Seek(0, 0)
if err != nil {
return fmt.Errorf("error while file.Seek: %s", err)
}
enc := yaml.NewEncoder(file)
err = enc.Encode(v)
if err != nil {
return fmt.Errorf("error while enc.Encode: %s", err)
}
return nil
}
func LoadJSON(ptr interface{}, filepath string) error {
jsonFile, err := ioutil.ReadFile(filepath)
if err != nil {
return fmt.Errorf("error while reading config file from %q: %s", filepath, err)
}
err = json.Unmarshal(jsonFile, ptr)
if err != nil {
return fmt.Errorf("error while unmarshaling config file: %s", err)
}
return nil
}
func SaveAsJSON(v interface{}, filepath string) error {
d, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("error while marshaling config: %s", err)
}
err = ioutil.WriteFile(filepath, d, 0640)
if err != nil {
return fmt.Errorf("error while writing config file: %s", err)
}
return nil
}
func SaveAsIndentedJSON(v interface{}, filepath string) error {
file, err := os.OpenFile(filepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error while OpenFile: %s", err)
}
defer file.Close()
err = file.Truncate(0)
if err != nil {
return fmt.Errorf("error while file.Truncate: %s", err)
}
_, err = file.Seek(0, 0)
if err != nil {
return fmt.Errorf("error while file.Seek: %s", err)
}
enc := json.NewEncoder(file)
enc.SetIndent("", " ")
err = enc.Encode(v)
if err != nil {
return fmt.Errorf("error while enc.Encode: %s", err)
}
return nil
}
func Itoa(i int) string {
return strconv.Itoa(i)
}
func Atoi(s string) (int, error) {
return strconv.Atoi(s)
}
func MustAtoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}