mirror of https://github.com/goccy/go-json.git
Merge pull request #305 from goccy/feature/fix-recursive-ptr-head
Fix operation conversion for PtrHead to Head in Recursive type
This commit is contained in:
commit
5ee0d18f0d
|
@ -2242,3 +2242,39 @@ func TestIssue299(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRecursivePtrHead(t *testing.T) {
|
||||
type User struct {
|
||||
Account *string `json:"account"`
|
||||
Password *string `json:"password"`
|
||||
Nickname *string `json:"nickname"`
|
||||
Address *string `json:"address,omitempty"`
|
||||
Friends []*User `json:"friends,omitempty"`
|
||||
}
|
||||
user1Account, user1Password, user1Nickname := "abcdef", "123456", "user1"
|
||||
user1 := &User{
|
||||
Account: &user1Account,
|
||||
Password: &user1Password,
|
||||
Nickname: &user1Nickname,
|
||||
Address: nil,
|
||||
}
|
||||
user2Account, user2Password, user2Nickname := "ghijkl", "123456", "user2"
|
||||
user2 := &User{
|
||||
Account: &user2Account,
|
||||
Password: &user2Password,
|
||||
Nickname: &user2Nickname,
|
||||
Address: nil,
|
||||
}
|
||||
user1.Friends = []*User{user2}
|
||||
expected, err := stdjson.Marshal(user1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := json.Marshal(user1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(expected, got) {
|
||||
t.Fatalf("failed to encode. expected %q but got %q", expected, got)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,7 +120,7 @@ func (t OpType) HeadToOmitEmptyHead() OpType {
|
|||
}
|
||||
|
||||
func (t OpType) PtrHeadToHead() OpType {
|
||||
idx := strings.Index(t.String(), "Ptr")
|
||||
idx := strings.Index(t.String(), "PtrHead")
|
||||
if idx == -1 {
|
||||
return t
|
||||
}
|
||||
|
|
|
@ -894,7 +894,7 @@ func (t OpType) HeadToOmitEmptyHead() OpType {
|
|||
}
|
||||
|
||||
func (t OpType) PtrHeadToHead() OpType {
|
||||
idx := strings.Index(t.String(), "Ptr")
|
||||
idx := strings.Index(t.String(), "PtrHead")
|
||||
if idx == -1 {
|
||||
return t
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue