|
|
// ------------------------------------------------------------------------
|
|
|
// ℹ️ Project active3
|
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
|
//
|
|
|
// File name field_any.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 acache
|
|
|
|
|
|
import (
|
|
|
"ablds"
|
|
|
"reflect"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type (
|
|
|
|
|
|
FieldAny struct {
|
|
|
partialField
|
|
|
valuerMode
|
|
|
valueFn func() any
|
|
|
}
|
|
|
)
|
|
|
|
|
|
|
|
|
func NewFieldAny (name string, value any) *FieldAny {
|
|
|
return &FieldAny{
|
|
|
partialField : makePartialField(name),
|
|
|
valuerMode: localValuer{},
|
|
|
valueFn : func() any { return value },
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
func NewDFieldAny (name string, valueFn func() any) *FieldAny {
|
|
|
if valueFn == nil {
|
|
|
panic(G.ErrInvalidDeferFunc)
|
|
|
}
|
|
|
return &FieldAny{
|
|
|
partialField: makePartialField(name),
|
|
|
valuerMode: deferValuer{},
|
|
|
valueFn : valueFn,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f FieldAny) Kind() Kind {
|
|
|
return KindAny | f.valuerKind() }
|
|
|
|
|
|
|
|
|
func (f FieldAny) Value() any {
|
|
|
return f.valueFn() }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (f FieldAny) Bool () (bool,bool) {
|
|
|
v,ok := f.valueFn().(bool)
|
|
|
return v,ok
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) Binary () ([]byte,bool) {
|
|
|
v, ok := f.valueFn().([]byte)
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) String () (string,bool) {
|
|
|
v,ok := f.valueFn().(string)
|
|
|
return v,ok
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) Int () (int,bool) {
|
|
|
switch t := f.valueFn().(type) {
|
|
|
case int : return t,true
|
|
|
case int8 : return int(t),true
|
|
|
case int16: return int(t),true
|
|
|
case int32: return int(t),true
|
|
|
case int64: return int(t),true
|
|
|
default : return 0,false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) Uint () (uint,bool) {
|
|
|
switch t := f.valueFn().(type) {
|
|
|
case uint : return t,true
|
|
|
case uint8 : return uint(t),true
|
|
|
case uint16: return uint(t),true
|
|
|
case uint32: return uint(t),true
|
|
|
case uint64: return uint(t),true
|
|
|
default : return 0,false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) Float () (float64,bool) {
|
|
|
v, ok := f.valueFn().(float64)
|
|
|
if !ok {
|
|
|
var f32 float32
|
|
|
if f32,ok = f.valueFn().(float32); ok {
|
|
|
v = float64(f32)
|
|
|
}
|
|
|
}
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f FieldAny) Date () (time.Time,bool) {
|
|
|
v, ok := f.valueFn().(time.Time)
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
func (f FieldAny) Time () (time.Time,bool) {
|
|
|
v, ok := f.valueFn().(time.Time)
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f FieldAny) Error () (error,bool) {
|
|
|
v, ok := f.valueFn().(error)
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
|
|
|
func (f FieldAny) Reflect () (reflect.Value,bool) {
|
|
|
v, ok := f.valueFn().(reflect.Value)
|
|
|
return v, ok
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f FieldAny) Build (builder ablds.Builder) {
|
|
|
builder.Object(func(b ablds.ObjectBuilder){
|
|
|
b.Field(f.name).Any(f.valueFn()) }) } |