MCPcopy
hub / github.com/pocketbase/pocketbase / add

Method add

core/fields_list.go:208–276  ·  view source on GitHub ↗
(pos int, newField Field)

Source from the content-addressed store, hash-verified

206}
207
208func (l *FieldsList) add(pos int, newField Field) {
209 fields := *l
210
211 var replaceByName bool
212 var replaceInPlace bool
213
214 if pos < 0 {
215 replaceInPlace = true
216 pos = len(fields)
217 } else if pos > len(fields) {
218 pos = len(fields)
219 }
220
221 newFieldId := newField.GetId()
222
223 // set default id
224 if newFieldId == "" {
225 replaceByName = true
226
227 baseId := newField.Type() + crc32Checksum(newField.GetName())
228 newFieldId = baseId
229 for i := 2; i < 1000; i++ {
230 if l.GetById(newFieldId) == nil {
231 break // already unique
232 }
233 newFieldId = baseId + strconv.Itoa(i)
234 }
235 newField.SetId(newFieldId)
236 }
237
238 // try to replace existing
239 for i, field := range fields {
240 if replaceByName {
241 if name := newField.GetName(); name != "" && field.GetName() == name {
242 // reuse the original id
243 newField.SetId(field.GetId())
244
245 if replaceInPlace {
246 (*l)[i] = newField
247 return
248 } else {
249 // remove the current field and insert it later at the specific position
250 *l = slices.Delete(*l, i, i+1)
251 if total := len(*l); pos > total {
252 pos = total
253 }
254 break
255 }
256 }
257 } else {
258 if field.GetId() == newFieldId {
259 if replaceInPlace {
260 (*l)[i] = newField
261 return
262 } else {
263 // remove the current field and insert it later at the specific position
264 *l = slices.Delete(*l, i, i+1)
265 if total := len(*l); pos > total {

Callers 3

AddMethod · 0.95
AddAtMethod · 0.95
UnmarshalJSONMethod · 0.95

Calls 8

GetByIdMethod · 0.95
crc32ChecksumFunction · 0.85
InsertMethod · 0.80
GetIdMethod · 0.65
TypeMethod · 0.65
GetNameMethod · 0.65
SetIdMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected