-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
102 lines (88 loc) · 1.67 KB
/
example_test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ag_test
import (
"encoding/json"
"fmt"
"log"
"github.com/bitnine-oss/agensgraph-golang"
)
type knows struct {
ag.Edge
meta struct {
null bool
id ag.GraphId
}
who ag.GraphId
whom ag.GraphId
since yearMonth
}
type knowsBody struct {
Type string
Since json.RawMessage
}
type yearMonth struct {
Year int
Month int
}
func (e knows) String() string {
if e.meta.null {
return "NULL"
} else {
return fmt.Sprintf("%s knows %s since %d, %d", e.who, e.whom, e.since.Month, e.since.Year)
}
}
func (e *knows) SaveEntity(valid bool, core interface{}) error {
e.meta.null = !valid
if !valid {
return nil
}
c, ok := core.(ag.EdgeCore)
if !ok {
return fmt.Errorf("invalid edge core: %T", core)
}
e.meta.id = c.Id
e.who = c.Start
e.whom = c.End
return nil
}
func (e *knows) SaveProperties(b []byte) error {
var body knowsBody
err := json.Unmarshal(b, &body)
if err != nil {
return err
}
switch body.Type {
case "array":
var ym [2]int
err = json.Unmarshal(body.Since, &ym)
if err != nil {
return err
}
e.since.Year, e.since.Month = ym[0], ym[1]
case "object":
err := json.Unmarshal(body.Since, &e.since)
if err != nil {
return err
}
default:
log.Panicf("unknown body type: %q", body.Type)
}
return nil
}
func (e *knows) Scan(src interface{}) error {
return ag.ScanEntity(src, e)
}
func ExampleScanEntity() {
ds := [][]byte{
[]byte(`knows[4.1][3.1,3.2]{"type": "array", "since": [1970, 1]}`),
[]byte(`knows[4.2][3.3,3.4]{"type": "object", "since": {"year": 2009, "month": 10}}`),
}
for _, d := range ds {
var r knows
err := r.Scan(d)
if err != nil {
log.Println(err)
} else {
fmt.Printf("%s\n", r)
}
}
}