/
gong_graph.go
148 lines (103 loc) · 2.99 KB
/
gong_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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// generated code - do not edit
package models
func IsStaged[Type Gongstruct](stage *StageStruct, instance *Type) (ok bool) {
switch target := any(instance).(type) {
// insertion point for stage
case *A:
ok = stage.IsStagedA(target)
case *B:
ok = stage.IsStagedB(target)
default:
_ = target
}
return
}
// insertion point for stage per struct
func (stage *StageStruct) IsStagedA(a *A) (ok bool) {
_, ok = stage.As[a]
return
}
func (stage *StageStruct) IsStagedB(b *B) (ok bool) {
_, ok = stage.Bs[b]
return
}
// StageBranch stages instance and apply StageBranch on all gongstruct instances that are
// referenced by pointers or slices of pointers of the insance
//
// the algorithm stops along the course of graph if a vertex is already staged
func StageBranch[Type Gongstruct](stage *StageStruct, instance *Type) {
switch target := any(instance).(type) {
// insertion point for stage branch
case *A:
stage.StageBranchA(target)
case *B:
stage.StageBranchB(target)
default:
_ = target
}
}
// insertion point for stage branch per struct
func (stage *StageStruct) StageBranchA(a *A) {
// check if instance is already staged
if IsStaged(stage, a) {
return
}
a.Stage(stage)
//insertion point for the staging of instances referenced by pointers
if a.B != nil {
StageBranch(stage, a.B)
}
//insertion point for the staging of instances referenced by slice of pointers
for _, _b := range a.Bs {
StageBranch(stage, _b)
}
}
func (stage *StageStruct) StageBranchB(b *B) {
// check if instance is already staged
if IsStaged(stage, b) {
return
}
b.Stage(stage)
//insertion point for the staging of instances referenced by pointers
//insertion point for the staging of instances referenced by slice of pointers
}
// UnstageBranch stages instance and apply UnstageBranch on all gongstruct instances that are
// referenced by pointers or slices of pointers of the insance
//
// the algorithm stops along the course of graph if a vertex is already staged
func UnstageBranch[Type Gongstruct](stage *StageStruct, instance *Type) {
switch target := any(instance).(type) {
// insertion point for unstage branch
case *A:
stage.UnstageBranchA(target)
case *B:
stage.UnstageBranchB(target)
default:
_ = target
}
}
// insertion point for unstage branch per struct
func (stage *StageStruct) UnstageBranchA(a *A) {
// check if instance is already staged
if ! IsStaged(stage, a) {
return
}
a.Unstage(stage)
//insertion point for the staging of instances referenced by pointers
if a.B != nil {
UnstageBranch(stage, a.B)
}
//insertion point for the staging of instances referenced by slice of pointers
for _, _b := range a.Bs {
UnstageBranch(stage, _b)
}
}
func (stage *StageStruct) UnstageBranchB(b *B) {
// check if instance is already staged
if ! IsStaged(stage, b) {
return
}
b.Unstage(stage)
//insertion point for the staging of instances referenced by pointers
//insertion point for the staging of instances referenced by slice of pointers
}