-
Notifications
You must be signed in to change notification settings - Fork 3
/
decode.go
40 lines (33 loc) · 792 Bytes
/
decode.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
package exgob
import (
"bytes"
"encoding/gob"
"github.com/ImSingee/go-ex/exstrings"
)
func Decode[T any](p []byte) (*T, error) {
v, err := DecodeToValue[T](p)
if err != nil {
return nil, err
}
return &v, nil
}
func DecodeString[T any](s string) (*T, error) {
return Decode[T](exstrings.UnsafeToBytes(s))
}
func DecodeToValue[T any](p []byte) (T, error) {
var v T
err := DecodeTo(p, &v)
if err != nil {
return v, err
}
return v, nil
}
func DecodeStringToValue[T any](s string) (T, error) {
return DecodeToValue[T](exstrings.UnsafeToBytes(s))
}
func DecodeTo[T any](p []byte, target *T) error {
return gob.NewDecoder(bytes.NewReader(p)).Decode(target)
}
func DecodeStringTo[T any](s string, target *T) error {
return DecodeTo[T](exstrings.UnsafeToBytes(s), target)
}