Decode decodes the given string to a value.
(ctx context.Context, cipher aead.Cipher, encoded string, opts ...CodecOption)
| 74 | |
| 75 | // Decode decodes the given string to a value. |
| 76 | func Decode[T any](ctx context.Context, cipher aead.Cipher, encoded string, opts ...CodecOption) (*T, error) { |
| 77 | plaintext, err := cipher.Decrypt(ctx, encoded, additionalDataFromOpts(opts...)) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | rawBytes, err := gzip.NewReader(bytes.NewReader(plaintext)) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | defer func() { _ = rawBytes.Close() }() |
| 87 | |
| 88 | var val T |
| 89 | if err = json.NewDecoder(rawBytes).Decode(&val); err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | |
| 93 | return &val, nil |
| 94 | } |
| 95 | |
| 96 | // Encode encodes the given value to a string. |
| 97 | func Encode(ctx context.Context, cipher aead.Cipher, val any, opts ...CodecOption) (s string, err error) { |