This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
schema_group_list.go
146 lines (125 loc) · 2.55 KB
/
schema_group_list.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
package property
type SchemaGroupList struct {
groups []*SchemaGroup
}
func NewSchemaGroupList(p []*SchemaGroup) *SchemaGroupList {
sgl := &SchemaGroupList{
groups: append(p[:0:0], p...),
}
if len(sgl.duplicatedGroups()) > 0 {
return nil
}
return sgl
}
func (p *SchemaGroupList) Len() int {
if p == nil {
return 0
}
return len(p.groups)
}
func (p *SchemaGroupList) Groups() []*SchemaGroup {
if p == nil {
return nil
}
return append(p.groups[:0:0], p.groups...)
}
func (p *SchemaGroupList) Fields() []*SchemaField {
if p == nil {
return nil
}
fields := []*SchemaField{}
for _, g := range p.groups {
fields = append(fields, g.Fields()...)
}
return fields
}
func (p *SchemaGroupList) GroupAndFields() []SchemaGroupAndField {
if p == nil {
return nil
}
fields := []SchemaGroupAndField{}
for _, g := range p.groups {
for _, f := range g.Fields() {
fields = append(fields, SchemaGroupAndField{Group: g, Field: f})
}
}
return fields
}
func (p *SchemaGroupList) Field(id FieldID) *SchemaField {
if p == nil {
return nil
}
for _, g := range p.groups {
if f := g.Field(id); f != nil {
return f
}
}
return nil
}
func (p *SchemaGroupList) Group(id SchemaGroupID) *SchemaGroup {
if p == nil {
return nil
}
for _, f := range p.groups {
if f.ID() == id {
return f
}
}
return nil
}
func (p *SchemaGroupList) GroupByField(id FieldID) *SchemaGroup {
if p == nil {
return nil
}
for _, f := range p.groups {
if f.HasField(id) {
return f
}
}
return nil
}
func (p *SchemaGroupList) GroupAndField(f FieldID) *SchemaGroupAndField {
if p == nil {
return nil
}
for _, g := range p.groups {
if gf := g.Field(f); gf != nil {
return &SchemaGroupAndField{Group: g, Field: gf}
}
}
return nil
}
func (s *SchemaGroupList) duplicatedGroups() []SchemaGroupID {
if s == nil {
return nil
}
var duplicated []SchemaGroupID
ids := map[SchemaGroupID]struct{}{}
for _, f := range s.Groups() {
i := f.ID()
if _, ok := ids[i]; ok {
duplicated = append(duplicated, i)
}
ids[i] = struct{}{}
}
return duplicated
}
type SchemaGroupAndField struct {
Group *SchemaGroup
Field *SchemaField
}
func (gf SchemaGroupAndField) IsEmpty() bool {
return gf.Group == nil && gf.Field == nil
}
func (gf SchemaGroupAndField) Pointer() *Pointer {
if gf.Group == nil && gf.Field == nil {
return nil
}
return NewPointer(gf.Group.ID().Ref(), nil, gf.Field.ID().Ref())
}
func (f SchemaGroupAndField) SchemaFieldPointer() SchemaFieldPointer {
return SchemaFieldPointer{
SchemaGroup: f.Group.ID(),
Field: f.Field.ID(),
}
}