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.

144 lines
3.5 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name builder.go
// Created by DEV
// Modified 20/04/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 collects
import (
"sort"
"time"
)
type (
Fields struct {
heads []*FieldHead
anys []any
strings []string
ints []int
floats []float64
dates []time.Time
}
)
func (f *Fields) Len() int {
return len(f.heads) }
func (f *Fields) AddFieldAny (name string, value any) {
f.addField(name, FieldKindAny).anys = append(f.anys,value) }
func (f *Fields) AddFieldString(name string, value string) {
f.addField(name, FieldKindString)
f.strings = append(f.strings, value)
}
func (f *Fields) AddFieldInt (name string, value int) {
f.addField(name, FieldKindInt).ints = append(f.ints, value) }
func (f *Fields) AddFieldBool (name string, value bool) {
bv := 0
if value {
bv = 1
}
f.addField(name, FieldKindInt).ints = append(f.ints, bv)
}
func (f *Fields) AddFieldFloat (name string, value float64) {
f.addField(name, FieldKindFloat).floats = append(f.floats, value) }
func (f *Fields) AddFieldTime (name string, value time.Time) {
f.addField(name, FieldKindTime).dates = append(f.dates, value) }
func (f *Fields) AddFieldDate (name string, value time.Time) {
f.addField(name, FieldKindDate).dates = append(f.dates, value) }
func (f *Fields) addField (name string, kind FieldKind) *Fields {
var pos int
switch kind {
case FieldKindBool,
FieldKindInt: pos = len(f.ints)
case FieldKindString: pos = len(f.strings)
case FieldKindDate,
FieldKindTime: pos = len(f.dates)
case FieldKindFloat: pos = len(f.floats)
case FieldKindAny: pos = len(f.anys)
}
f.heads = append(f.heads, &FieldHead{
Name: name,
Kind: kind,
Pos: pos,
})
return f
}
func (f *Fields) GetHeads () []*FieldHead {
return f.heads }
func (f *Fields) GetField (name string) (*FieldHead,bool) {
for i,h := range f.heads {
if h.Name == name {
return f.heads[i],true
}
}
return nil,false
}
func (f *Fields) GetFieldValueAny (head *FieldHead) any {
return f.anys[head.Pos] }
func (f *Fields) GetFieldValueString (head *FieldHead) string {
return f.strings[head.Pos] }
func (f *Fields) GetFieldValueInt (head *FieldHead) int {
return f.ints[head.Pos] }
func (f *Fields) GetFieldValueFloat (head *FieldHead) float64 {
return f.floats[head.Pos] }
func (f *Fields) GetFieldValueBool (head *FieldHead) bool {
return (f.ints[head.Pos]) > 0 }
func (f *Fields) GetFieldValueDate (head *FieldHead) time.Time {
return f.dates[head.Pos] }
func (f *Fields) GetFieldValueTime (head *FieldHead) time.Time {
return f.dates[head.Pos] }
func (f *Fields) Sort () {
sort.Slice(f.heads, func(i, j int) bool {
return f.heads[i].Name < f.heads[j].Name
})
}

Powered by TurnKey Linux.