forked from reddit/baseplate.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
74 lines (65 loc) · 1.51 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
package secrets
import (
"encoding/base64"
"encoding/json"
)
// Encoding represents the Encoding used to encode a secret.
type Encoding int
const (
// IdentityEncoding indicates no encoding beyond JSON itself.
IdentityEncoding Encoding = iota
// Base64Encoding indicates that the secret is base64 encoded.
Base64Encoding
)
const (
identityEncodingJSON = `"identity"`
identityEncodingStr = "identity"
base64EncodingJSON = `"base64"`
base64EncodingStr = "base64"
)
// MarshalJSON returns a JSON string representation of the encoding.
func (e Encoding) MarshalJSON() ([]byte, error) {
switch e {
case IdentityEncoding:
return []byte(identityEncodingJSON), nil
case Base64Encoding:
return []byte(base64EncodingJSON), nil
default:
return nil, ErrInvalidEncoding
}
}
// UnmarshalJSON unmarshals the given JSON data into an encoding.
func (e *Encoding) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case identityEncodingStr, "":
*e = IdentityEncoding
case base64EncodingStr:
*e = Base64Encoding
default:
return ErrInvalidEncoding
}
return nil
}
func (e Encoding) decodeValue(value string) (Secret, error) {
if value == "" {
return nil, nil
}
switch e {
case IdentityEncoding:
return Secret(value), nil
default:
data, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return nil, err
}
return Secret(data), nil
}
}
var (
_ json.Marshaler = Encoding(0)
_ json.Unmarshaler = (*Encoding)(nil)
)