You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.4 KiB

// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name properties.go
// Created by DEV
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package values
import "activething.com/go/gogo/core/opts"
type (
Properties struct {
atts PropertiesAtt
parent *Properties
props map[string]any
}
PropertiesWalkFnc func(key string, value any) bool
)
func NewProperties (options ...opts.OptionFnc[Properties]) *Properties {
return opts.Apply(&Properties{
props: make(map[string]any),
}, options ...)
}
func (p *Properties) Get (name string) (any,bool) {
v,h := p.props[name]
if h {
return v,true
}
if p.parent != nil {
return p.parent.Get(name)
}
return nil,false
}
func (p *Properties) Set (nme string, val interface{}) error {
if p.atts.IsReadOnly() {
return G.Errors.PropertiesReadOnly
}
if _,hs := p.props[nme]; !hs && p.IsFixed() {
return Errors.PropertiesFixed
}
p.props[nme]=val
return nil
}
func (p *Properties) Del (nme string) error {
if p.ref != nil {
if _,hs := p.props[nme]; hs && p.IsReadOnly() {
return Errors.PropertiesReadOnly
}
delete(p.props,nme)
return nil
}
if _,hs := p.props[nme]; hs {
if p.IsFixed() {
return Errors.PropertiesFixed
}
if p.IsReadOnly() {
return Errors.PropertiesReadOnly
}
}
delete(p.props,nme)
return nil
}
// Properties.Map
// Desc:
//
// Prms: mfn,
//
func (p *Properties) Map (mfn PropertiesMapFnc) {
for ky,vl := range p.props {
if !mfn(ky,vl,false) {
return
}
}
if p.ref != nil {
for ky,vl := range p.ref.props {
if _,hs := p.props[ky]; !hs {
if !mfn(ky, vl, true) {
return
}
}
}
}
}

Powered by TurnKey Linux.