Files
aego/schema/bag.go
T
sanspie 5eb1832907
ci-platforms / darwin amd64 smoke (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Failing after 15m28s
ci / release (push) Skipped
ci-platforms / windows amd64 smoke (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
ci / checks (push) Successful in 7m47s
Complete Stage 12 for v0.12.0
2026-11-05 11:46:53 +03:00

72 lines
1.4 KiB
Go

package schema
import "slices"
type Property struct {
Name string
Value Value
}
type PropertyBag struct {
Props []Property
}
func (b *PropertyBag) Get(name string) (Value, bool) {
for i := range b.Props {
if b.Props[i].Name == name {
return b.Props[i].Value, true
}
}
return Value{}, false
}
func (b *PropertyBag) Set(name string, v Value) {
for i := range b.Props {
if b.Props[i].Name == name {
b.Props[i].Value = v
return
}
}
b.Props = append(b.Props, Property{Name: name, Value: v})
}
func (b *PropertyBag) Delete(name string) bool {
for i := range b.Props {
if b.Props[i].Name == name {
b.Props = append(b.Props[:i], b.Props[i+1:]...)
return true
}
}
return false
}
func (b PropertyBag) Clone() PropertyBag {
out := PropertyBag{Props: slices.Clone(b.Props)}
for i := range out.Props {
out.Props[i].Value.cloneReferences()
}
return out
}
// The outer values have already been copied. Only nested mutable payloads
// need new storage. Scalars, strings and asset/node IDs are values.
func (v *Value) cloneReferences() {
if v.List != nil {
v.List = cloneValues(v.List)
}
if v.Obj != nil {
v.Obj = &Object{Type: v.Obj.Type, Fields: cloneValues(v.Obj.Fields)}
}
if v.Cv.Keys != nil {
v.Cv = v.Cv.Clone()
}
}
func cloneValues(values []Value) []Value {
out := slices.Clone(values)
for i := range out {
out[i].cloneReferences()
}
return out
}