/
gob.go
59 lines (47 loc) · 1.24 KB
/
gob.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
package zygo
import (
"bytes"
"encoding/gob"
"fmt"
)
func GobEncodeFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
if len(args) != 1 {
return SexpNull, WrongNargs
}
h, isHash := args[0].(*SexpHash)
if !isHash {
return SexpNull, fmt.Errorf("gob argument must be a hash or defmap")
}
// fill the go shadow struct
_, err := ToGoFunction(env, "togo", []Sexp{h})
if err != nil {
return SexpNull, fmt.Errorf("error converting object to Go struct: '%s'", err)
}
// serialize to gob
var gobBytes bytes.Buffer
enc := gob.NewEncoder(&gobBytes)
err = enc.Encode(h.GoShadowStruct)
if err != nil {
return SexpNull, fmt.Errorf("gob encode error: '%s'", err)
}
return &SexpRaw{Val: gobBytes.Bytes()}, nil
}
func GobDecodeFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
if len(args) != 1 {
return SexpNull, WrongNargs
}
raw, isRaw := args[0].(*SexpRaw)
if !isRaw {
return SexpNull, fmt.Errorf("ungob argument must be raw []byte")
}
rawBuf := bytes.NewBuffer(raw.Val)
dec := gob.NewDecoder(rawBuf)
var iface interface{}
err := dec.Decode(iface)
if err != nil {
return SexpNull, fmt.Errorf("gob decode error: '%s'", err)
}
// TODO convert to hash
panic("not done yet!")
//return SexpNull, nil
}