|
|
// ------------------------------------------------------------------------
|
|
|
// ℹ️ Project active3
|
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
|
//
|
|
|
// File name fields.go
|
|
|
// ✍️ Created by DEV
|
|
|
// 📅 Modified 01/05/2024
|
|
|
//
|
|
|
// 🔒 COPYRIGHT 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 fields
|
|
|
|
|
|
import (
|
|
|
"ablds"
|
|
|
"sort"
|
|
|
)
|
|
|
|
|
|
type (
|
|
|
|
|
|
// Fields es un tipo que representa una lista de campos y proporciona métodos
|
|
|
// para gestionar y manipular dicha lista.
|
|
|
Fields []Field
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (f Fields) Len () int {
|
|
|
return len(f) }
|
|
|
|
|
|
func (f Fields) Has (name string) bool {
|
|
|
return f.indexOf(name) != -1 }
|
|
|
|
|
|
|
|
|
func (f Fields) Kinds() Kind {
|
|
|
var k Kind
|
|
|
for _,f := range f {
|
|
|
k|=f.Kind()
|
|
|
}
|
|
|
return k
|
|
|
}
|
|
|
|
|
|
func (f Fields) Get(name string) Field {
|
|
|
if p := f.indexOf(name); p != -1 {
|
|
|
return f[p]
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f Fields) GetAll (name string) []Field {
|
|
|
var ls []Field
|
|
|
for _,fl := range f {
|
|
|
if fl.Name() == name {
|
|
|
ls = append(ls,fl)
|
|
|
}
|
|
|
}
|
|
|
return ls
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (f Fields) Add(field Field) Fields {
|
|
|
if field != nil {
|
|
|
return append(f,field)
|
|
|
}
|
|
|
return f
|
|
|
}
|
|
|
|
|
|
func (f Fields) Set(field Field) Fields {
|
|
|
if field == nil {
|
|
|
return f
|
|
|
}
|
|
|
if p := f.indexOf(field.Name()); p != -1 {
|
|
|
return append(append(f[:p],field), f[p+1:]...)
|
|
|
}
|
|
|
return append(f,field)
|
|
|
}
|
|
|
|
|
|
func (f Fields) Del(name string) Fields {
|
|
|
if p := f.indexOf(name); p != -1 {
|
|
|
return append(f[:p],f[p+1:]...)
|
|
|
}
|
|
|
return f
|
|
|
}
|
|
|
|
|
|
func (f Fields) DelAll (name string) Fields {
|
|
|
l := make(Fields,0,len(f))
|
|
|
c := 0
|
|
|
for i := 0; i<len(f); i++ {
|
|
|
if f[i].Name() == name {
|
|
|
l = append(l,f[c:i]...)
|
|
|
c = i+1
|
|
|
}
|
|
|
}
|
|
|
l = append(l,f[c:]...)
|
|
|
return l
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f Fields) Sort() {
|
|
|
sort.Sort(f) }
|
|
|
|
|
|
func (f Fields) Swap(i, j int) {
|
|
|
f[i], f[j] = f[j], f[i] }
|
|
|
|
|
|
func (f Fields) Less(i, j int) bool {
|
|
|
return f[i].Name() < f[j].Name() }
|
|
|
|
|
|
|
|
|
|
|
|
func (f Fields) indexOf(name string) int {
|
|
|
for i,f := range f {
|
|
|
if f.Name() == name {
|
|
|
return i
|
|
|
}
|
|
|
}
|
|
|
return -1
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f Fields) Build (builder ablds.Builder) {
|
|
|
builder.List(func(lb ablds.ListBuilder) {
|
|
|
for _,fl := range f {
|
|
|
buildFieldAsObject(fl,lb.Item())
|
|
|
}
|
|
|
})
|
|
|
}
|