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
/
infobox.go
191 lines (165 loc) · 3.82 KB
/
infobox.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package layer
import (
"errors"
"fmt"
"github.com/reearth/reearth-backend/pkg/builtin"
"github.com/reearth/reearth-backend/pkg/property"
)
type Infobox struct {
property PropertyID
fields []*InfoboxField
// for checking duplication
ids map[InfoboxFieldID]struct{}
}
func NewInfobox(fields []*InfoboxField, p PropertyID) *Infobox {
infobox := Infobox{
property: p,
fields: make([]*InfoboxField, len(fields)),
ids: make(map[InfoboxFieldID]struct{}, len(fields)),
}
for i, f := range fields {
if f == nil {
continue
}
infobox.fields[i] = f
infobox.ids[f.ID()] = struct{}{}
}
return &infobox
}
func (i *Infobox) Property() PropertyID {
return i.property
}
func (i *Infobox) PropertyRef() *PropertyID {
if i == nil {
return nil
}
pid := i.property
return &pid
}
func (i *Infobox) Fields() []*InfoboxField {
if i == nil {
return nil
}
return append([]*InfoboxField{}, i.fields...)
}
func (i *Infobox) Field(field InfoboxFieldID) *InfoboxField {
for _, f := range i.fields {
if f.ID() == field {
return f
}
}
return nil
}
func (i *Infobox) FieldAt(index int) *InfoboxField {
if i == nil || index < 0 || len(i.fields) <= index {
return nil
}
return i.fields[index]
}
func (i *Infobox) FieldsByPlugin(pid PluginID, eid *PluginExtensionID) []*InfoboxField {
if i == nil {
return nil
}
fields := make([]*InfoboxField, 0, len(i.fields))
for _, f := range i.fields {
if f.Plugin().Equal(pid) && (eid == nil || f.Extension() == *eid) {
fields = append(fields, f)
}
}
return fields
}
func (i *Infobox) Has(id InfoboxFieldID) bool {
_, ok := i.ids[id]
return ok
}
func (i *Infobox) Count() int {
return len(i.fields)
}
func (i *Infobox) Add(field *InfoboxField, index int) {
l := len(i.fields)
if index < 0 || l <= index {
index = l
}
id := field.ID()
if i.Has(id) {
return
}
i.fields = append(i.fields[:index], append([]*InfoboxField{field}, i.fields[index:]...)...)
i.ids[id] = struct{}{}
}
func (i *Infobox) Move(field InfoboxFieldID, toIndex int) {
for fromIndex, f := range i.fields {
if f.ID() == field {
i.MoveAt(fromIndex, toIndex)
return
}
}
}
func (i *Infobox) MoveAt(fromIndex int, toIndex int) {
l := len(i.fields)
if fromIndex < 0 || l <= fromIndex {
return
}
if toIndex < 0 || l <= toIndex {
toIndex = l - 1
}
f := i.fields[fromIndex]
i.fields = append(i.fields[:fromIndex], i.fields[fromIndex+1:]...)
newSlice := make([]*InfoboxField, toIndex+1)
copy(newSlice, i.fields[:toIndex])
newSlice[toIndex] = f
i.fields = append(newSlice, i.fields[toIndex:]...)
}
func (i *Infobox) Remove(field InfoboxFieldID) {
for index, f := range i.fields {
if f.ID() == field {
i.RemoveAt(index)
return
}
}
}
func (i *Infobox) RemoveAllByPlugin(pid PluginID, eid *PluginExtensionID) []PropertyID {
if i == nil {
return nil
}
var properties []PropertyID
for j := 0; j < len(i.fields); j++ {
if i.fields[j].plugin.Equal(pid) && (eid == nil || i.fields[j].Extension() == *eid) {
properties = append(properties, i.fields[j].Property())
i.fields = append(i.fields[:j], i.fields[j+1:]...)
j--
}
}
return properties
}
func (i *Infobox) RemoveAt(index int) {
l := len(i.fields)
if index < 0 || l <= index {
index = l
}
f := i.fields[index]
if index == l {
i.fields = i.fields[:index]
} else {
i.fields = append(i.fields[:index], i.fields[index+1:]...)
}
delete(i.ids, f.ID())
}
func (i *Infobox) ValidateProperties(pm property.Map) error {
if i == nil || pm == nil {
return nil
}
lp := pm[i.property]
if lp == nil {
return errors.New("property does not exist")
}
if !lp.Schema().Equal(builtin.PropertySchemaIDInfobox) {
return errors.New("property has a invalid schema")
}
for i, f := range i.fields {
if err := f.ValidateProperty(pm); err != nil {
return fmt.Errorf("field[%d](%s): %w", i, f.ID(), err)
}
}
return nil
}