update field slice
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
andrey
2024-01-02 15:38:42 +03:00
parent 931b802139
commit 3c83f30826
8 changed files with 316 additions and 34 deletions

5
field/errors.go Normal file
View File

@@ -0,0 +1,5 @@
package field
import "errors"
var ErrUndefined = errors.New("indefined")

View File

@@ -85,7 +85,7 @@ func Uint8(key string, value uint8) Field {
func Uint8s(key string, value ...uint8) Field {
return Field{
Key: key,
Value: AnyValue(value),
Value: Uint8sValue(value),
}
}
@@ -316,7 +316,7 @@ func Complex64(key string, value complex64) Field {
func Complex64s(key string, value ...complex64) Field {
return Field{
Key: key,
Value: AnyValue(value),
Value: Complex64sValue(value),
}
}

View File

@@ -1,5 +1,7 @@
package field
import "fmt"
//go:generate stringer -type=Kind -linecomment -output=kind_string.go
type Kind int
@@ -22,3 +24,63 @@ const (
KindGroup // group
KindClosure // closure
)
func (l Kind) MarshalJSON() ([]byte, error) {
return []byte("\"" + l.String() + "\""), nil
}
func (l *Kind) UnmarshalJSON(in []byte) error {
return l.UnmarshalText(in[1 : len(in)-1])
}
func (l Kind) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
//nolint:gocyclo,cyclop
func (l *Kind) UnmarshalText(in []byte) error {
switch string(in) {
case KindAny.String():
*l = KindAny
case KindArray.String():
*l = KindArray
case KindNil.String():
*l = KindNil
case KindString.String():
*l = KindString
case KindBool.String():
*l = KindBool
case KindInt64.String():
*l = KindInt64
case KindUint64.String():
*l = KindUint64
case KindFloat32.String():
*l = KindFloat32
case KindFloat64.String():
*l = KindFloat64
case KindComplex128.String():
*l = KindComplex128
case KindBinary.String():
*l = KindBinary
case KindDuration.String():
*l = KindDuration
case KindTime.String():
*l = KindTime
case KindError.String():
*l = KindError
case KindGroup.String():
*l = KindGroup
case KindClosure.String():
*l = KindClosure
}
return fmt.Errorf("%w:filed(%v)", ErrUndefined, string(in))
}
func (l Kind) MarshalBinary() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *Kind) UnmarshalBinary(in []byte) error {
return l.UnmarshalText(in)
}

View File

@@ -57,9 +57,14 @@ func BoolValue(v bool) Value {
func BoolsValue(values []bool) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindBool,
any: values,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = BoolValue(values[idx])
}
return vals
},
}
}
@@ -79,12 +84,33 @@ func Uint64Value(v uint64) Value {
}
// Uint64sValue returns a Value for a []uint64.
func Uint64sValue(v []uint64) Value {
func Uint64sValue(values []uint64) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindUint64,
any: v,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Uint64Value(values[idx])
}
return vals
},
}
}
// Uint8sValue returns a Value for a []uint8.
func Uint8sValue(values []uint8) Value {
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]))
}
return vals
},
}
}
@@ -107,9 +133,14 @@ func Int64Value(value int64) Value {
func Int64sValue(value []int64) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindInt64,
any: value,
num: uint64(len(value)),
any: func() []Value {
vals := make([]Value, len(value))
for idx := range value {
vals[idx] = Int64Value(value[idx])
}
return vals
},
}
}
@@ -129,12 +160,17 @@ func Float64Value(v float64) Value {
}
// Float64Value returns a Value for a floating-points number.
func Float64sValue(v []float64) Value {
func Float64sValue(values []float64) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindFloat64,
any: v,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Float64Value(values[idx])
}
return vals
},
}
}
@@ -148,6 +184,22 @@ func Float64pValue(v *float64) Value {
return Float64Value(*v)
}
// Complex64sValue returns a Value for a []complex64.
func Complex64sValue(values []complex64) Value {
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]))
}
return vals
},
}
}
// Complex128Value returns a Value for a complex128.
func Complex128Value(v complex128) Value {
return Value{
@@ -157,12 +209,17 @@ func Complex128Value(v complex128) Value {
}
// Complex128Value returns a Value for a []complex128.
func Complex128sValue(v []complex128) Value {
func Complex128sValue(values []complex128) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindComplex128,
any: v,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = Complex128Value(values[idx])
}
return vals
},
}
}
@@ -191,12 +248,17 @@ func TimepValue(v *time.Time) Value {
}
// TimesValue returns a Value for a []time.Time.
func TimesValue(v []time.Time) Value {
func TimesValue(values []time.Time) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindTime,
any: v,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = TimeValue(values[idx])
}
return vals
},
}
}
@@ -223,12 +285,17 @@ func DurationpValue(v *time.Duration) Value {
}
// DurationValue returns a Value for a *time.Duration.
func DurationsValue(v []time.Duration) Value {
func DurationsValue(values []time.Duration) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindDuration,
any: v,
num: uint64(len(values)),
any: func() []Value {
vals := make([]Value, len(values))
for idx := range values {
vals[idx] = DurationValue(values[idx])
}
return vals
},
}
}
@@ -252,9 +319,14 @@ func ErrorValue(value error) Value {
func ErrorsValue(value []error) Value {
return Value{
Kind: KindArray,
any: Value{
Kind: KindError,
any: value,
num: uint64(len(value)),
any: func() []Value {
vals := make([]Value, len(value))
for idx := range value {
vals[idx] = ErrorValue(value[idx])
}
return vals
},
}
}
@@ -314,6 +386,8 @@ func AnyValue(v any) Value {
return Complex128sValue(value)
case complex64:
return Complex128Value(complex128(value))
case []complex64:
return Complex64sValue(value)
case time.Duration:
return DurationValue(value)
case *time.Duration:
@@ -328,6 +402,8 @@ func AnyValue(v any) Value {
return TimesValue(value)
case uint8:
return Uint64Value(uint64(value))
case []uint8:
return Uint8sValue(value)
case uint16:
return Uint64Value(uint64(value))
case uint32: