-
Notifications
You must be signed in to change notification settings - Fork 3
/
base64.go
55 lines (47 loc) · 1.66 KB
/
base64.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
package coze
import (
"encoding/base64"
"fmt"
"strings"
)
// B64 is a Coze wrapper for encoding/base64. B64 is useful for marshaling and
// unmarshalling structs. B64's underlying type is []byte and is represented in
// JSON as "RFC 4648 base 64 URI canonical with padding truncated" (b64ut).
//
// When converting integers or other types to B64, `nil` is encoded as "" and
// zero is encoded as "AA".
type B64 []byte
// UnmarshalJSON implements json.Unmarshaler.
func (t *B64) UnmarshalJSON(b []byte) error {
// JSON.Unmarshal returns b encapsulated in quotes which is invalid base64 characters.
s, err := base64.URLEncoding.Strict().WithPadding(base64.NoPadding).DecodeString(strings.Trim(string(b), "\""))
if err != nil {
return err
}
*t = B64(s)
return nil
}
// MarshalJSON implements json.Marshaler. Error is always nil.
func (t B64) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", t)), nil
}
// String implements fmt.Stringer. Use with `%s`, `%v`, `%+v`.
func (t B64) String() string {
return base64.URLEncoding.Strict().WithPadding(base64.NoPadding).EncodeToString([]byte(t))
}
// GoString implements fmt.GoStringer. Use with `%#v` (not %s or %+v).
func (t B64) GoString() string {
return base64.URLEncoding.Strict().WithPadding(base64.NoPadding).EncodeToString([]byte(t))
}
// Decode decodes a b64ut string.
func Decode(b64 string) (B64, error) {
return base64.URLEncoding.Strict().WithPadding(base64.NoPadding).DecodeString(b64)
}
// MustDecode decodes b64ut and panics on error.
func MustDecode(b64 string) B64 {
b, err := base64.URLEncoding.Strict().WithPadding(base64.NoPadding).DecodeString(b64)
if err != nil {
panic(err)
}
return b
}