forked from zhihu/norm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
94 lines (79 loc) · 1.64 KB
/
model.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
package norm
import (
"fmt"
"github.com/zhihu/norm/v3/constants"
)
type (
ITag interface {
TagName() string
}
IVertex interface {
ITag
GetVid() interface{}
GetPolicy() constants.Policy
}
// vid is int or string
VModel struct {
Vid interface{} `norm:"-"`
Policy constants.Policy `norm:"-"`
}
)
var _ IVertex = new(VModel)
func (v VModel) TagName() string {
// TODO return model name with snake style
panic("")
}
func (v VModel) GetVid() interface{} {
return v.Vid
}
func (v VModel) GetPolicy() constants.Policy {
return v.Policy
}
type (
IEdge interface {
// 返回边的名称
EdgeName() string
GetVidSrc() interface{}
GetVidSrcPolicy() constants.Policy
GetVidDst() interface{}
GetVidDstPolicy() constants.Policy
}
EModel struct {
Src interface{} `norm:"-"`
SrcPolicy constants.Policy `norm:"-"`
Dst interface{} `norm:"-"`
DstPolicy constants.Policy `norm:"-"`
}
)
var _ IEdge = new(EModel)
func (v EModel) EdgeName() string {
panic("")
}
func (v EModel) GetVidSrc() interface{} {
return v.Src
}
func (v EModel) GetVidSrcPolicy() constants.Policy {
return v.SrcPolicy
}
func (v EModel) GetVidDst() interface{} {
return v.Dst
}
func (v EModel) GetVidDstPolicy() constants.Policy {
return v.DstPolicy
}
func GetVidWithPolicy(vid interface{}, policy constants.Policy) string {
vidStr := ""
switch vid := vid.(type) {
case int, int8, int32, int64, float32, float64:
vidStr = fmt.Sprint(vid)
case string:
vidStr = "'" + vid + "'"
default:
vidStr += "'" + fmt.Sprint(vid) + "'"
}
switch policy {
case constants.PolicyHash:
vidStr = "hash(" + vidStr + ")"
}
return vidStr
}