github.com/go-haru/field@v0.0.2/field.go (about)

     1  package field
     2  
     3  import (
     4  	"bytes"
     5  	"encoding"
     6  	"encoding/base64"
     7  	"encoding/json"
     8  	"fmt"
     9  	"reflect"
    10  	"sort"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  	"unsafe"
    15  )
    16  
    17  type Fields []Field
    18  
    19  func (f Fields) Unique() []Field {
    20  	var newFields = make([]Field, 0, len(f))
    21  	var m = make(map[string]struct{}, len(f))
    22  	var newCount = 0
    23  	for i := 0; i < len(f); i++ {
    24  		if _, exist := m[f[i].Key]; !exist {
    25  			m[f[i].Key] = struct{}{}
    26  			newFields = append(newFields, f[i])
    27  			newCount++
    28  		}
    29  	}
    30  	newFields = newFields[:newCount]
    31  	sort.SliceStable(newFields, func(i, j int) bool {
    32  		return strings.Compare(newFields[i].Key, newFields[j].Key) < 0
    33  	})
    34  	return newFields
    35  }
    36  
    37  func (f Fields) Has(key string) bool {
    38  	for i := 0; i < len(f); i++ {
    39  		if f[i].Key == key {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }
    45  
    46  func (f Fields) Get(key string) (Field, bool) {
    47  	for i := 0; i < len(f); i++ {
    48  		if f[i].Key == key {
    49  			return f[i], true
    50  		}
    51  	}
    52  	return Field{}, false
    53  }
    54  
    55  func (f Fields) Export() map[string]any {
    56  	var m = make(map[string]any, len(f))
    57  	for i := 0; i < len(f); i++ {
    58  		m[f[i].Key] = f[i].Data()
    59  	}
    60  	return m
    61  }
    62  
    63  func (f Fields) EncodeJSON(buf Buffer) (err error) {
    64  	var snap = f.Unique()
    65  	if err = buf.WriteByte('{'); err != nil {
    66  		return err
    67  	}
    68  	for i := 0; i < len(snap); i++ {
    69  		if i > 0 {
    70  			if err = buf.WriteByte(','); err != nil {
    71  				return err
    72  			}
    73  		}
    74  		if err = snap[i].EncodeJSON(buf); err != nil {
    75  			return err
    76  		}
    77  	}
    78  	if err = buf.WriteByte('}'); err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  func (f Fields) MarshalJSON() (dst []byte, err error) {
    85  	var buf bytes.Buffer
    86  	err = f.EncodeJSON(&buf)
    87  	return buf.Bytes(), err
    88  }
    89  
    90  type Type uint8
    91  
    92  const (
    93  	TypeNull = iota
    94  	TypeBool
    95  	TypeInt
    96  	TypeUint
    97  	TypeUintptr
    98  	TypeFloat
    99  	TypeComplex
   100  	TypeString
   101  	TypeStringer
   102  	TypeBinary
   103  	TypeTime
   104  	TypeError
   105  	TypeJSON
   106  	TypeAny
   107  	TypeArray = 0x80
   108  )
   109  
   110  func valWithoutErr[T any](val T, _ error) T { return val }
   111  
   112  func errWithoutVal[T any](_ T, err error) error { return err }
   113  
   114  type Buffer interface {
   115  	Write(p []byte) (n int, err error)
   116  	WriteString(s string) (n int, err error)
   117  	WriteByte(c byte) error
   118  	WriteRune(r rune) (n int, err error)
   119  }
   120  
   121  type Content interface {
   122  	Type() Type
   123  	Data() any
   124  	EncodeJSON(buffer Buffer) error
   125  }
   126  
   127  type Field struct {
   128  	Key string
   129  	Content
   130  }
   131  
   132  func (f Field) EncodeJSON(buffer Buffer) (err error) {
   133  	if err = appendJsonStringBuf(buffer, f.Key); err != nil {
   134  		return err
   135  	}
   136  	if err = buffer.WriteByte(':'); err != nil {
   137  		return err
   138  	}
   139  	return f.Content.EncodeJSON(buffer)
   140  }
   141  
   142  func (f Field) MarshalJSON() (_ []byte, err error) {
   143  	var buf bytes.Buffer
   144  	if err = buf.WriteByte('{'); err != nil {
   145  		return nil, err
   146  	}
   147  	if err = f.EncodeJSON(&buf); err != nil {
   148  		return nil, err
   149  	}
   150  	if err = buf.WriteByte('}'); err != nil {
   151  		return nil, err
   152  	}
   153  	return buf.Bytes(), nil
   154  }
   155  
   156  // array wrapper
   157  
   158  func newArray[T any](list []T, converter func(T) Content) ArrayContent {
   159  	var result = make([]Content, len(list))
   160  	for i := 0; i < len(list); i++ {
   161  		result[i] = converter(list[i])
   162  	}
   163  	return ArrayContent{arrayRaw: result}
   164  }
   165  
   166  type ArrayContent struct {
   167  	arrayRaw []Content
   168  }
   169  
   170  func (f ArrayContent) Type() Type {
   171  	if len(f.arrayRaw) == 0 {
   172  		return TypeArray | TypeNull
   173  	}
   174  	return TypeArray | f.arrayRaw[0].Type()
   175  }
   176  
   177  func (f ArrayContent) Data() any {
   178  	var data = make([]any, len(f.arrayRaw))
   179  	for i := 0; i < len(f.arrayRaw); i++ {
   180  		data[i] = f.arrayRaw[i].Data()
   181  	}
   182  	return data
   183  }
   184  
   185  func (f ArrayContent) Raw() []Content { return f.arrayRaw }
   186  
   187  func (f ArrayContent) EncodeJSON(buffer Buffer) (err error) {
   188  	if err = buffer.WriteByte('['); err != nil {
   189  		return err
   190  	}
   191  	for i := 0; i < len(f.arrayRaw); i++ {
   192  		if i > 0 {
   193  			if err = buffer.WriteByte(','); err != nil {
   194  				return err
   195  			}
   196  		}
   197  		if err = f.arrayRaw[i].EncodeJSON(buffer); err != nil {
   198  			return err
   199  		}
   200  	}
   201  	if err = buffer.WriteByte(']'); err != nil {
   202  		return err
   203  	}
   204  	return nil
   205  }
   206  
   207  type NilContent struct{}
   208  
   209  func NewNilContent() Content { return NilContent{} }
   210  
   211  func (n NilContent) Type() Type { return TypeNull }
   212  
   213  func (n NilContent) Data() any { return nil }
   214  
   215  func (n NilContent) EncodeJSON(buffer Buffer) error {
   216  	return errWithoutVal(buffer.Write([]byte{'n', 'u', 'l', 'l'}))
   217  }
   218  
   219  func Nil(key string) Field { return Field{Key: key, Content: NilContent{}} }
   220  
   221  // data type: jsonRawRawMessage
   222  
   223  type JSONContent struct{ jsonRaw json.RawMessage }
   224  
   225  func NewJSONContent(val []byte) Content { return JSONContent{jsonRaw: val} }
   226  
   227  func (f JSONContent) Type() Type { return TypeJSON }
   228  
   229  func (f JSONContent) Data() any { return f.jsonRaw }
   230  
   231  func (f JSONContent) Raw() json.RawMessage { return f.jsonRaw }
   232  
   233  func (f JSONContent) EncodeJSON(buffer Buffer) (err error) {
   234  	return errWithoutVal(buffer.Write(f.jsonRaw))
   235  }
   236  
   237  func JsonRawMessage(key string, val json.RawMessage) Field {
   238  	return Field{key, NewJSONContent(val)}
   239  }
   240  
   241  // data type: binary
   242  
   243  type BinaryContent struct{ binaryRaw []byte }
   244  
   245  func NewBinaryContent(val []byte) Content { return BinaryContent{binaryRaw: val} }
   246  
   247  func (f BinaryContent) Type() Type { return TypeBinary }
   248  
   249  func (f BinaryContent) Data() any { return f.binaryRaw }
   250  
   251  func (f BinaryContent) Raw() json.RawMessage { return f.binaryRaw }
   252  
   253  func (f BinaryContent) String() string { return base64.StdEncoding.EncodeToString(f.binaryRaw) }
   254  
   255  func (f BinaryContent) EncodeJSON(buffer Buffer) (err error) {
   256  	if _, err = buffer.WriteString(`"data:;base64,`); err != nil {
   257  		return err
   258  	}
   259  	var encoder = base64.NewEncoder(base64.RawStdEncoding, buffer)
   260  	if _, err = encoder.Write(f.binaryRaw); err != nil {
   261  		return err
   262  	}
   263  	if err = encoder.Close(); err != nil {
   264  		return err
   265  	}
   266  	if err = buffer.WriteByte('"'); err != nil {
   267  		return err
   268  	}
   269  	return nil
   270  }
   271  
   272  func Binary(key string, val []byte) Field { return Field{key, NewBinaryContent(val)} }
   273  
   274  func Binarys(key string, valArr [][]byte) Field {
   275  	return Field{key, newArray(valArr, NewBinaryContent)}
   276  }
   277  
   278  // data type: bool
   279  
   280  type BoolContent bool
   281  
   282  func NewBoolField(val bool) Content { return BoolContent(val) }
   283  
   284  func (f BoolContent) Type() Type { return TypeBool }
   285  
   286  func (f BoolContent) Data() any { return bool(f) }
   287  
   288  func (f BoolContent) Raw() bool { return bool(f) }
   289  
   290  func (f BoolContent) EncodeJSON(buffer Buffer) error {
   291  	if f {
   292  		return errWithoutVal(buffer.Write([]byte{'t', 'r', 'u', 'e'}))
   293  	} else {
   294  		return errWithoutVal(buffer.Write([]byte{'f', 'a', 'l', 's', 'e'}))
   295  	}
   296  }
   297  
   298  func Bool(key string, val bool) Field { return Field{Key: key, Content: NewBoolField(val)} }
   299  
   300  func Bools(key string, valArr []bool) Field { return Field{key, newArray(valArr, NewBoolField)} }
   301  
   302  // data type: complex128
   303  
   304  type Complex128Content complex128
   305  
   306  func NewComplex128Content(val complex128) Content { return Complex128Content(val) }
   307  
   308  func (f Complex128Content) Type() Type { return TypeComplex }
   309  
   310  func (f Complex128Content) Data() any { return complex128(f) }
   311  
   312  func (f Complex128Content) Raw() complex128 { return complex128(f) }
   313  
   314  func (f Complex128Content) EncodeJSON(buffer Buffer) (err error) {
   315  	return appendJsonStringBuf(buffer, strconv.FormatComplex(complex128(f), 'f', -1, 128))
   316  }
   317  
   318  func Complex128(key string, val complex128) Field {
   319  	return Field{Key: key, Content: NewComplex128Content(val)}
   320  }
   321  
   322  func Complex128s(key string, nums []complex128) Field {
   323  	return Field{key, newArray(nums, NewComplex128Content)}
   324  }
   325  
   326  // data type: complex64
   327  
   328  type Complex64Content complex64
   329  
   330  func NewComplex64Content(val complex64) Content { return Complex64Content(val) }
   331  
   332  func (f Complex64Content) Type() Type { return TypeComplex }
   333  
   334  func (f Complex64Content) Data() any { return complex64(f) }
   335  
   336  func (f Complex64Content) Raw() complex64 { return complex64(f) }
   337  
   338  func (f Complex64Content) EncodeJSON(buffer Buffer) (err error) {
   339  	return appendJsonStringBuf(buffer, strconv.FormatComplex(complex128(f), 'f', -1, 64))
   340  }
   341  
   342  func Complex64(key string, val complex64) Field {
   343  	return Field{Key: key, Content: NewComplex64Content(val)}
   344  }
   345  
   346  func Complex64s(key string, nums []complex64) Field {
   347  	return Field{key, newArray(nums, NewComplex64Content)}
   348  }
   349  
   350  // data type: error
   351  
   352  type ErrorContent struct{ data error }
   353  
   354  func NewErrorContent(val error) Content { return ErrorContent{data: val} }
   355  
   356  func (f ErrorContent) Type() Type { return TypeError }
   357  
   358  func (f ErrorContent) Data() any { return f.data }
   359  
   360  func (f ErrorContent) Raw() error { return f.data }
   361  
   362  func (f ErrorContent) EncodeJSON(buffer Buffer) error {
   363  	if f.data == nil {
   364  		return errWithoutVal(buffer.Write([]byte{'n', 'u', 'l', 'l'}))
   365  	}
   366  	if content, err := asJsonErrMarshaler(f.data).MarshalJSON(); err == nil {
   367  		return errWithoutVal(buffer.Write(content))
   368  	} else {
   369  		return err
   370  	}
   371  }
   372  
   373  func Error(key string, err error) Field { return Field{Key: key, Content: NewErrorContent(err)} }
   374  
   375  func Errors(key string, errs []error) Field { return Field{key, newArray(errs, NewErrorContent)} }
   376  
   377  // data type: float32
   378  
   379  type Float32Content float32
   380  
   381  func NewFloat32Content(val float32) Content { return Float32Content(val) }
   382  
   383  func (f Float32Content) Type() Type { return TypeFloat }
   384  
   385  func (f Float32Content) Data() any { return float32(f) }
   386  
   387  func (f Float32Content) Raw() float32 { return float32(f) }
   388  
   389  func (f Float32Content) EncodeJSON(buffer Buffer) (err error) {
   390  	return errWithoutVal(buffer.WriteString(strconv.FormatFloat(float64(f), 'f', -1, 32)))
   391  }
   392  
   393  func Float32(key string, val float32) Field { return Field{Key: key, Content: NewFloat32Content(val)} }
   394  
   395  func Float32s(key string, nums []float32) Field { return Field{key, newArray(nums, NewFloat32Content)} }
   396  
   397  // data type: float64
   398  
   399  type Float64Content float64
   400  
   401  func NewFloat64Content(val float64) Content { return Float64Content(val) }
   402  
   403  func (f Float64Content) Type() Type { return TypeFloat }
   404  
   405  func (f Float64Content) Data() any { return float64(f) }
   406  
   407  func (f Float64Content) Raw() float64 { return float64(f) }
   408  
   409  func (f Float64Content) EncodeJSON(buffer Buffer) (err error) {
   410  	return errWithoutVal(buffer.WriteString(strconv.FormatFloat(float64(f), 'f', -1, 64)))
   411  }
   412  
   413  func Float64(key string, val float64) Field { return Field{Key: key, Content: NewFloat64Content(val)} }
   414  
   415  func Float64s(key string, nums []float64) Field { return Field{key, newArray(nums, NewFloat64Content)} }
   416  
   417  // data type: int
   418  
   419  type IntContent[T int | int8 | int16 | int32 | int64] struct {
   420  	data T
   421  }
   422  
   423  func NewIntContent[T int | int8 | int16 | int32 | int64](val T) Content {
   424  	return IntContent[T]{data: val}
   425  }
   426  
   427  func (f IntContent[T]) Type() Type { return TypeInt }
   428  
   429  func (f IntContent[T]) Data() any { return f.data }
   430  
   431  func (f IntContent[T]) Raw() T { return f.data }
   432  
   433  func (f IntContent[T]) EncodeJSON(buffer Buffer) (err error) {
   434  	return errWithoutVal(buffer.WriteString(strconv.FormatInt(int64(f.data), 10)))
   435  }
   436  
   437  func Int[T int | int8 | int16 | int32 | int64](key string, val T) Field {
   438  	return Field{Key: key, Content: NewIntContent(val)}
   439  }
   440  
   441  func Ints[T int | int8 | int16 | int32 | int64](key string, nums []T) Field {
   442  	return Field{key, newArray(nums, NewIntContent[T])}
   443  }
   444  
   445  func Int8(key string, val int8) Field {
   446  	return Field{Key: key, Content: NewIntContent(val)}
   447  }
   448  
   449  func Int8s(key string, nums []int8) Field {
   450  	return Field{key, newArray(nums, NewIntContent[int8])}
   451  }
   452  
   453  func Int16(key string, val int16) Field {
   454  	return Field{Key: key, Content: NewIntContent(val)}
   455  }
   456  
   457  func Int16s(key string, nums []int16) Field {
   458  	return Field{key, newArray(nums, NewIntContent[int16])}
   459  }
   460  
   461  func Int32(key string, val int32) Field {
   462  	return Field{Key: key, Content: NewIntContent(val)}
   463  }
   464  
   465  func Int32s(key string, nums []int32) Field {
   466  	return Field{key, newArray(nums, NewIntContent[int32])}
   467  }
   468  
   469  func Int64(key string, val int64) Field {
   470  	return Field{Key: key, Content: NewIntContent(val)}
   471  }
   472  
   473  func Int64s(key string, nums []int64) Field {
   474  	return Field{key, newArray(nums, NewIntContent[int64])}
   475  }
   476  
   477  // data type: uint
   478  
   479  type UintContent[T uint | uint8 | uint16 | uint32 | uint64] struct {
   480  	data T
   481  }
   482  
   483  func NewUintContent[T uint | uint8 | uint16 | uint32 | uint64](val T) Content {
   484  	return UintContent[T]{data: val}
   485  }
   486  
   487  func (f UintContent[T]) Type() Type { return TypeUint }
   488  
   489  func (f UintContent[T]) Data() any { return f.data }
   490  
   491  func (f UintContent[T]) Raw() T { return f.data }
   492  
   493  func (f UintContent[T]) EncodeJSON(buffer Buffer) (err error) {
   494  	return errWithoutVal(buffer.WriteString(strconv.FormatUint(uint64(f.data), 10)))
   495  }
   496  
   497  func Uint[T uint | uint8 | uint16 | uint32 | uint64](key string, val T) Field {
   498  	return Field{Key: key, Content: NewUintContent(val)}
   499  }
   500  
   501  func Uints[T uint | uint8 | uint16 | uint32 | uint64](key string, nums []T) Field {
   502  	return Field{key, newArray(nums, NewUintContent[T])}
   503  }
   504  
   505  func Uint8(key string, val uint8) Field {
   506  	return Field{Key: key, Content: NewUintContent(val)}
   507  }
   508  
   509  func Uint8s(key string, nums []uint8) Field {
   510  	return Field{key, newArray(nums, NewUintContent[uint8])}
   511  }
   512  
   513  func Uint16(key string, val uint16) Field {
   514  	return Field{Key: key, Content: NewUintContent(val)}
   515  }
   516  
   517  func Uint16s(key string, nums []uint16) Field {
   518  	return Field{key, newArray(nums, NewUintContent[uint16])}
   519  }
   520  
   521  func Uint32(key string, val uint32) Field {
   522  	return Field{Key: key, Content: NewUintContent(val)}
   523  }
   524  
   525  func Uint32s(key string, nums []uint32) Field {
   526  	return Field{key, newArray(nums, NewUintContent[uint32])}
   527  }
   528  
   529  func Uint64(key string, val uint64) Field {
   530  	return Field{Key: key, Content: NewUintContent(val)}
   531  }
   532  
   533  func Uint64s(key string, nums []uint64) Field {
   534  	return Field{key, newArray(nums, NewUintContent[uint64])}
   535  }
   536  
   537  // data type: uintptr
   538  
   539  type UintptrContent uintptr
   540  
   541  func NewUintptrContent(val uintptr) Content { return UintptrContent(val) }
   542  
   543  func (f UintptrContent) Type() Type { return TypeUintptr }
   544  
   545  func (f UintptrContent) Data() any { return uintptr(f) }
   546  
   547  func (f UintptrContent) Raw() uintptr { return uintptr(f) }
   548  
   549  func (f UintptrContent) EncodeJSON(buffer Buffer) (err error) {
   550  	return errWithoutVal(fmt.Fprintf(buffer, "%#0*x", 2*unsafe.Sizeof(f), f.Data()))
   551  }
   552  
   553  func Uintptr(key string, val uintptr) Field {
   554  	return Field{Key: key, Content: UintptrContent(val)}
   555  }
   556  
   557  func Uintptrs(key string, us []uintptr) Field {
   558  	return Field{Key: key, Content: newArray(us, NewUintptrContent)}
   559  }
   560  
   561  // data type: string
   562  
   563  type StringContent string
   564  
   565  func NewStringContent(val string) Content { return StringContent(val) }
   566  
   567  func (f StringContent) Type() Type { return TypeString }
   568  
   569  func (f StringContent) Data() any { return string(f) }
   570  
   571  func (f StringContent) Raw() string { return string(f) }
   572  
   573  func (f StringContent) EncodeJSON(buffer Buffer) error {
   574  	return appendJsonStringBuf(buffer, string(f))
   575  }
   576  
   577  func String(key string, val string) Field {
   578  	return Field{Key: key, Content: NewStringContent(val)}
   579  }
   580  
   581  func Strings(key string, valArr []string) Field {
   582  	return Field{Key: key, Content: newArray(valArr, NewStringContent)}
   583  }
   584  
   585  // data type: byteString
   586  
   587  func NewByteStringContent(val []byte) Content { return StringContent(val) }
   588  
   589  func ByteString(key string, val []byte) Field {
   590  	return Field{Key: key, Content: NewStringContent(string(val))}
   591  }
   592  
   593  func ByteStrings(key string, valArr [][]byte) Field {
   594  	return Field{Key: key, Content: newArray(valArr, NewByteStringContent)}
   595  }
   596  
   597  // data type: stringer
   598  
   599  type StringerContent struct{ data fmt.Stringer }
   600  
   601  func NewStringerContent[T fmt.Stringer](val T) Content { return StringerContent{data: val} }
   602  
   603  func (f StringerContent) Type() Type { return TypeStringer }
   604  
   605  func (f StringerContent) Data() any { return f.data }
   606  
   607  func (f StringerContent) Raw() fmt.Stringer { return f.data }
   608  
   609  func (f StringerContent) EncodeJSON(buffer Buffer) (err error) {
   610  	if f.data == nil {
   611  		return errWithoutVal(buffer.Write([]byte{'n', 'u', 'l', 'l'}))
   612  	}
   613  	return appendJsonStringBuf(buffer, fmt.Sprintf("%s", f.data))
   614  }
   615  
   616  func Stringer(key string, val fmt.Stringer) Field {
   617  	return Field{Key: key, Content: NewStringerContent(val)}
   618  }
   619  
   620  func Stringers[T fmt.Stringer](key string, valArr []T) Field {
   621  	return Field{Key: key, Content: newArray(valArr, NewStringerContent[T])}
   622  }
   623  
   624  // data type: time
   625  
   626  type TimeContent time.Time
   627  
   628  func NewTimeContent(val time.Time) Content { return TimeContent(val) }
   629  
   630  func (f TimeContent) Type() Type { return TypeTime }
   631  
   632  func (f TimeContent) Data() any { return time.Time(f) }
   633  
   634  func (f TimeContent) Raw() time.Time { return time.Time(f) }
   635  
   636  func (f TimeContent) EncodeJSON(buffer Buffer) error {
   637  	return appendJsonStringBuf(buffer, time.Time(f).Format(time.RFC3339))
   638  }
   639  
   640  func Time(key string, val time.Time) Field {
   641  	return Field{Key: key, Content: NewTimeContent(val)}
   642  }
   643  
   644  func Times(key string, valArr []time.Time) Field {
   645  	return Field{Key: key, Content: newArray(valArr, NewTimeContent)}
   646  }
   647  
   648  // data type: duration
   649  
   650  func Duration(key string, val time.Duration) Field { return Stringer(key, val) }
   651  
   652  func Durations(key string, valArr []time.Duration) Field { return Stringers(key, valArr) }
   653  
   654  func anyPointer[T any](key string, ptr *T, fn func(string, T) Field) Field {
   655  	if ptr == nil {
   656  		return Nil(key)
   657  	}
   658  	return fn(key, *ptr)
   659  }
   660  
   661  type AnyTypeInterceptor interface {
   662  	Priority() uint
   663  	Handle(reflectedType reflect.Type, val any) (Content, bool)
   664  }
   665  
   666  func Any(key string, val any) Field {
   667  	switch v := val.(type) {
   668  	case nil:
   669  		return Nil(key)
   670  	case bool:
   671  		return Bool(key, v)
   672  	case *bool:
   673  		return anyPointer(key, v, Bool)
   674  	case []bool:
   675  		return Bools(key, v)
   676  	case complex128:
   677  		return Complex128(key, v)
   678  	case *complex128:
   679  		return anyPointer(key, v, Complex128)
   680  	case []complex128:
   681  		return Complex128s(key, v)
   682  	case complex64:
   683  		return Complex64(key, v)
   684  	case *complex64:
   685  		return anyPointer(key, v, Complex64)
   686  	case []complex64:
   687  		return Complex64s(key, v)
   688  	case float64:
   689  		return Float64(key, v)
   690  	case *float64:
   691  		return anyPointer(key, v, Float64)
   692  	case []float64:
   693  		return Float64s(key, v)
   694  	case float32:
   695  		return Float32(key, v)
   696  	case *float32:
   697  		return anyPointer(key, v, Float32)
   698  	case []float32:
   699  		return Float32s(key, v)
   700  	case int:
   701  		return Int(key, v)
   702  	case *int:
   703  		return anyPointer(key, v, Int[int])
   704  	case []int:
   705  		return Ints(key, v)
   706  	case int64:
   707  		return Int(key, v)
   708  	case *int64:
   709  		return anyPointer(key, v, Int64)
   710  	case []int64:
   711  		return Int64s(key, v)
   712  	case int32:
   713  		return Int(key, v)
   714  	case *int32:
   715  		return anyPointer(key, v, Int32)
   716  	case []int32:
   717  		return Int32s(key, v)
   718  	case int16:
   719  		return Int(key, v)
   720  	case *int16:
   721  		return anyPointer(key, v, Int16)
   722  	case []int16:
   723  		return Int16s(key, v)
   724  	case int8:
   725  		return Int(key, v)
   726  	case *int8:
   727  		return anyPointer(key, v, Int8)
   728  	case []int8:
   729  		return Int8s(key, v)
   730  	case string:
   731  		return String(key, v)
   732  	case *string:
   733  		return anyPointer(key, v, String)
   734  	case []string:
   735  		return Strings(key, v)
   736  	case uint:
   737  		return Uint(key, v)
   738  	case *uint:
   739  		return anyPointer(key, v, Uint[uint])
   740  	case []uint:
   741  		return Uints(key, v)
   742  	case uint64:
   743  		return Uint(key, v)
   744  	case *uint64:
   745  		return anyPointer(key, v, Uint64)
   746  	case []uint64:
   747  		return Uint64s(key, v)
   748  	case uint32:
   749  		return Uint(key, v)
   750  	case *uint32:
   751  		return anyPointer(key, v, Uint32)
   752  	case []uint32:
   753  		return Uint32s(key, v)
   754  	case uint16:
   755  		return Uint(key, v)
   756  	case *uint16:
   757  		return anyPointer(key, v, Uint16)
   758  	case []uint16:
   759  		return Uint16s(key, v)
   760  	case uint8:
   761  		return Uint(key, v)
   762  	case *uint8:
   763  		return anyPointer(key, v, Uint8)
   764  	case []byte:
   765  		return Binary(key, v)
   766  	case uintptr:
   767  		return Uintptr(key, v)
   768  	case *uintptr:
   769  		return anyPointer(key, v, Uintptr)
   770  	case []uintptr:
   771  		return Uintptrs(key, v)
   772  	case time.Time:
   773  		return Time(key, v)
   774  	case *time.Time:
   775  		return anyPointer(key, v, Time)
   776  	case []time.Time:
   777  		return Times(key, v)
   778  	case time.Duration:
   779  		return Duration(key, v)
   780  	case *time.Duration:
   781  		return anyPointer(key, v, Duration)
   782  	case []time.Duration:
   783  		return Durations(key, v)
   784  	case error:
   785  		return Error(key, v)
   786  	case *error:
   787  		return anyPointer(key, v, Error)
   788  	case []error:
   789  		return Errors(key, v)
   790  	case fmt.Stringer:
   791  		return Stringer(key, v)
   792  	case json.Marshaler:
   793  		if content, err := v.MarshalJSON(); err != nil {
   794  			return Error(key, fmt.Errorf("cant marshal json: %w", err))
   795  		} else {
   796  			return JsonRawMessage(key, content)
   797  		}
   798  	case encoding.TextMarshaler:
   799  		if content, err := v.MarshalText(); err != nil {
   800  			return Error(key, fmt.Errorf("cant marshal text: %w", err))
   801  		} else {
   802  			return ByteString(key, content)
   803  		}
   804  	case encoding.BinaryMarshaler:
   805  		if content, err := v.MarshalBinary(); err != nil {
   806  			return Error(key, fmt.Errorf("cant marshal binary: %w", err))
   807  		} else {
   808  			return Binary(key, content)
   809  		}
   810  	}
   811  	return Error(key, fmt.Errorf("cant marshal field: no type matched"))
   812  }