-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
60 lines (51 loc) · 992 Bytes
/
utils.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
// Copyright 2024 geebytes. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package golayeredcache
import (
"bytes"
"encoding/gob"
"fmt"
)
type TypeEnum int
const (
// TypeNone is the default type
TypeInt TypeEnum = iota
TypeUint
TypeInt8
TypeUint8
TypeInt16
TypeUint16
TypeInt32
TypeUint32
TypeInt64
TypeUint64
TypeFloat32
TypeFloat64
TypeComplex64
TypeComplex128
TypeNumber
TypeString
TypeBytes
TypeBool
TypeStruct
TypeJson
)
func EncodeToBytes(src interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(src)
if err != nil {
return nil, fmt.Errorf("gob.Encode failed: %w", err)
}
return buf.Bytes(), nil
}
func DecodeFromBytes(src []byte, dst interface{}) error {
buf := bytes.NewBuffer(src)
dec := gob.NewDecoder(buf)
err := dec.Decode(dst)
if err != nil {
return fmt.Errorf("gob.Decode failed: %w", err)
}
return nil
}