-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoding.go
74 lines (63 loc) · 1.62 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
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// license that can be found in the LICENSE file.
package funcmap
import (
"encoding/base64"
"encoding/json"
"html/template"
"sigs.k8s.io/yaml"
)
// EncodeBase64 returns the base64 encoding of s.
func EncodeBase64(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString([]byte(ss)), nil
}
// DecodeBase64 returns the base64 decoding of s.
func DecodeBase64(s interface{}) (string, error) {
ss, err := toStringE(s)
if err != nil {
return "", err
}
d, err := base64.StdEncoding.DecodeString(ss)
return string(d), err
}
// EncodeJSON returns the JSON encoding of v.
func EncodeJSON(v interface{}) (template.HTML, error) {
b, err := json.Marshal(v)
if err != nil {
return "", err
}
return template.HTML(b), nil
}
// DecodeJSON returns the JSON decoding of s.
func DecodeJSON(s interface{}) (map[string]interface{}, error) {
v := map[string]interface{}{}
data, err := toStringE(s)
if err != nil {
return v, err
}
err = json.Unmarshal([]byte(data), &v)
return v, err
}
// EncodeYAML returns the YAML encoding of v.
func EncodeYAML(v interface{}) (template.HTML, error) {
b, err := yaml.Marshal(v)
if err != nil {
return "", err
}
return template.HTML(b), nil
}
// DecodeYAML returns the YAML decoding of s.
func DecodeYAML(s interface{}) (map[string]interface{}, error) {
v := map[string]interface{}{}
data, err := toStringE(s)
if err != nil {
return v, err
}
err = yaml.Unmarshal([]byte(data), &v)
return v, err
}