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:
Masaaki Goshima 2021-12-05 11:54:01 +09:00 committed by GitHub
commit 5ee0d18f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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
}