github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/collection/collection_obj.go (about) 1 /* 2 * Copyright (c) 2021-present unTill Pro, Ltd. 3 * 4 * @author Michael Saigachenko 5 */ 6 7 package collection 8 9 import ( 10 "fmt" 11 12 "github.com/voedger/voedger/pkg/goutils/logger" 13 "github.com/voedger/voedger/pkg/istructs" 14 ) 15 16 // Implements IElement 17 type collectionObject struct { 18 istructs.IRecord 19 children []*collectionObject 20 rawRecords []istructs.IRecord 21 } 22 23 func newCollectionObject(rec istructs.IRecord) *collectionObject { 24 return &collectionObject{ 25 IRecord: rec, 26 children: make([]*collectionObject, 0), 27 } 28 } 29 30 func (me *collectionObject) addElementsForParent(list []istructs.IRecord, parent istructs.RecordID) { 31 for _, r := range list { 32 if r.Parent() == parent { 33 child := newCollectionObject(r) 34 me.children = append(me.children, child) 35 if logger.IsVerbose() { 36 logger.Verbose(fmt.Sprintf("collectionElem ID: %d: added ID: %d, QName: %s", me.ID(), r.ID(), r.QName().String())) 37 } 38 child.addElementsForParent(list, r.ID()) 39 } 40 } 41 } 42 43 func (me *collectionObject) handleRawRecords() { 44 me.addElementsForParent(me.rawRecords, me.ID()) 45 } 46 47 func (me *collectionObject) addRawRecord(rec istructs.IRecord) { 48 if me.rawRecords == nil { 49 me.rawRecords = make([]istructs.IRecord, 1) 50 me.rawRecords[0] = rec 51 } else { 52 me.rawRecords = append(me.rawRecords, rec) 53 } 54 } 55 56 // Children in given container 57 func (me *collectionObject) Children(container string, cb func(istructs.IObject)) { 58 for i := range me.children { 59 c := me.children[i] 60 if (container == "") || (c.Container() == container) { 61 cb(c) 62 } 63 } 64 } 65 66 // First level qname-s 67 func (me *collectionObject) Containers(cb func(container string)) { 68 iterated := make(map[string]bool) 69 for i := range me.children { 70 c := me.children[i] 71 cont := c.Container() 72 if _, ok := iterated[cont]; !ok { 73 iterated[cont] = true 74 cb(cont) 75 } 76 } 77 } 78 79 func (me *collectionObject) AsRecord() istructs.IRecord { 80 return me 81 }