github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_struct_complex_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type testComplexInner struct {
     8  	testComplexOuter
     9  	A0 int
    10  	A1 int
    11  }
    12  
    13  type testComplexInner2 struct {
    14  	B0 int
    15  }
    16  
    17  type aliasTestComplexInner2 testComplexInner2
    18  
    19  type testComplexInner2Inner struct {
    20  	B0 int
    21  	B1 int
    22  }
    23  
    24  type testComplexInner2Outer struct {
    25  	testComplexInner2Inner
    26  }
    27  
    28  type testComplexInner3 struct {
    29  	C0 int
    30  }
    31  
    32  type renameTestComplexInner3 = testComplexInner3
    33  
    34  type testComplexInner3Inner struct {
    35  	C0 int
    36  	C1 int
    37  }
    38  
    39  type testComplexInner3Outer struct {
    40  	testComplexInner3Inner
    41  }
    42  
    43  type testComplexInner4A struct {
    44  	D0 int // overridden by json tag
    45  	D1 int // the sibling is renamed
    46  	D3 int // the sibling is ignored
    47  	D4 int // duplicated
    48  	D5 int `json:"D6"` // duplicated with json key
    49  	D7 int // duplicated, but inner tagged field will be promoted
    50  }
    51  
    52  type testComplexInner4A2 testComplexInner4A
    53  
    54  type testComplexInner4A3 testComplexInner4A
    55  
    56  type testComplexInner4B struct {
    57  	D0 int `json:"D0"`
    58  	D1 int `json:"D2"`
    59  	D3 int `json:"-"`
    60  	D4 int
    61  	D5 int `json:"D6"`
    62  	D7 int
    63  }
    64  
    65  type testComplexInner4CInternal struct {
    66  	D0 int
    67  	D1 int
    68  	D2 int
    69  	D3 int
    70  	D4 int
    71  	D5 int
    72  	D6 int
    73  	D7 int `json:"D7"`
    74  }
    75  
    76  type testComplexInner4C struct {
    77  	testComplexInner4CInternal
    78  }
    79  
    80  type testComplexInner5A struct {
    81  	E0 int // wins for lesser depth
    82  	E1 int `json:"E1"` // wins for tagged
    83  }
    84  
    85  type testComplexInner5BInternal struct {
    86  	E0 int
    87  }
    88  
    89  type testComplexInner5B struct {
    90  	testComplexInner5BInternal
    91  	E1 int
    92  }
    93  
    94  type testComplexOuter struct {
    95  	// nested
    96  	*testComplexInner
    97  	A0 int
    98  
    99  	// alias
   100  	testComplexInner2
   101  	aliasTestComplexInner2
   102  	testComplexInner2Outer
   103  
   104  	// duplicated (by rename)
   105  	testComplexInner3
   106  	renameTestComplexInner3
   107  	testComplexInner3Outer
   108  
   109  	// duplicate
   110  	testComplexInner4A
   111  	testComplexInner4A2 `json:"e"` // treated as named
   112  	testComplexInner4A3 `json:"-"` // ignored
   113  	testComplexInner4B
   114  	testComplexInner4C
   115  
   116  	// Ambigue
   117  	F0 int `json:"AMBIGUE"`
   118  	F1 int `json:"Ambigue"`
   119  
   120  	// sort
   121  	testComplexInner5A
   122  	testComplexInner5B
   123  }
   124  
   125  func TestValDecoder_Native_Struct_Embedded_Complex(t *testing.T) {
   126  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
   127  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   128  	}
   129  	t.Run("complex", func(t *testing.T) {
   130  		f(t, `{
   131  			"A0": 10, "A1": 11,
   132  			"B0": 20, "B1": 21,
   133              "C0": 30, "C1": 31,
   134  			"D0": 40, "D1": 41, "D2": 42,
   135  			"D3": 43, "D4": 44, "D5": 45,
   136  			"D6": 46, "D7": 47,
   137  			"ambigue": 50,
   138  			"E0": 60, "E1": 61
   139  		}`, nil, &testComplexOuter{
   140  			testComplexInner: &testComplexInner{},
   141  		}, &testComplexOuter{
   142  			testComplexInner: &testComplexInner{},
   143  		})
   144  	})
   145  }