-
Notifications
You must be signed in to change notification settings - Fork 16
/
core.go
50 lines (42 loc) · 1002 Bytes
/
core.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
package pgo
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
"os"
)
func isOk(ok bool, msg string, args ...interface{}) {
if !ok {
printError(msg, args)
}
}
func printError(msg string, args ...interface{}) {
fmt.Printf(msg, []interface{}(args)) // strange fix but it didn't work on go version go1.11.4 darwin/amd64 with args...
if os.Getenv("PGO_ENV") != "dev" {
os.Exit(1)
}
}
// Serialize encodes Go code entities to string for e.g.: later storage
func Serialize(val interface{}) (string, error) {
b := bytes.Buffer{}
err := gob.NewEncoder(&b).Encode(val)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
// Unserialize decodes string back into Go code representation
func Unserialize(val string, v interface{}) error {
by, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return err
}
b := new(bytes.Buffer)
b.Write(by)
err = gob.NewDecoder(b).Decode(v)
if err != nil {
return err
}
return nil
}