-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializable_secret.go
32 lines (26 loc) · 979 Bytes
/
serializable_secret.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
package secrecy
import "encoding/json"
// SecretExposer define any secret wrapper types that can expose it's underlying
// secret of type T.
// Don't store returned value and prefer passing SecretExposer itself if needed.
type SecretExposer[T any] interface {
ExposeSecret() T
}
// NewSerializableSecret wraps secret and return a SerializableSecret that implements
// json.Marshaler interface.
func NewSerializableSecret[S any, T SecretExposer[S]](secret T) SerializableSecret[S, T] {
return SerializableSecret[S, T]{secret}
}
// SerializableSecret is a serializable wrapper around a SecretExposer.
type SerializableSecret[S any, T SecretExposer[S]] struct {
secret T
}
// ExposeSecret implements SecretExposer.
func (ss SerializableSecret[S, T]) ExposeSecret() S {
return ss.secret.ExposeSecret()
}
// MarshalJSON implements json.Marshaler.
func (ss SerializableSecret[S, T]) MarshalJSON() ([]byte, error) {
secret := ss.ExposeSecret()
return json.Marshal(secret)
}