github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue141_test.go (about)

     1  /*
     2  * Copyright 2021 ByteDance Inc.
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *     http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	. "github.com/goshafaq/sonic"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  type Issue141_Case_Insentive1 struct {
    29  	Field0 int `json:"foo"`
    30  	Field1 int `json:"FOO"`
    31  	Field2 int `json:"FoO"`
    32  	FOo    int
    33  }
    34  
    35  type Issue141_Case_Insentive2 struct {
    36  	Field0 int `json:"FOO"`
    37  	Field1 int `json:"foo"`
    38  	Field2 int `json:"FoO"`
    39  	FOo    int
    40  }
    41  
    42  type Issue141_Case_Insentive3 struct {
    43  	FOo    int
    44  	Field0 int `json:"FoO"`
    45  	Field1 int `json:"foo"`
    46  	Field2 int `json:"FOO"`
    47  }
    48  
    49  type Issue141_Case_Insentive4 struct {
    50  	foo    int
    51  	Field0 int `json:"FoO"`
    52  	Field1 int `json:"foo"`
    53  	Field2 int `json:"FOO"`
    54  }
    55  
    56  type Issue141_Matched1 struct {
    57  	Field0 int `json:"FOO"`
    58  	Field1 int `json:"foo"`
    59  	Field2 int `json:"FoO"`
    60  	Foo    int
    61  }
    62  
    63  type Issue141_Matched2 struct {
    64  	Field0 int `json:"FOO"`
    65  	Field1 int `json:"foo"`
    66  	Field2 int `json:"FoO"`
    67  	Foo    int
    68  	Field3 int `json:"Foo"`
    69  }
    70  
    71  // Struct field priority in unmarshal, see https://go.dev/blog/json
    72  func TestIssue141_StructFieldPriority(t *testing.T) {
    73  	data := []byte("{\"Foo\":1}")
    74  	for _, factory := range []func() interface{}{
    75  		func() interface{} { return new(Issue141_Case_Insentive1) },
    76  		func() interface{} { return new(Issue141_Case_Insentive2) },
    77  		func() interface{} { return new(Issue141_Case_Insentive3) },
    78  		func() interface{} { return new(Issue141_Case_Insentive4) },
    79  		func() interface{} { return new(Issue141_Matched1) },
    80  		func() interface{} { return new(Issue141_Matched2) },
    81  	} {
    82  		v1, v2 := factory(), factory()
    83  		err1 := json.Unmarshal(data, &v1)
    84  		err2 := Unmarshal(data, &v2)
    85  		require.NoError(t, err1)
    86  		require.NoError(t, err2)
    87  		require.Equal(t, v1, v2)
    88  
    89  		switch reflect.TypeOf(v2).Elem() {
    90  		case reflect.TypeOf(Issue141_Case_Insentive1{}):
    91  			println("Issue141_Case_Insentive1.Field0(tag foo) is ", v2.(*Issue141_Case_Insentive1).Field0)
    92  		case reflect.TypeOf(Issue141_Case_Insentive2{}):
    93  			println("Issue141_Case_Insentive2.Field0(tag FOO) is ", v2.(*Issue141_Case_Insentive2).Field0)
    94  		case reflect.TypeOf(Issue141_Case_Insentive3{}):
    95  			println("Issue141_Case_Insentive3.FOo is ", v2.(*Issue141_Case_Insentive3).FOo)
    96  		case reflect.TypeOf(Issue141_Case_Insentive4{}):
    97  			println("Issue141_Case_Insentive4.Field0(tag FoO) is ", v2.(*Issue141_Case_Insentive4).Field0)
    98  		case reflect.TypeOf(Issue141_Matched1{}):
    99  			println("Issue141_Matched1.Foo is ", v2.(*Issue141_Matched1).Foo)
   100  		case reflect.TypeOf(Issue141_Matched2{}):
   101  			println("Issue141_Matched2.Field3(tag Foo) is ", v2.(*Issue141_Matched2).Field3)
   102  			println("Issue141_Matched2.Foo is ", v2.(*Issue141_Matched2).Foo)
   103  		}
   104  	}
   105  }