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
144 lines
3.5 KiB
|
2 years ago
|
// ------------------------------------------------------------------------
|
||
|
|
// 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.
|
||
|
|
// ------------------------------------------------------------------------
|
||
|
|
|
||
|
2 years ago
|
package collects
|
||
|
2 years ago
|
|
||
|
|
import (
|
||
|
|
"sort"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
type (
|
||
|
|
|
||
|
|
Fields struct {
|
||
|
|
heads []*FieldHead
|
||
|
|
|
||
|
|
anys []any
|
||
|
|
strings []string
|
||
|
|
ints []int
|
||
|
|
floats []float64
|
||
|
|
dates []time.Time
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
2 years ago
|
func (f *Fields) Len() int {
|
||
|
|
return len(f.heads) }
|
||
|
|
|
||
|
2 years ago
|
|
||
|
|
func (f *Fields) AddFieldAny (name string, value any) {
|
||
|
2 years ago
|
f.addField(name, FieldKindAny).anys = append(f.anys,value) }
|
||
|
2 years ago
|
|
||
|
|
func (f *Fields) AddFieldString(name string, value string) {
|
||
|
2 years ago
|
f.addField(name, FieldKindString)
|
||
|
2 years ago
|
f.strings = append(f.strings, value)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *Fields) AddFieldInt (name string, value int) {
|
||
|
2 years ago
|
f.addField(name, FieldKindInt).ints = append(f.ints, value) }
|
||
|
2 years ago
|
|
||
|
|
func (f *Fields) AddFieldBool (name string, value bool) {
|
||
|
|
bv := 0
|
||
|
|
if value {
|
||
|
|
bv = 1
|
||
|
|
}
|
||
|
2 years ago
|
f.addField(name, FieldKindInt).ints = append(f.ints, bv)
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
|
func (f *Fields) AddFieldFloat (name string, value float64) {
|
||
|
2 years ago
|
f.addField(name, FieldKindFloat).floats = append(f.floats, value) }
|
||
|
2 years ago
|
|
||
|
|
func (f *Fields) AddFieldTime (name string, value time.Time) {
|
||
|
2 years ago
|
f.addField(name, FieldKindTime).dates = append(f.dates, value) }
|
||
|
2 years ago
|
|
||
|
|
func (f *Fields) AddFieldDate (name string, value time.Time) {
|
||
|
2 years ago
|
f.addField(name, FieldKindDate).dates = append(f.dates, value) }
|
||
|
2 years ago
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
func (f *Fields) addField (name string, kind FieldKind) *Fields {
|
||
|
|
var pos int
|
||
|
|
switch kind {
|
||
|
|
case FieldKindBool,
|
||
|
2 years ago
|
FieldKindInt: pos = len(f.ints)
|
||
|
2 years ago
|
case FieldKindString: pos = len(f.strings)
|
||
|
|
case FieldKindDate,
|
||
|
2 years ago
|
FieldKindTime: pos = len(f.dates)
|
||
|
|
case FieldKindFloat: pos = len(f.floats)
|
||
|
|
case FieldKindAny: pos = len(f.anys)
|
||
|
2 years ago
|
}
|
||
|
|
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
|
||
|
|
})
|
||
|
|
}
|