-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
53 lines (43 loc) · 1000 Bytes
/
graph.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
package model
import (
"bytes"
"encoding/json"
"fmt"
"time"
. "github.com/Madongming/resource-tree/global"
)
// DB recorder.
type DBResourceRelationship struct {
ID int `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
SourceResourceNodeID int `gorm:"index"`
TargetResourceNodeID int `gorm:"index"`
}
type ResourceEdge struct {
Source int `json:"source"`
Target int `json:"target"`
}
type Graph struct {
Nodes []*ResourceNode `json:"nodes"`
Edges []*ResourceEdge `json:"edges"`
}
func (rr *DBResourceRelationship) Create() error {
return DB().Create(rr).Error
}
func (rr *DBResourceRelationship) Delete() error {
return DB().Delete(rr).Error
}
func (g *Graph) String() string {
b, err := json.Marshal(g)
if err != nil {
return fmt.Sprintf("%#v", g)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", "\t")
if err != nil {
return fmt.Sprintf("%#v", g)
}
return out.String()
}