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.

652 lines
12 KiB

11 months ago
// nolint: exhaustruct
3 years ago
package field
import (
"fmt"
"math"
"strconv"
"time"
"unsafe"
3 years ago
)
// StringValue returns a new Value for a string.
func StringValue(value string) Value {
return Value{
num: uint64(len(value)),
any: stringptr(unsafe.StringData(value)),
Kind: KindString,
}
3 years ago
}
// StringpValue returns a new Value for a *string.
func StringpValue(value *string) Value {
if value == nil {
return NilValue()
3 years ago
}
return StringValue(*value)
3 years ago
}
// StringpValue returns a new Value for a string.
func StringsValue(value []string) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(value)),
any: func() []Value {
values := make([]Value, len(value))
for idx := range value {
values[idx] = StringValue(value[idx])
}
3 years ago
return values
},
3 years ago
}
}
// BoolValue returns a Value for a bool.
func BoolValue(v bool) Value {
u := uint64(0)
if v {
u = 1
3 years ago
}
return Value{num: u, Kind: KindBool}
3 years ago
}
// BoolsValue returns a Value for a []bool.
func BoolsValue(values []bool) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = BoolValue(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// BoolpValue returns a new Value for a *bool.
func BoolpValue(value *bool) Value {
if value == nil {
return NilValue()
3 years ago
}
return BoolValue(*value)
3 years ago
}
// Uint64Value returns a Value for a uint64.
func Uint64Value(v uint64) Value {
return Value{num: v, Kind: KindUint64}
3 years ago
}
// Uint64sValue returns a Value for a []uint64.
func Uint64sValue(values []uint64) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Uint64Value(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// Uint8sValue returns a Value for a []uint8.
func Uint8sValue(values []uint8) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Uint64Value(uint64(values[idx]))
}
3 years ago
return vals
},
3 years ago
}
}
// Uint64sValue returns a Value for a []uint64.
func Uint64pValue(v *uint64) Value {
if v == nil {
return NilValue()
3 years ago
}
return Uint64Value(*v)
3 years ago
}
// Int64Value returns a Value for an int64.
func Int64Value(value int64) Value {
return Value{inum: value, Kind: KindInt64}
3 years ago
}
// Int64sValue returns a Value for an []int64.
func Int64sValue(value []int64) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(value)),
any: func() []Value {
vals := make([]Value, len(value))
for idx := range value {
vals[idx] = Int64Value(value[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// Int64sValue returns a Value for an *int64.
func Int64pValue(value *int64) Value {
if value == nil {
return NilValue()
3 years ago
}
return Int64Value(*value)
3 years ago
}
// Float64Value returns a Value for a floating-point number.
func Float64Value(v float64) Value {
return Value{num: math.Float64bits(v), Kind: KindFloat64}
3 years ago
}
// Float64Value returns a Value for a floating-points number.
func Float64sValue(values []float64) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Float64Value(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// Float64Value returns a Value for a floating-points number.
func Float64pValue(v *float64) Value {
if v == nil {
return NilValue()
3 years ago
}
return Float64Value(*v)
3 years ago
}
// Complex64sValue returns a Value for a []complex64.
func Complex64sValue(values []complex64) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Complex128Value(complex128(values[idx]))
}
3 years ago
return vals
},
3 years ago
}
}
// Complex128Value returns a Value for a complex128.
func Complex128Value(v complex128) Value {
3 years ago
return Value{
Kind: KindComplex128,
any: v,
3 years ago
}
}
// Complex128Value returns a Value for a []complex128.
func Complex128sValue(values []complex128) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Complex128Value(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// Complex128Value returns a Value for a *complex128.
func Complex128pValue(v *complex128) Value {
if v == nil {
return NilValue()
3 years ago
}
return Complex128Value(*v)
3 years ago
}
// TimeValue returns a Value for a time.Time.
func TimeValue(v time.Time) Value {
return Value{inum: v.UnixNano(), any: v.Location(), Kind: KindTime}
3 years ago
}
// TimepValue returns a Value for a *time.Time.
func TimepValue(v *time.Time) Value {
if v == nil {
return NilValue()
3 years ago
}
return TimeValue(*v)
3 years ago
}
// TimesValue returns a Value for a []time.Time.
func TimesValue(values []time.Time) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = TimeValue(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
func ClosureValue(fn ClosureFn) Value {
3 years ago
return Value{
Kind: KindClosure,
any: fn,
3 years ago
}
}
// DurationValue returns a Value for a time.Duration.
func DurationValue(v time.Duration) Value {
return Value{inum: v.Nanoseconds(), Kind: KindDuration}
3 years ago
}
// DurationValue returns a Value for a *time.Duration.
func DurationpValue(v *time.Duration) Value {
if v == nil {
return NilValue()
3 years ago
}
return DurationValue(*v)
3 years ago
}
// DurationValue returns a Value for a *time.Duration.
func DurationsValue(values []time.Duration) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = DurationValue(values[idx])
}
3 years ago
return vals
},
3 years ago
}
}
// GroupValue returns a new Value for a list of Fields.
func GroupValue(as ...Field) Value {
return Value{
num: uint64(len(as)),
any: groupptr(unsafe.SliceData(as)),
Kind: KindGroup,
3 years ago
}
}
func ErrorValue(value error) Value {
3 years ago
return Value{
Kind: KindError,
any: value,
3 years ago
}
}
func ErrorsValue(value []error) Value {
3 years ago
return Value{
Kind: KindArray,
num: uint64(len(value)),
any: func() []Value {
vals := make([]Value, len(value))
for idx := range value {
vals[idx] = ErrorValue(value[idx])
}
3 years ago
return vals
},
3 years ago
}
}
func BytesValue(value []byte) Value {
return Value{
Kind: KindBinary,
any: value,
}
}
//nolint:gochecknoglobals
var nilValue = Value{
Kind: KindNil,
}
func NilValue() Value {
return nilValue
}
// AnyValue returns a Value for the supplied value.
//
//nolint:funlen,gocyclo,cyclop
func AnyValue(v any) Value {
switch value := v.(type) {
case string:
return StringValue(value)
case int:
return Int64Value(int64(value))
case uint:
return Uint64Value(uint64(value))
case int64:
return Int64Value(value)
case *int64:
return Int64pValue(value)
case []int64:
return Int64sValue(value)
case uint64:
return Uint64Value(value)
case *uint64:
return Uint64pValue(value)
case []uint64:
return Uint64sValue(value)
case bool:
return BoolValue(value)
case *bool:
return BoolpValue(value)
case []bool:
return BoolsValue(value)
case nil:
return NilValue()
case complex128:
return Complex128Value(value)
case *complex128:
return Complex128pValue(value)
case []complex128:
return Complex128sValue(value)
case complex64:
return Complex128Value(complex128(value))
case []complex64:
return Complex64sValue(value)
case time.Duration:
return DurationValue(value)
case *time.Duration:
return DurationpValue(value)
case []time.Duration:
return DurationsValue(value)
case time.Time:
return TimeValue(value)
case *time.Time:
return TimepValue(value)
case []time.Time:
return TimesValue(value)
case uint8:
return Uint64Value(uint64(value))
case []uint8:
return Uint8sValue(value)
case uint16:
return Uint64Value(uint64(value))
case uint32:
return Uint64Value(uint64(value))
case uintptr:
return Uint64Value(uint64(value))
case int8:
return Int64Value(int64(value))
case int16:
return Int64Value(int64(value))
case int32:
return Int64Value(int64(value))
case float64:
return Float64Value(value)
case *float64:
return Float64pValue(value)
case []float64:
return Float64sValue(value)
case float32:
return Float64Value(float64(value))
case error:
return ErrorValue(value)
case []error:
return ErrorsValue(value)
case []Field:
return GroupValue(value...)
case Fields:
return GroupValue(value...)
case Kind:
return Value{Kind: value}
case func() any:
return ClosureValue(value)
case ClosureFn:
return ClosureValue(value)
case LogValuer:
return ClosureValue(value.LogValue)
case Value:
return value
default:
return Value{any: value}
}
}
type (
stringptr *byte // used in Value.any when the Value is a string
groupptr *Field // used in Value.any when the Value is a []Field
)
type Value struct {
Kind Kind
num uint64
inum int64
any any
3 years ago
}
func (v Value) String() string {
if sp, ok := v.any.(stringptr); ok {
return unsafe.String(sp, v.num)
}
var buf []byte
return string(v.append(buf))
}
// append appends a text representation of v to dst.
// v is formatted as with fmt.Sprint.
//
//nolint:gomnd,cyclop
func (v Value) append(dst []byte) []byte {
switch v.Kind {
case KindString:
return append(dst, v.AsString()...)
case KindInt64:
return strconv.AppendInt(dst, v.inum, 10)
case KindUint64:
return strconv.AppendUint(dst, v.num, 10)
case KindFloat64:
return strconv.AppendFloat(dst, v.AsFloat64(), 'g', -1, 64)
case KindFloat32:
return strconv.AppendFloat(dst, float64(v.AsFloat32()), 'g', -1, 32)
case KindBool:
return strconv.AppendBool(dst, v.AsBool())
case KindDuration:
return append(dst, v.AsDuration().String()...)
case KindTime:
return append(dst, v.AsTime().String()...)
case KindError:
return append(dst, v.AsError().Error()...)
case KindGroup:
return fmt.Append(dst, v.AsGroup())
case KindClosure:
return fmt.Append(dst, v.Resolve())
case KindAny:
return fmt.Append(dst, v.any)
default:
return fmt.Appendf(dst, "%+v", v.any)
}
}
11 months ago
// nolint: gocyclo,cyclop
func (v Value) Any() any {
switch v.Kind {
case KindAny, KindBinary:
return v.any
case KindString:
return v.AsString()
case KindInt64:
return v.AsInt64()
case KindArray:
return v.AsArray().Resolve()
case KindBool:
return v.AsBool()
case KindClosure:
return v.Resolve()
case KindComplex128:
return v.AsComplex128()
case KindDuration:
return v.AsDuration()
case KindTime:
return v.AsTime()
case KindError:
return v.AsError()
case KindFloat32:
return v.AsFloat32()
case KindFloat64:
return v.AsFloat64()
case KindNil:
return nil
case KindUint64:
return v.AsUint64()
case KindGroup:
return v.AsGroup().Any()
3 years ago
}
return v.any
3 years ago
}
11 months ago
// nolint: forcetypeassert
func (v Value) AsString() string {
if v.Kind != KindString {
return ""
3 years ago
}
return unsafe.String(v.any.(stringptr), v.num)
3 years ago
}
func (v Value) AsBool() bool {
return v.num == 1
3 years ago
}
func (v Value) AsInt64() int64 {
return v.inum
3 years ago
}
func (v Value) AsUint() uint {
return uint(v.num)
}
3 years ago
func (v Value) AsUint64() uint64 {
return v.num
3 years ago
}
func (v Value) AsFloat32() float32 {
return math.Float32frombits(uint32(v.num))
3 years ago
}
func (v Value) AsFloat64() float64 {
return math.Float64frombits(v.num)
3 years ago
}
func (v Value) AsComplex128() complex128 {
cmplex, _ := v.any.(complex128)
3 years ago
return cmplex
3 years ago
}
func (v Value) AsUintptr() uintptr {
return uintptr(v.num)
3 years ago
}
func (v Value) AsBinary() []byte {
bytes, _ := v.any.([]byte)
return bytes
3 years ago
}
func (v Value) AsDuration() time.Duration {
return time.Duration(v.inum)
3 years ago
}
func (v Value) AsTime() time.Time {
loc, ok := v.any.(*time.Location)
if !ok {
return time.Time{}
3 years ago
}
return time.Unix(0, v.inum).In(loc)
3 years ago
}
func (v Value) AsError() error {
err, _ := v.any.(error)
3 years ago
return err
3 years ago
}
//nolint:forcetypeassert
func (v Value) AsGroup() Fields {
if v.Kind != KindGroup {
return nil
3 years ago
}
return unsafe.Slice((*Field)(v.any.(groupptr)), v.num)
3 years ago
}
func (v Value) Resolve() any {
cl, ok := v.any.(ClosureFn)
if !ok {
return nil
3 years ago
}
return cl()
3 years ago
}
type Values []Value
func (v Values) Resolve() any {
res := make([]any, len(v))
for idx := range v {
res[idx] = v[idx].Any()
3 years ago
}
return res
3 years ago
}
func (v Value) AsArray() Values {
switch res := v.any.(type) {
case []Value:
return res
case func() []Value:
return res()
default:
return nil
3 years ago
}
}