parent
207e58c9fe
commit
3fca3ef79d
@ -0,0 +1,184 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name bfield_list.go
|
||||
// Created by DEV
|
||||
// Modified 19/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 bfields
|
||||
|
||||
import (
|
||||
"activething.com/go/gogo/core/opts"
|
||||
"activething.com/go/gogo/core/slices"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
BFields struct {
|
||||
BFieldsAtt
|
||||
fields []BField
|
||||
}
|
||||
|
||||
BFieldsWalkFnc func(index int, field *BField) bool
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewBFields (options ...opts.OptionErrFnc[BFields]) (*BFields,error) {
|
||||
fs,er := opts.ApplyAndCheck(&BFields{},options...)
|
||||
if er != nil {
|
||||
return nil,er
|
||||
}
|
||||
if fs.IsReadOnly() {
|
||||
|
||||
}
|
||||
return fs,nil
|
||||
}
|
||||
|
||||
|
||||
func (f BFields) Get(nme []byte) *BField {
|
||||
if ix := findField(f.fields, 0, nme); ix != slices.NotFound {
|
||||
f.get(ix)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (f BFields) get(index int) *BField {
|
||||
if f.IsReadOnly() {
|
||||
vl := f.fields[index]
|
||||
return &vl
|
||||
}
|
||||
return &f.fields[index]
|
||||
}
|
||||
|
||||
|
||||
func (f *BFields) Add(nme, val []byte) error {
|
||||
if f.IsFixed() {
|
||||
return G.ErrFixed
|
||||
}
|
||||
if ix := findField(f.fields, 0, nme); ix != slices.NotFound {
|
||||
if !f.IsMultiple() {
|
||||
return G.ErrDuplicated
|
||||
}
|
||||
}
|
||||
fl, er := NewBField(nme, val)
|
||||
if er != nil {
|
||||
return er
|
||||
}
|
||||
f.fields = append(f.fields, *fl)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *BFields) Del(nme []byte) error {
|
||||
if f.IsFixed() {
|
||||
return G.ErrFixed
|
||||
}
|
||||
if f.IsReadOnly() {
|
||||
return G.ErrReadOnly
|
||||
}
|
||||
fm := 0
|
||||
ix := -1
|
||||
ln := len(f.fields)
|
||||
for {
|
||||
if ix = findField(f.fields, fm, nme); ix == slices.NotFound {
|
||||
break
|
||||
}
|
||||
f.fields[ix] = f.fields[ln-1]
|
||||
f.fields = f.fields[:ln-1]
|
||||
ln -= 1
|
||||
if !f.IsMultiple() || ix == ln-1 {
|
||||
break
|
||||
}
|
||||
fm = ix
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *BFields) Upsert(nme, val []byte) error {
|
||||
if len(nme) == 0 {
|
||||
return nil
|
||||
}
|
||||
if f.IsReadOnly() {
|
||||
return G.ErrReadOnly
|
||||
}
|
||||
if ix := findField(f.fields, 0, nme); ix != -1 {
|
||||
f.fields[ix].SetValue(val)
|
||||
return nil
|
||||
}
|
||||
if f.IsFixed() {
|
||||
return G.ErrFixed
|
||||
}
|
||||
fl, er := NewBField(nme, val)
|
||||
if er != nil {
|
||||
return er
|
||||
}
|
||||
f.fields = append(f.fields, *fl)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f BFields) Len() int {
|
||||
return len(f.fields) }
|
||||
|
||||
|
||||
func (f BFields) Has(nme []byte) bool {
|
||||
return findField(f.fields, 0, nme) != slices.NotFound }
|
||||
|
||||
|
||||
func (f BFields) Count(nme []byte) int {
|
||||
rs := 0
|
||||
for _, fl := range f.fields {
|
||||
if bytes.Equal(fl.name, nme) {
|
||||
rs++
|
||||
}
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
||||
|
||||
func (f BFields) Walk(walker BFieldsWalkFnc) {
|
||||
for ln, ix := len(f.fields), 0; ix < ln; ix++ {
|
||||
if !walker(ix, f.get(ix)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func findField(list []BField, from int, name []byte) int {
|
||||
for ln, ix := len(list), from; ix < ln; ix++ {
|
||||
if bytes.Equal(list[ix].name, name) {
|
||||
return ix
|
||||
}
|
||||
}
|
||||
return slices.NotFound
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name bfields_att.go
|
||||
// Created by DEV
|
||||
// Modified 19/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 bfields
|
||||
|
||||
|
||||
const(
|
||||
|
||||
BFieldsAttReadOnly BFieldsAtt = 1 << iota
|
||||
BFieldsAttFixed
|
||||
BFieldsAttMultiple
|
||||
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
BFieldsAtt uint8
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (a BFieldsAtt) IsReadOnly() bool {
|
||||
return a&BFieldsAttReadOnly != 0 }
|
||||
|
||||
|
||||
func (a BFieldsAtt) IsFixed() bool {
|
||||
return a&BFieldsAttFixed != 0 }
|
||||
|
||||
|
||||
func (a BFieldsAtt) IsMultiple() bool {
|
||||
return a&BFieldsAttMultiple != 0 }
|
||||
@ -0,0 +1,37 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name bfields_ops.go
|
||||
// Created by DEV
|
||||
// Modified 19/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 bfields
|
||||
|
||||
import "activething.com/go/gogo/core/opts"
|
||||
|
||||
func WithField(nme, val []byte) opts.OptionErrFnc[BFields] {
|
||||
return func(f *BFields) error { return f.Add(nme, val) }}
|
||||
|
||||
|
||||
func WithAtts(atts BFieldsAtt) opts.OptionErrFnc[BFields] {
|
||||
return func(f *BFields) error {
|
||||
f.BFieldsAtt = atts
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
module chars
|
||||
|
||||
|
||||
go 1.20
|
||||
@ -0,0 +1,26 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_print.go
|
||||
// Created by DEV
|
||||
// Modified 19/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 errs
|
||||
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Feb21
|
||||
//
|
||||
// GO Active Thing
|
||||
// Framework
|
||||
//
|
||||
// http://activething.com/dev/goat
|
||||
//
|
||||
// Dev : Juan V.Navarro
|
||||
// Email: juanvnl@activething.com
|
||||
// Date : 16/02/2021
|
||||
//
|
||||
// Copyright 2021 the GOAT Authors
|
||||
//
|
||||
// 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 syncs
|
||||
|
||||
type (
|
||||
|
||||
|
||||
LockerRead interface {
|
||||
RLock()
|
||||
RUnlock()
|
||||
}
|
||||
)
|
||||
|
||||
/*
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
|
||||
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
|
||||
|
||||
SIDDHARTA
|
||||
- HERMANN HESSE -
|
||||
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
*/
|
||||
@ -0,0 +1,49 @@
|
||||
//
|
||||
// Feb21
|
||||
//
|
||||
// GO Active Thing
|
||||
// Framework
|
||||
//
|
||||
// http://activething.com/dev/goat
|
||||
//
|
||||
// Dev : Juan V.Navarro
|
||||
// Email: juanvnl@activething.com
|
||||
// Date : 16/02/2021
|
||||
//
|
||||
// Copyright 2021 the GOAT Authors
|
||||
//
|
||||
// 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 syncs
|
||||
|
||||
type (
|
||||
|
||||
|
||||
LockerRW interface {
|
||||
LockerRead
|
||||
LockerWrite
|
||||
}
|
||||
)
|
||||
|
||||
/*
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
|
||||
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
|
||||
|
||||
SIDDHARTA
|
||||
- HERMANN HESSE -
|
||||
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
*/
|
||||
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Feb21
|
||||
//
|
||||
// GO Active Thing
|
||||
// Framework
|
||||
//
|
||||
// http://activething.com/dev/goat
|
||||
//
|
||||
// Dev : Juan V.Navarro
|
||||
// Email: juanvnl@activething.com
|
||||
// Date : 16/02/2021
|
||||
//
|
||||
// Copyright 2021 the GOAT Authors
|
||||
//
|
||||
// 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 syncs
|
||||
|
||||
type (
|
||||
|
||||
|
||||
LockerWrite interface {
|
||||
Lock()
|
||||
Unlock()
|
||||
}
|
||||
)
|
||||
|
||||
/*
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA.
|
||||
"LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS.
|
||||
|
||||
SIDDHARTA
|
||||
- HERMANN HESSE -
|
||||
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
|
||||
*/
|
||||
@ -0,0 +1,45 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name nop_locker.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 syncs
|
||||
|
||||
|
||||
type (
|
||||
|
||||
LockerNop struct{}
|
||||
)
|
||||
|
||||
|
||||
func (_ LockerNop) Lock() {}
|
||||
|
||||
|
||||
func (_ LockerNop) Unlock() {}
|
||||
|
||||
|
||||
func (_ LockerNop) RLock() {}
|
||||
|
||||
|
||||
func (_ LockerNop) RUnlock() {}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
module activething.com/go/gogo/values
|
||||
|
||||
|
||||
|
||||
go 1.20
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name gogo.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/errs"
|
||||
|
||||
var (
|
||||
|
||||
G = struct {
|
||||
|
||||
ErrInvalidScope errs.ErrCode
|
||||
ErrInvalidScopeKey errs.ErrCode
|
||||
ErrReadOnlyScope errs.ErrCode
|
||||
|
||||
}{
|
||||
ErrInvalidScope: errs.ErrCode("core:values.invalid-scope"),
|
||||
ErrInvalidScopeKey: errs.ErrCode("core:values.invalid-scope-key"),
|
||||
ErrReadOnlyScope: errs.ErrCode("core:values.read-only-scope"),
|
||||
}
|
||||
|
||||
)
|
||||
@ -0,0 +1,118 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name properties_att.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
|
||||
|
||||
|
||||
const(
|
||||
|
||||
PropertiesAttNone PropertiesAtt = 1 << iota
|
||||
PropertiesAttFixed
|
||||
PropertiesAttReadOnly
|
||||
)
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
PropertiesAtt uint8
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (a PropertiesAtt) IsFixed () bool {
|
||||
return a & PropertiesAttFixed != 0 }
|
||||
|
||||
|
||||
func (a PropertiesAtt) IsReadOnly () bool {
|
||||
return a & PropertiesAttReadOnly != 0 }
|
||||
|
||||
@ -0,0 +1,133 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name scope.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"
|
||||
"activething.com/go/gogo/core/syncs"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Scope struct {
|
||||
locker syncs.LockerRW
|
||||
atts ScopeAtt
|
||||
parent ValueProvider
|
||||
valuers map[any]Valuer
|
||||
}
|
||||
|
||||
ScopeWalkFnc func (key, value any) bool
|
||||
)
|
||||
|
||||
|
||||
func NewScope (options ...opts.OptionErrFnc[Scope]) (*Scope,error) {
|
||||
return opts.ApplyAndCheck[Scope](&Scope {
|
||||
valuers : make(map[any]Valuer),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Scope) Reset () {
|
||||
s.locker.Lock()
|
||||
s.parent = nil
|
||||
for k,_ := range s.valuers {
|
||||
delete(s.valuers,k)
|
||||
}
|
||||
s.locker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func (s *Scope) Attach (parent ValueProvider) {
|
||||
s.locker.Lock()
|
||||
s.parent = parent
|
||||
s.locker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func (s *Scope) Detach() {
|
||||
s.locker.Lock()
|
||||
s.parent = nil
|
||||
s.locker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func (s *Scope) Parent () ValueProvider {
|
||||
s.locker.RLock()
|
||||
p := s.parent
|
||||
s.locker.RUnlock()
|
||||
return p
|
||||
}
|
||||
|
||||
|
||||
func (s *Scope) Has (key any) bool {
|
||||
s.locker.RLock()
|
||||
_,h := s.valuers[key]
|
||||
s.locker.RUnlock()
|
||||
return h
|
||||
}
|
||||
|
||||
func (s *Scope) Value (key any) (any,bool) {
|
||||
if key == nil {
|
||||
return nil,false
|
||||
}
|
||||
s.locker.RLock()
|
||||
v,h := s.valuers[key]
|
||||
s.locker.RUnlock()
|
||||
if h {
|
||||
return v,true
|
||||
}
|
||||
if s.parent != nil {
|
||||
return s.parent.Value(key)
|
||||
}
|
||||
return nil,false
|
||||
}
|
||||
|
||||
func (s *Scope) SetValue(key any, value any) error {
|
||||
if key == nil {
|
||||
return G.ErrInvalidScopeKey
|
||||
}
|
||||
s.locker.Lock()
|
||||
if s.atts.IsReadOnly() {
|
||||
s.locker.Unlock()
|
||||
return G.ErrReadOnlyScope
|
||||
}
|
||||
s.valuers[key]= AsValuer(value)
|
||||
s.locker.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scope) DelValue (key any) {
|
||||
s.locker.Lock()
|
||||
delete (s.valuers,key)
|
||||
s.locker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *Scope) Walk (walker ScopeWalkFnc) {
|
||||
for k,v := range s.valuers {
|
||||
if !walker(k,v.Value()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name scope_att.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
|
||||
|
||||
|
||||
|
||||
const (
|
||||
|
||||
ScopeAttReadOnly ScopeAtt = 1 << iota
|
||||
)
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ScopeAtt uint8
|
||||
)
|
||||
|
||||
|
||||
func (a ScopeAtt) IsReadOnly() bool {
|
||||
return a&ScopeAttReadOnly != 0 }
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name scope_ops.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"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ScopeWithParent(parent ValueProvider) opts.OptionErrFnc[Scope] {
|
||||
return func(sc *Scope) error {
|
||||
sc.parent = parent
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func ScopeWithLocker() opts.OptionErrFnc[Scope] {
|
||||
return func(sc *Scope) error {
|
||||
sc.locker = &sync.RWMutex{}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func ScopeWithValue(key, val interface{}) opts.OptionErrFnc[Scope] {
|
||||
return func(sc *Scope) error {
|
||||
return sc.SetValue(key, val) }
|
||||
}
|
||||
|
||||
|
||||
func ScopeWithReadOnly(att ScopeAtt) opts.OptionErrFnc[Scope] {
|
||||
return func(sc *Scope) error {
|
||||
sc.atts |= att
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name value_provider.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
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ValueProvider interface {
|
||||
|
||||
Value (key any) (any,bool)
|
||||
}
|
||||
|
||||
ProviderValueFnc func(key any) (any,bool)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func (f ProviderValueFnc) Value (key any) (any,bool) {
|
||||
return f(key) }
|
||||
@ -0,0 +1,75 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project atila
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name valuer.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
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Valuer interface {
|
||||
|
||||
Value () any
|
||||
|
||||
}
|
||||
|
||||
|
||||
ValueFnc func () any
|
||||
)
|
||||
|
||||
|
||||
func (f ValueFnc) Value() any {
|
||||
return f() }
|
||||
|
||||
|
||||
|
||||
|
||||
func AsValuer (value any) Valuer {
|
||||
if value == nil {
|
||||
return ValueFnc(nopValue)
|
||||
}
|
||||
switch tp := value.(type) {
|
||||
case Valuer : return tp
|
||||
case func() any : return ValueFnc(tp)
|
||||
default:
|
||||
return ValueFnc(func() any { return value })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func AsValue (value any) any {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
if vl,is := value.(Valuer); is {
|
||||
return vl.Value()
|
||||
}
|
||||
if vl,is := value.(func() any); is {
|
||||
return vl()
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
func nopValue () any {
|
||||
return nil }
|
||||
Loading…
Reference in new issue