forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
46 lines (36 loc) · 909 Bytes
/
codec.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
package query
import (
"log"
"github.com/dgraph-io/dgraph/query/graph"
"github.com/gogo/protobuf/proto"
)
// Codec implements the custom codec interface.
type Codec struct{}
// Marshal release the graph.Node pointers after marshalling the response.
func (c *Codec) Marshal(v interface{}) ([]byte, error) {
r, ok := v.(*graph.Response)
if !ok {
log.Fatalf("Invalid type of value: %+v", v)
}
b, err := proto.Marshal(r)
if err != nil {
return []byte{}, err
}
select {
// Passing onto to channel which would put it into the sync pool.
case nodeCh <- r.N:
default:
}
return b, nil
}
// Unmarshal constructs graph.Request from the byte slice.
func (c *Codec) Unmarshal(data []byte, v interface{}) error {
n, ok := v.(*graph.Request)
if !ok {
log.Fatalf("Invalid type of value: %+v", v)
}
return proto.Unmarshal(data, n)
}
func (c *Codec) String() string {
return "query.Codec"
}