/
gong_slices.go
84 lines (75 loc) · 2.79 KB
/
gong_slices.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
// generated code - do not edit
package models
// EvictInOtherSlices allows for adherance between
// the gong association model and go.
//
// Says you have a Astruct struct with a slice field "anarrayofb []*Bstruct"
//
// go allows many Astruct instance to have the anarrayofb field that have the
// same pointers. go slices are MANY-MANY association.
//
// With gong it is only ZERO-ONE-MANY associations, a Bstruct can be pointed only
// once by an Astruct instance through a given field. This follows the requirement
// that gong is suited for full stack programming and therefore the association
// is encoded as a reverse pointer (not as a joint table). In gong, a named struct
// is translated in a table and each table is a named struct.
//
// EvictInOtherSlices removes the fields instances from other
// fields of other instance
//
// Note : algo is in O(N)log(N) of nb of Astruct and Bstruct instances
func EvictInOtherSlices[OwningType PointerToGongstruct, FieldType PointerToGongstruct](
stage *StageStruct,
owningInstance OwningType,
sliceField []FieldType,
fieldName string) {
// create a map of the field elements
setOfFieldInstances := make(map[FieldType]any, 0)
for _, fieldInstance := range sliceField {
setOfFieldInstances[fieldInstance] = true
}
switch owningInstanceInfered := any(owningInstance).(type) {
// insertion point
case *A:
// insertion point per field
if fieldName == "Bs" {
// walk all instances of the owning type
for _instance := range *GetGongstructInstancesSetFromPointerType[OwningType](stage) {
if any(_instance).(*A) != owningInstanceInfered {
_inferedTypeInstance := any(_instance).(*A)
reference := make([]FieldType, 0)
targetFieldSlice := any(_inferedTypeInstance.Bs).([]FieldType)
copy(targetFieldSlice, reference)
_inferedTypeInstance.Bs = _inferedTypeInstance.Bs[0:]
for _, fieldInstance := range reference {
if _, ok := setOfFieldInstances[any(fieldInstance).(FieldType)]; !ok {
_inferedTypeInstance.Bs =
append(_inferedTypeInstance.Bs, any(fieldInstance).(*B))
}
}
}
}
}
case *B:
// insertion point per field
default:
_ = owningInstanceInfered // to avoid "declared and not used" error if no named struct has slices
}
}
// ComputeReverseMaps computes the reverse map, for all intances, for all slice to pointers field
// Its complexity is in O(n)O(p) where p is the number of pointers
func (stage *StageStruct) ComputeReverseMaps() {
// insertion point per named struct
// Compute reverse map for named struct A
// insertion point per field
clear(stage.A_Bs_reverseMap)
stage.A_Bs_reverseMap = make(map[*B]*A)
for a := range stage.As {
_ = a
for _, _b := range a.Bs {
stage.A_Bs_reverseMap[_b] = a
}
}
// Compute reverse map for named struct B
// insertion point per field
}