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

     1  package field
     2  
     3  import (
     4  	"bytes"
     5  	"encoding"
     6  	"encoding/json"
     7  	"fmt"
     8  	"math"
     9  	"math/rand"
    10  	"reflect"
    11  	"testing"
    12  	"time"
    13  	"unsafe"
    14  )
    15  
    16  func TestJsonRawRawMessageField(t *testing.T) {
    17  	t.Run("newField", func(t *testing.T) {
    18  		if data := NewJSONContent([]byte("{[]}")).Data(); !reflect.DeepEqual(data, json.RawMessage("{[]}")) {
    19  			t.Errorf("invalid newAnyField response: %v", data)
    20  			return
    21  		}
    22  	})
    23  	t.Run("json", func(t *testing.T) {
    24  		var buf bytes.Buffer
    25  		if err := JsonRawMessage("case1", json.RawMessage("{[]}")).EncodeJSON(&buf); err != nil {
    26  			t.Error(fmt.Errorf("cant marshal any Field: %w", err))
    27  			return
    28  		}
    29  		if result := buf.String(); result != `"case1":{[]}` {
    30  			t.Errorf("invalid marshal JsonRawMessage result: %v", result)
    31  			return
    32  		}
    33  	})
    34  }
    35  
    36  func TestFieldList(t *testing.T) {
    37  	var list Fields
    38  	list = append(list, Bool("fieldBool", true))
    39  	list = append(list, Strings("fieldStrings", []string{"aaa", "bbb"}))
    40  	var expected = fmt.Sprintf(`{"fieldBool":true,"fieldStrings":["aaa","bbb"]}`)
    41  	if jBytes, err := list.MarshalJSON(); err != nil {
    42  		t.Error(err)
    43  	} else if !bytes.Equal(jBytes, []byte(expected)) {
    44  		t.Errorf("invalid marshal stringer result: `%s`, expected: `%s`", jBytes, expected)
    45  		return
    46  	}
    47  }
    48  
    49  func TestArrayFieldElem(t *testing.T) {
    50  	t.Run("emptyArray", func(t *testing.T) {
    51  		var buf bytes.Buffer
    52  		var _field = ArrayContent{}
    53  		if _data := _field.Data(); reflect.DeepEqual(_data, []Content{}) {
    54  			t.Errorf("invalid array data")
    55  			return
    56  		}
    57  		if err := _field.EncodeJSON(&buf); err != nil {
    58  			t.Error(fmt.Errorf("cant marshal array Field: %w", err))
    59  			return
    60  		}
    61  
    62  		if result := buf.String(); result != `[]` {
    63  			t.Errorf("invalid marshal array result: %v", result)
    64  			return
    65  		}
    66  	})
    67  	t.Run("dataArray", func(t *testing.T) {
    68  		var buf bytes.Buffer
    69  		var _fieldData = []Content{
    70  			NewJSONContent([]byte(`"test1"`)),
    71  			NewJSONContent([]byte(`true`)),
    72  			NewJSONContent([]byte(`undefined`)),
    73  		}
    74  		var _field = ArrayContent{arrayRaw: _fieldData}
    75  		if _data := _field.Data(); reflect.DeepEqual(_data, _fieldData) {
    76  			t.Errorf("invalid array data")
    77  			return
    78  		}
    79  		if err := _field.EncodeJSON(&buf); err != nil {
    80  			t.Error(fmt.Errorf("cant marshal array Field: %w", err))
    81  			return
    82  		}
    83  		if result := buf.String(); result != `["test1",true,undefined]` {
    84  			t.Errorf("invalid marshal array result: %v", result)
    85  			return
    86  		}
    87  	})
    88  }
    89  
    90  func TestBinaryField(t *testing.T) {
    91  	t.Run("newField", func(t *testing.T) {
    92  		if data := NewBinaryContent([]byte{0x12, 0x34}).Data(); !reflect.DeepEqual(data, []byte{0x12, 0x34}) {
    93  			t.Errorf("invalid NewBinaryContent response: %v", data)
    94  			return
    95  		}
    96  	})
    97  	t.Run("json", func(t *testing.T) {
    98  		var buf bytes.Buffer
    99  		if err := Binary("case1", []byte{0x12, 0x34}).EncodeJSON(&buf); err != nil {
   100  			t.Error(fmt.Errorf("cant marshal binary Field: %w", err))
   101  			return
   102  		}
   103  		buf.WriteByte(',')
   104  		if err := Binarys("case2", [][]byte{{0x12, 0x34}, {0x56, 0x78}}).EncodeJSON(&buf); err != nil {
   105  			t.Error(fmt.Errorf("cant marshal binary Field: %w", err))
   106  			return
   107  		}
   108  		if result := buf.String(); result != `"case1":"data:;base64,EjQ","case2":["data:;base64,EjQ","data:;base64,Vng"]` {
   109  			t.Errorf("invalid marshal binary result: %v", result)
   110  			return
   111  		}
   112  	})
   113  }
   114  
   115  func TestBoolField(t *testing.T) {
   116  	t.Run("newField", func(t *testing.T) {
   117  		if data := NewBoolField(true).Data(); data != true {
   118  			t.Errorf("invalid NewBoolField response: %v", data)
   119  			return
   120  		}
   121  		if data := NewBoolField(false).Data(); data != false {
   122  			t.Errorf("invalid NewBoolField response: %v", data)
   123  			return
   124  		}
   125  	})
   126  	t.Run("json", func(t *testing.T) {
   127  		var buf bytes.Buffer
   128  		if err := Bool("case1", true).EncodeJSON(&buf); err != nil {
   129  			t.Error(fmt.Errorf("cant marshal bool Field: %w", err))
   130  			return
   131  		}
   132  		buf.WriteByte(',')
   133  		if err := Bools("case2", []bool{false, true}).EncodeJSON(&buf); err != nil {
   134  			t.Error(fmt.Errorf("cant marshal bool Field: %w", err))
   135  			return
   136  		}
   137  		if result := buf.String(); result != `"case1":true,"case2":[false,true]` {
   138  			t.Errorf("invalid marshal bool result: %v", result)
   139  			return
   140  		}
   141  	})
   142  }
   143  
   144  func TestComplex64Field(t *testing.T) {
   145  	t.Run("newField", func(t *testing.T) {
   146  		if data := NewComplex64Content(complex(3, -5)).Data(); data != complex64(complex(3, -5)) {
   147  			t.Errorf("invalid NewComplex64Content response: %v", data)
   148  			return
   149  		}
   150  	})
   151  	t.Run("json", func(t *testing.T) {
   152  		var buf bytes.Buffer
   153  		if err := Complex64("case1", complex(3, -5)).EncodeJSON(&buf); err != nil {
   154  			t.Error(fmt.Errorf("cant marshal complex64 Field: %w", err))
   155  			return
   156  		}
   157  		buf.WriteByte(',')
   158  		if err := Complex64s("case2", []complex64{complex(3, -5), complex(4, 6)}).EncodeJSON(&buf); err != nil {
   159  			t.Error(fmt.Errorf("cant marshal complex64 Field: %w", err))
   160  			return
   161  		}
   162  		if result := buf.String(); result != `"case1":"(3-5i)","case2":["(3-5i)","(4+6i)"]` {
   163  			t.Errorf("invalid marshal complex64 result: %v", result)
   164  			return
   165  		}
   166  	})
   167  }
   168  
   169  func TestComplex128Field(t *testing.T) {
   170  	t.Run("newField", func(t *testing.T) {
   171  		if data := NewComplex128Content(complex(3, -5)).Data(); data != complex(3, -5) {
   172  			t.Errorf("invalid NewComplex128Content response: %v", data)
   173  			return
   174  		}
   175  	})
   176  	t.Run("json", func(t *testing.T) {
   177  		var buf bytes.Buffer
   178  		if err := Complex128("case1", complex(3, -5)).EncodeJSON(&buf); err != nil {
   179  			t.Error(fmt.Errorf("cant marshal complex128 Field: %w", err))
   180  			return
   181  		}
   182  		buf.WriteByte(',')
   183  		if err := Complex128s("case2", []complex128{complex(3, -5), complex(4, 6)}).EncodeJSON(&buf); err != nil {
   184  			t.Error(fmt.Errorf("cant marshal complex64 Field: %w", err))
   185  			return
   186  		}
   187  		if result := buf.String(); result != `"case1":"(3-5i)","case2":["(3-5i)","(4+6i)"]` {
   188  			t.Errorf("invalid marshal complex128 result: %v", result)
   189  			return
   190  		}
   191  	})
   192  }
   193  
   194  func TestErrorField(t *testing.T) {
   195  	var testErr = fmt.Errorf("test error")
   196  	t.Run("newField", func(t *testing.T) {
   197  		if data := NewErrorContent(testErr).Data(); data != testErr {
   198  			t.Errorf("invalid NewErrorContent response: %v", data)
   199  			return
   200  		}
   201  	})
   202  	t.Run("json", func(t *testing.T) {
   203  		var buf bytes.Buffer
   204  		if err := Error("case1", testErr).EncodeJSON(&buf); err != nil {
   205  			t.Error(fmt.Errorf("cant marshal error Field: %w", err))
   206  			return
   207  		}
   208  		buf.WriteByte(',')
   209  		if err := Errors("case2", []error{testErr, nil}).EncodeJSON(&buf); err != nil {
   210  			t.Error(fmt.Errorf("cant marshal error Field: %w", err))
   211  			return
   212  		}
   213  		if result := buf.String(); result != `"case1":"test error","case2":["test error",null]` {
   214  			t.Errorf("invalid marshal error result: %v", result)
   215  			return
   216  		}
   217  	})
   218  }
   219  
   220  func TestFloat32Field(t *testing.T) {
   221  	t.Run("newField", func(t *testing.T) {
   222  		if data := NewFloat32Content(1.5).Data(); data != float32(1.5) {
   223  			t.Errorf("invalid NewFloat32Content response: %v", data)
   224  			return
   225  		}
   226  	})
   227  	t.Run("json", func(t *testing.T) {
   228  		var buf bytes.Buffer
   229  		if err := Float32("case1", 1.5).EncodeJSON(&buf); err != nil {
   230  			t.Error(fmt.Errorf("cant marshal float32 Field: %w", err))
   231  			return
   232  		}
   233  		buf.WriteByte(',')
   234  		if err := Float32s("case2", []float32{3.9, 8.15}).EncodeJSON(&buf); err != nil {
   235  			t.Error(fmt.Errorf("cant marshal float32 Field: %w", err))
   236  			return
   237  		}
   238  		if result := buf.String(); result != `"case1":1.5,"case2":[3.9,8.15]` {
   239  			t.Errorf("invalid marshal float32 result: %v", result)
   240  			return
   241  		}
   242  	})
   243  }
   244  
   245  func TestFloat64Field(t *testing.T) {
   246  	t.Run("newField", func(t *testing.T) {
   247  		if data := NewFloat64Content(1.5).Data(); data != float64(1.5) {
   248  			t.Errorf("invalid NewFloat64Content response: %v", data)
   249  			return
   250  		}
   251  	})
   252  	t.Run("json", func(t *testing.T) {
   253  		var buf bytes.Buffer
   254  		if err := Float64("case1", 1.5).EncodeJSON(&buf); err != nil {
   255  			t.Error(fmt.Errorf("cant marshal float64 Field: %w", err))
   256  			return
   257  		}
   258  		buf.WriteByte(',')
   259  		if err := Float64s("case2", []float64{3.9, 8.15}).EncodeJSON(&buf); err != nil {
   260  			t.Error(fmt.Errorf("cant marshal float64 Field: %w", err))
   261  			return
   262  		}
   263  		if result := buf.String(); result != `"case1":1.5,"case2":[3.9,8.15]` {
   264  			t.Errorf("invalid marshal float64 result: %v", result)
   265  			return
   266  		}
   267  	})
   268  }
   269  
   270  func TestInt64Field(t *testing.T) {
   271  	var int64A, int64B = rand.Int63(), rand.Int63()
   272  	t.Run("newField", func(t *testing.T) {
   273  		if data := NewIntContent[int64](int64A).Data(); data != int64A {
   274  			t.Errorf("invalid newInt64Field response: %v", data)
   275  			return
   276  		}
   277  	})
   278  	t.Run("json", func(t *testing.T) {
   279  		var buf bytes.Buffer
   280  		if err := Int64("case1", int64A).EncodeJSON(&buf); err != nil {
   281  			t.Error(fmt.Errorf("cant marshal int64 Field: %w", err))
   282  			return
   283  		}
   284  		buf.WriteByte(',')
   285  		if err := Int64s("case2", []int64{int64A, int64B}).EncodeJSON(&buf); err != nil {
   286  			t.Error(fmt.Errorf("cant marshal int64 Field: %w", err))
   287  			return
   288  		}
   289  		if result := buf.String(); result != fmt.Sprintf(`"case1":%d,"case2":[%d,%d]`, int64A, int64A, int64B) {
   290  			t.Errorf("invalid marshal int64 result: %v", result)
   291  			return
   292  		}
   293  	})
   294  }
   295  
   296  func TestUint64Field(t *testing.T) {
   297  	var uint64A, uint64B = rand.Uint64(), rand.Uint64()
   298  	t.Run("newField", func(t *testing.T) {
   299  		if data := NewUintContent[uint64](uint64A).Data(); data != uint64A {
   300  			t.Errorf("invalid newUint64Field response: %v", data)
   301  			return
   302  		}
   303  	})
   304  	t.Run("json", func(t *testing.T) {
   305  		var buf bytes.Buffer
   306  		if err := Uint64("case1", uint64A).EncodeJSON(&buf); err != nil {
   307  			t.Error(fmt.Errorf("cant marshal uint64 Field: %w", err))
   308  			return
   309  		}
   310  		buf.WriteByte(',')
   311  		if err := Uint64s("case2", []uint64{uint64A, uint64B}).EncodeJSON(&buf); err != nil {
   312  			t.Error(fmt.Errorf("cant marshal uint64 Field: %w", err))
   313  			return
   314  		}
   315  		if result := buf.String(); result != fmt.Sprintf(`"case1":%d,"case2":[%d,%d]`, uint64A, uint64A, uint64B) {
   316  			t.Errorf("invalid marshal uint64 result: %v", result)
   317  			return
   318  		}
   319  	})
   320  }
   321  
   322  func TestUintptrField(t *testing.T) {
   323  	var uintptrA, uintptrB = uintptr(1), uintptr(2)
   324  	t.Run("newField", func(t *testing.T) {
   325  		if data := NewUintptrContent(uintptrA).Data(); data != uintptrA {
   326  			t.Errorf("invalid NewUintptrContent response: %v", data)
   327  			return
   328  		}
   329  	})
   330  	t.Run("json", func(t *testing.T) {
   331  		var buf bytes.Buffer
   332  		if err := Uintptr("case1", uintptrA).EncodeJSON(&buf); err != nil {
   333  			t.Error(fmt.Errorf("cant marshal uintptr Field: %w", err))
   334  			return
   335  		}
   336  		buf.WriteByte(',')
   337  		if err := Uintptrs("case2", []uintptr{uintptrA, uintptrB}).EncodeJSON(&buf); err != nil {
   338  			t.Error(fmt.Errorf("cant marshal uintptr Field: %w", err))
   339  			return
   340  		}
   341  		var expected = fmt.Sprintf(
   342  			`"case1":%#0*x,"case2":[%#0*x,%#0*x]`,
   343  			2*unsafe.Sizeof(uintptrA), uintptrA,
   344  			2*unsafe.Sizeof(uintptrA), uintptrA,
   345  			2*unsafe.Sizeof(uintptrB), uintptrB,
   346  		)
   347  		if result := buf.String(); result != expected {
   348  			t.Errorf("invalid marshal uintptr result: %v, expected: %s", result, expected)
   349  			return
   350  		}
   351  	})
   352  }
   353  
   354  func TestStringField(t *testing.T) {
   355  	var stringA, stringB = "t0001", "t0002测试"
   356  	t.Run("newField", func(t *testing.T) {
   357  		if data := NewStringContent(stringA).Data(); data != stringA {
   358  			t.Errorf("invalid NewStringContent response: %v", data)
   359  			return
   360  		}
   361  	})
   362  	t.Run("json", func(t *testing.T) {
   363  		var buf bytes.Buffer
   364  		if err := String("case1", stringA).EncodeJSON(&buf); err != nil {
   365  			t.Error(fmt.Errorf("cant marshal string Field: %w", err))
   366  			return
   367  		}
   368  		buf.WriteByte(',')
   369  		if err := Strings("case2", []string{stringA, stringB}).EncodeJSON(&buf); err != nil {
   370  			t.Error(fmt.Errorf("cant marshal string Field: %w", err))
   371  			return
   372  		}
   373  		var expected = fmt.Sprintf(`"case1":%q,"case2":[%q,%q]`, stringA, stringA, stringB)
   374  		if result := buf.String(); result != expected {
   375  			t.Errorf("invalid marshal string result: `%v`, expected: `%s`", result, expected)
   376  			return
   377  		}
   378  	})
   379  }
   380  
   381  type testStringer struct {
   382  	val string
   383  }
   384  
   385  func (s *testStringer) String() string { return s.val }
   386  
   387  func TestStringerField(t *testing.T) {
   388  	var stringerA, stringerB = &testStringer{val: "t0001"}, &testStringer{val: "t0002"}
   389  	t.Run("newField", func(t *testing.T) {
   390  		if data := NewStringerContent(stringerA).Data(); fmt.Sprint(data) != fmt.Sprint(stringerA) {
   391  			t.Errorf("invalid NewStringerContent response: %v", data)
   392  			return
   393  		}
   394  	})
   395  	t.Run("json", func(t *testing.T) {
   396  		var buf bytes.Buffer
   397  		if err := Stringer("case1", stringerA).EncodeJSON(&buf); err != nil {
   398  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   399  			return
   400  		}
   401  		buf.WriteByte(',')
   402  		if err := Stringers("case2", []fmt.Stringer{stringerA, stringerB}).EncodeJSON(&buf); err != nil {
   403  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   404  			return
   405  		}
   406  		buf.WriteByte(',')
   407  		if err := Stringer("case3", nil).EncodeJSON(&buf); err != nil {
   408  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   409  			return
   410  		}
   411  		buf.WriteByte(',')
   412  		var testPtr *testStringer
   413  		if err := Stringer("case4", testPtr).EncodeJSON(&buf); err != nil {
   414  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   415  			return
   416  		}
   417  		var expected = fmt.Sprintf(`"case1":%q,"case2":[%q,%q],"case3":null,"case4":"<nil>"`, stringerA.val, stringerA.val, stringerB.val)
   418  		if result := buf.String(); result != expected {
   419  			t.Errorf("invalid marshal stringer result: `%v`, expected: `%s`", result, expected)
   420  			return
   421  		}
   422  	})
   423  }
   424  
   425  func TestByteStringField(t *testing.T) {
   426  	var byteStringA, byteStringB = []byte("t0001"), []byte("t0002")
   427  	t.Run("newField", func(t *testing.T) {
   428  		if data := NewByteStringContent(byteStringA).Data(); !reflect.DeepEqual(data, string(byteStringA)) {
   429  			t.Errorf("invalid NewByteStringContent response: %v", data)
   430  			return
   431  		}
   432  	})
   433  	t.Run("json", func(t *testing.T) {
   434  		var buf bytes.Buffer
   435  		if err := ByteString("case1", byteStringA).EncodeJSON(&buf); err != nil {
   436  			t.Error(fmt.Errorf("cant marshal byteString Field: %w", err))
   437  			return
   438  		}
   439  		buf.WriteByte(',')
   440  		if err := ByteStrings("case2", [][]byte{byteStringA, byteStringB}).EncodeJSON(&buf); err != nil {
   441  			t.Error(fmt.Errorf("cant marshal byteString Field: %w", err))
   442  			return
   443  		}
   444  		var expected = fmt.Sprintf(`"case1":%q,"case2":[%q,%q]`, byteStringA, byteStringA, byteStringB)
   445  		if result := buf.String(); result != expected {
   446  			t.Errorf("invalid marshal byteString result: `%v`, expected: `%s`", result, expected)
   447  			return
   448  		}
   449  	})
   450  }
   451  
   452  func TestTimeField(t *testing.T) {
   453  	var timezoneB = time.FixedZone("TTT", 60*60*7)
   454  	var timeA, timeB = time.Now(), time.Date(1111, 5, 20, 23, 15, 16, 999111, timezoneB)
   455  	t.Run("newField", func(t *testing.T) {
   456  		if data := NewTimeContent(timeA).Data(); data != timeA {
   457  			t.Errorf("invalid NewTimeContent response: %v", data)
   458  			return
   459  		}
   460  	})
   461  	t.Run("json", func(t *testing.T) {
   462  		var buf bytes.Buffer
   463  		if err := Time("case1", timeA).EncodeJSON(&buf); err != nil {
   464  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   465  			return
   466  		}
   467  		buf.WriteByte(',')
   468  		if err := Times("case2", []time.Time{timeA, timeB}).EncodeJSON(&buf); err != nil {
   469  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   470  			return
   471  		}
   472  		var expected = fmt.Sprintf(`"case1":%q,"case2":[%q,%q]`, timeA.Format(time.RFC3339), timeA.Format(time.RFC3339), timeB.Format(time.RFC3339))
   473  		if result := buf.String(); result != expected {
   474  			t.Errorf("invalid marshal stringer result: `%v`, expected: `%s`", result, expected)
   475  			return
   476  		}
   477  	})
   478  }
   479  
   480  func TestDurationField(t *testing.T) {
   481  	var durationA, durationB = time.Hour - time.Minute*16, time.Minute - time.Second*5
   482  	t.Run("newField", func(t *testing.T) {
   483  		if data := NewStringerContent(durationA).Data(); data != durationA {
   484  			t.Errorf("invalid newDurationField response: %v", data)
   485  			return
   486  		}
   487  	})
   488  	t.Run("json", func(t *testing.T) {
   489  		var buf bytes.Buffer
   490  		if err := Duration("case1", durationA).EncodeJSON(&buf); err != nil {
   491  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   492  			return
   493  		}
   494  		buf.WriteByte(',')
   495  		if err := Durations("case2", []time.Duration{durationA, durationB}).EncodeJSON(&buf); err != nil {
   496  			t.Error(fmt.Errorf("cant marshal stringer Field: %w", err))
   497  			return
   498  		}
   499  		var expected = fmt.Sprintf(`"case1":%q,"case2":[%q,%q]`, durationA, durationA, durationB)
   500  		if result := buf.String(); result != expected {
   501  			t.Errorf("invalid marshal stringer result: `%v`, expected: `%s`", result, expected)
   502  			return
   503  		}
   504  	})
   505  }
   506  
   507  type testJSONMarshaler struct{}
   508  
   509  func (m testJSONMarshaler) MarshalJSON() ([]byte, error) {
   510  	return []byte(`{"testMarshaler": "123456"}`), nil
   511  }
   512  
   513  type testTextMarshaler struct{}
   514  
   515  func (m testTextMarshaler) MarshalText() ([]byte, error) {
   516  	return []byte("testMarshaler"), nil
   517  }
   518  
   519  type testBinaryMarshaler struct{}
   520  
   521  func (m testBinaryMarshaler) MarshalBinary() ([]byte, error) {
   522  	return []byte{0x12, 0x34, 0x56}, nil
   523  }
   524  
   525  type funcContent struct{ fn any }
   526  
   527  func (f funcContent) Type() Type { return TypeAny }
   528  
   529  func (f funcContent) Data() any { return f.fn }
   530  
   531  func (f funcContent) EncodeJSON(buffer Buffer) error {
   532  	return errWithoutVal(fmt.Fprintf(buffer, "%q", reflect.TypeOf(f.fn)))
   533  }
   534  
   535  type funcTypeInterceptor struct {
   536  }
   537  
   538  func (f funcTypeInterceptor) Priority() uint { return 20 }
   539  
   540  func (f funcTypeInterceptor) Handle(reflectedType reflect.Type, val any) (Content, bool) {
   541  	if reflectedType.Kind() == reflect.Func {
   542  		return funcContent{fn: val}, true
   543  	}
   544  	return nil, false
   545  }
   546  
   547  func TestAnyField(t *testing.T) {
   548  	var (
   549  		boolVal       bool                     = true
   550  		complex128Val complex128               = complex(0, 0)
   551  		complex64Val  complex64                = complex(0, 0)
   552  		durationVal   time.Duration            = time.Second
   553  		float64Val    float64                  = math.MaxFloat64
   554  		float32Val    float32                  = math.MaxFloat32
   555  		intVal        int                      = math.MaxInt
   556  		int64Val      int64                    = math.MaxInt64
   557  		int32Val      int32                    = math.MaxInt32
   558  		int16Val      int16                    = math.MaxInt16
   559  		int8Val       int8                     = math.MaxInt8
   560  		stringVal     string                   = "hello"
   561  		timeVal       time.Time                = time.Unix(100000, 0)
   562  		uintVal       uint                     = math.MaxUint
   563  		uint64Val     uint64                   = math.MaxUint64
   564  		uint32Val     uint32                   = math.MaxUint32
   565  		uint16Val     uint16                   = math.MaxUint16
   566  		uint8Val      uint8                    = math.MaxUint8
   567  		uintptrVal    uintptr                  = 1
   568  		errorVal      error                    = fmt.Errorf("testError")
   569  		stringerVal   fmt.Stringer             = &testStringer{val: "hello world"}
   570  		jsonMarshaler json.Marshaler           = &testJSONMarshaler{}
   571  		textMarshaler encoding.TextMarshaler   = &testTextMarshaler{}
   572  		binMarshaler  encoding.BinaryMarshaler = &testBinaryMarshaler{}
   573  	)
   574  
   575  	tests := []struct {
   576  		name   string
   577  		field  Field
   578  		expect Field
   579  	}{
   580  		{"Any:Nil", Any("k", nil), Nil("k")},
   581  		{"Any:Bool", Any("k", true), Bool("k", true)},
   582  		{"Any:Bools", Any("k", []bool{true}), Bools("k", []bool{true})},
   583  		{"Any:Byte", Any("k", byte(1)), Uint8("k", 1)},
   584  		{"Any:Bytes", Any("k", []byte{1}), Binary("k", []byte{1})},
   585  		{"Any:Complex128", Any("k", 1+2i), Complex128("k", 1+2i)},
   586  		{"Any:Complex128s", Any("k", []complex128{1 + 2i}), Complex128s("k", []complex128{1 + 2i})},
   587  		{"Any:Complex64", Any("k", complex64(1+2i)), Complex64("k", 1+2i)},
   588  		{"Any:Complex64s", Any("k", []complex64{1 + 2i}), Complex64s("k", []complex64{1 + 2i})},
   589  		{"Any:Float64", Any("k", 3.14), Float64("k", 3.14)},
   590  		{"Any:Float64s", Any("k", []float64{3.14}), Float64s("k", []float64{3.14})},
   591  		{"Any:Float32", Any("k", float32(3.14)), Float32("k", 3.14)},
   592  		{"Any:Float32s", Any("k", []float32{3.14}), Float32s("k", []float32{3.14})},
   593  		{"Any:Int", Any("k", 1), Int("k", 1)},
   594  		{"Any:Ints", Any("k", []int{1}), Ints("k", []int{1})},
   595  		{"Any:Int64", Any("k", int64(1)), Int64("k", 1)},
   596  		{"Any:Int64s", Any("k", []int64{1}), Int64s("k", []int64{1})},
   597  		{"Any:Int32", Any("k", int32(1)), Int32("k", 1)},
   598  		{"Any:Int32s", Any("k", []int32{1}), Int32s("k", []int32{1})},
   599  		{"Any:Int16", Any("k", int16(1)), Int16("k", 1)},
   600  		{"Any:Int16s", Any("k", []int16{1}), Int16s("k", []int16{1})},
   601  		{"Any:Int8", Any("k", int8(1)), Int8("k", 1)},
   602  		{"Any:Int8s", Any("k", []int8{1}), Int8s("k", []int8{1})},
   603  		{"Any:Rune", Any("k", rune(1)), Int32("k", 1)},
   604  		{"Any:Runes", Any("k", []rune{1}), Int32s("k", []int32{1})},
   605  		{"Any:String", Any("k", "v"), String("k", "v")},
   606  		{"Any:Strings", Any("k", []string{"v"}), Strings("k", []string{"v"})},
   607  		{"Any:Uint", Any("k", uint(1)), Uint("k", uint(1))},
   608  		{"Any:Uints", Any("k", []uint{1}), Uints("k", []uint{1})},
   609  		{"Any:Uint64", Any("k", uint64(1)), Uint64("k", 1)},
   610  		{"Any:Uint64s", Any("k", []uint64{1}), Uint64s("k", []uint64{1})},
   611  		{"Any:Uint32", Any("k", uint32(1)), Uint32("k", 1)},
   612  		{"Any:Uint32s", Any("k", []uint32{1}), Uint32s("k", []uint32{1})},
   613  		{"Any:Uint16", Any("k", uint16(1)), Uint16("k", 1)},
   614  		{"Any:Uint16s", Any("k", []uint16{1}), Uint16s("k", []uint16{1})},
   615  		{"Any:Uint8", Any("k", uint8(1)), Uint8("k", 1)},
   616  		{"Any:Uint8s", Any("k", []uint8{1}), Binary("k", []uint8{1})},
   617  		{"Any:Uintptr", Any("k", uintptr(1)), Uintptr("k", 1)},
   618  		{"Any:Uintptrs", Any("k", []uintptr{1}), Uintptrs("k", []uintptr{1})},
   619  		{"Any:Time", Any("k", time.Unix(0, 0)), Time("k", time.Unix(0, 0))},
   620  		{"Any:Times", Any("k", []time.Time{time.Unix(0, 0)}), Times("k", []time.Time{time.Unix(0, 0)})},
   621  		{"Any:Duration", Any("k", time.Second), Duration("k", time.Second)},
   622  		{"Any:Durations", Any("k", []time.Duration{time.Second}), Durations("k", []time.Duration{time.Second})},
   623  		{"Any:Error", Any("k", errorVal), Error("k", errorVal)},
   624  		{"Any:fmtStringer", Any("k", stringerVal), Stringer("k", stringerVal)},
   625  		{"Any:jsonMarshaler", Any("k", jsonMarshaler), JsonRawMessage("k", valWithoutErr(jsonMarshaler.MarshalJSON()))},
   626  		{"Any:textMarshaler", Any("k", textMarshaler), ByteString("k", valWithoutErr(textMarshaler.MarshalText()))},
   627  		{"Any:binaryMarshaler", Any("k", binMarshaler), Binary("k", valWithoutErr(binMarshaler.MarshalBinary()))},
   628  		{"Any:PtrBool", Any("k", (*bool)(nil)), Nil("k")},
   629  		{"Any:PtrBool", Any("k", &boolVal), Bool("k", boolVal)},
   630  		{"Any:PtrComplex128", Any("k", (*complex128)(nil)), Nil("k")},
   631  		{"Any:PtrComplex128", Any("k", &complex128Val), Complex128("k", complex128Val)},
   632  		{"Any:PtrComplex64", Any("k", (*complex64)(nil)), Nil("k")},
   633  		{"Any:PtrComplex64", Any("k", &complex64Val), Complex64("k", complex64Val)},
   634  		{"Any:PtrDuration", Any("k", (*time.Duration)(nil)), Nil("k")},
   635  		{"Any:PtrDuration", Any("k", &durationVal), Duration("k", durationVal)},
   636  		{"Any:PtrFloat64", Any("k", (*float64)(nil)), Nil("k")},
   637  		{"Any:PtrFloat64", Any("k", &float64Val), Float64("k", float64Val)},
   638  		{"Any:PtrFloat32", Any("k", (*float32)(nil)), Nil("k")},
   639  		{"Any:PtrFloat32", Any("k", &float32Val), Float32("k", float32Val)},
   640  		{"Any:PtrInt", Any("k", (*int)(nil)), Nil("k")},
   641  		{"Any:PtrInt", Any("k", &intVal), Int("k", intVal)},
   642  		{"Any:PtrInt64", Any("k", (*int64)(nil)), Nil("k")},
   643  		{"Any:PtrInt64", Any("k", &int64Val), Int64("k", int64Val)},
   644  		{"Any:PtrInt32", Any("k", (*int32)(nil)), Nil("k")},
   645  		{"Any:PtrInt32", Any("k", &int32Val), Int32("k", int32Val)},
   646  		{"Any:PtrInt16", Any("k", (*int16)(nil)), Nil("k")},
   647  		{"Any:PtrInt16", Any("k", &int16Val), Int16("k", int16Val)},
   648  		{"Any:PtrInt8", Any("k", (*int8)(nil)), Nil("k")},
   649  		{"Any:PtrInt8", Any("k", &int8Val), Int8("k", int8Val)},
   650  		{"Any:PtrString", Any("k", (*string)(nil)), Nil("k")},
   651  		{"Any:PtrString", Any("k", &stringVal), String("k", stringVal)},
   652  		{"Any:PtrTime", Any("k", (*time.Time)(nil)), Nil("k")},
   653  		{"Any:PtrTime", Any("k", &timeVal), Time("k", timeVal)},
   654  		{"Any:PtrUint", Any("k", (*uint)(nil)), Nil("k")},
   655  		{"Any:PtrUint", Any("k", &uintVal), Uint("k", uintVal)},
   656  		{"Any:PtrUint64", Any("k", (*uint64)(nil)), Nil("k")},
   657  		{"Any:PtrUint64", Any("k", &uint64Val), Uint64("k", uint64Val)},
   658  		{"Any:PtrUint32", Any("k", (*uint32)(nil)), Nil("k")},
   659  		{"Any:PtrUint32", Any("k", &uint32Val), Uint32("k", uint32Val)},
   660  		{"Any:PtrUint16", Any("k", (*uint16)(nil)), Nil("k")},
   661  		{"Any:PtrUint16", Any("k", &uint16Val), Uint16("k", uint16Val)},
   662  		{"Any:PtrUint8", Any("k", (*uint8)(nil)), Nil("k")},
   663  		{"Any:PtrUint8", Any("k", &uint8Val), Uint8("k", uint8Val)},
   664  		{"Any:PtrUintptr", Any("k", (*uintptr)(nil)), Nil("k")},
   665  		{"Any:PtrUintptr", Any("k", &uintptrVal), Uintptr("k", uintptrVal)},
   666  		{"Any:PtrError", Any("k", &errorVal), Error("k", errorVal)},
   667  	}
   668  
   669  	for _, testItem := range tests {
   670  		t.Run(testItem.name, func(t *testing.T) {
   671  			var buf1, buf2 bytes.Buffer
   672  			if err := testItem.field.EncodeJSON(&buf1); err != nil {
   673  				t.Error(err)
   674  				return
   675  			}
   676  			if err := testItem.expect.EncodeJSON(&buf2); err != nil {
   677  				t.Error(err)
   678  				return
   679  			}
   680  			if b1, b2 := buf1.Bytes(), buf2.Bytes(); !bytes.Equal(b1, b2) {
   681  				t.Error(fmt.Errorf("not equal:\n\texpected: %s\n\tgot:%s", b2, b1))
   682  			}
   683  		})
   684  	}
   685  }