forked from drone/funcmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
39 lines (34 loc) · 914 Bytes
/
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
// 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"
)
// 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
}