github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue39_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  	. "github.com/goshafaq/sonic"
    21  	"testing"
    22  
    23  	"github.com/goshafaq/sonic/decoder"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  type normalIfaceIssue39 interface {
    28  	Foo()
    29  }
    30  
    31  type normalWrapIssue39 struct {
    32  	F normalIfaceIssue39
    33  }
    34  
    35  type normalImplIssue39 struct {
    36  	X int
    37  }
    38  
    39  func (_ *normalImplIssue39) Foo() {}
    40  
    41  type jsonIfaceIssue39 interface {
    42  	UnmarshalJSON(b []byte) error
    43  }
    44  
    45  type jsonWrapIssue39 struct {
    46  	F jsonIfaceIssue39
    47  }
    48  
    49  type jsonImplIssue39 struct {
    50  	a string
    51  }
    52  
    53  func (self *jsonImplIssue39) UnmarshalJSON(b []byte) error {
    54  	self.a = string(b)
    55  	return nil
    56  }
    57  
    58  type textIfaceIssue39 interface {
    59  	UnmarshalText(b []byte) error
    60  }
    61  
    62  type textWrapIssue39 struct {
    63  	F textIfaceIssue39
    64  }
    65  
    66  type textImplIssue39 struct {
    67  	a string
    68  }
    69  
    70  func (self *textImplIssue39) UnmarshalText(b []byte) error {
    71  	self.a = string(b)
    72  	return nil
    73  }
    74  
    75  func TestIssue39_Iface(t *testing.T) {
    76  	p := new(normalImplIssue39)
    77  	obj := normalWrapIssue39{F: p}
    78  	err := Unmarshal([]byte(`{"F":{"X":123}}`), &obj)
    79  	if err != nil {
    80  		if v, ok := err.(decoder.SyntaxError); ok {
    81  			println(v.Description())
    82  		}
    83  		require.NoError(t, err)
    84  	}
    85  	require.Equal(t, 123, p.X)
    86  }
    87  
    88  func TestIssue39_UnmarshalJSON(t *testing.T) {
    89  	p := &jsonImplIssue39{}
    90  	obj := jsonWrapIssue39{F: p}
    91  	err := Unmarshal([]byte(`{"F":"xx"}`), &obj)
    92  	if err != nil {
    93  		if v, ok := err.(decoder.SyntaxError); ok {
    94  			println(v.Description())
    95  		}
    96  		require.NoError(t, err)
    97  	}
    98  	require.Equal(t, `"xx"`, p.a)
    99  }
   100  
   101  func TestIssue39_UnmarshalText(t *testing.T) {
   102  	p := &textImplIssue39{}
   103  	obj := textWrapIssue39{F: p}
   104  	err := Unmarshal([]byte(`{"F":"xx"}`), &obj)
   105  	if err != nil {
   106  		if v, ok := err.(decoder.SyntaxError); ok {
   107  			println(v.Description())
   108  		}
   109  		require.NoError(t, err)
   110  	}
   111  	require.Equal(t, `xx`, p.a)
   112  }